({
shouldShowPlusHeader,
}: CommonPlusPageProps)
| 20 | ); |
| 21 | |
| 22 | export const PlusDesktop = ({ |
| 23 | shouldShowPlusHeader, |
| 24 | }: CommonPlusPageProps): ReactElement => { |
| 25 | const { |
| 26 | openCheckout, |
| 27 | isPaddleReady, |
| 28 | productOptions, |
| 29 | giftOneYear, |
| 30 | isPricesPending, |
| 31 | isOrganization, |
| 32 | } = usePaymentContext(); |
| 33 | const { giftToUser } = useGiftUserContext(); |
| 34 | const { |
| 35 | query: { selectedPlan }, |
| 36 | } = useRouter(); |
| 37 | const { isPlus } = usePlusSubscription(); |
| 38 | const initialPaymentOption = selectedPlan ? `${selectedPlan}` : null; |
| 39 | const [selectedOption, setSelectedOption] = useState<string | null>(null); |
| 40 | const ref = useRef<HTMLDivElement>(null); |
| 41 | |
| 42 | const onChangeCheckoutOption: OpenCheckoutFn = useCallback( |
| 43 | ({ priceId, giftToUserId, quantity }) => { |
| 44 | setSelectedOption(priceId); |
| 45 | openCheckout?.({ priceId, giftToUserId, quantity }); |
| 46 | }, |
| 47 | [openCheckout], |
| 48 | ); |
| 49 | |
| 50 | useEffect(() => { |
| 51 | if (!ref?.current || !isPaddleReady || selectedOption) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if (giftToUser) { |
| 56 | if (!giftOneYear) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | const { priceId } = giftOneYear; |
| 61 | setSelectedOption(priceId); |
| 62 | openCheckout?.({ priceId, giftToUserId: giftToUser.id }); |
| 63 | |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | const option = initialPaymentOption || productOptions?.[0]?.priceId; |
| 68 | |
| 69 | // Auto-select if user is not plus or it is organization checkout |
| 70 | if (option && (!isPlus || isOrganization)) { |
| 71 | setSelectedOption(option); |
| 72 | openCheckout?.({ priceId: option }); |
| 73 | } |
| 74 | }, [ |
| 75 | giftOneYear, |
| 76 | giftToUser, |
| 77 | initialPaymentOption, |
| 78 | openCheckout, |
| 79 | isPaddleReady, |
nothing calls this directly
no test coverage detected