-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathPlusDesktop.tsx
More file actions
133 lines (122 loc) · 4.13 KB
/
PlusDesktop.tsx
File metadata and controls
133 lines (122 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type { ReactElement } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useRouter } from 'next/router';
import dynamic from 'next/dynamic';
import type { OpenCheckoutFn } from '../../contexts/payment/context';
import { usePaymentContext } from '../../contexts/payment/context';
import { PlusInfo } from './PlusInfo';
import { PlusCheckoutContainer } from './PlusCheckoutContainer';
import { useGiftUserContext } from './GiftUserContext';
import type { CommonPlusPageProps } from './common';
import { PlusTrustRefund } from './PlusTrustRefund';
import { usePlusSubscription } from '../../hooks';
import { PurchaseType } from '../../graphql/paddle';
import { PlusProductToggle } from './PlusProductToggle';
import { useFeature } from '../GrowthBookProvider';
import { featurePlusApiLanding } from '../../lib/featureManagement';
const PlusFAQs = dynamic(() => import('./PlusFAQ').then((mod) => mod.PlusFAQ));
const PlusApiShowcase = dynamic(() =>
import('./PlusApiShowcase').then((mod) => mod.PlusApiShowcase),
);
export const PlusDesktop = ({
shouldShowPlusHeader,
}: CommonPlusPageProps): ReactElement => {
const {
openCheckout,
isPaddleReady,
productOptions,
giftOneYear,
isPricesPending,
isOrganization,
} = usePaymentContext();
const { giftToUser } = useGiftUserContext();
const {
query: { selectedPlan },
} = useRouter();
const { isPlus } = usePlusSubscription();
const isApiLanding = useFeature(featurePlusApiLanding);
const initialPaymentOption = selectedPlan ? `${selectedPlan}` : null;
const [selectedOption, setSelectedOption] = useState<string | null>(null);
const ref = useRef<HTMLDivElement>(null);
const onChangeCheckoutOption: OpenCheckoutFn = useCallback(
({ priceId, giftToUserId, quantity }) => {
setSelectedOption(priceId);
openCheckout?.({ priceId, giftToUserId, quantity });
},
[openCheckout],
);
useEffect(() => {
if (!ref?.current || !isPaddleReady || selectedOption) {
return;
}
if (giftToUser) {
if (!giftOneYear) {
return;
}
const { priceId } = giftOneYear;
setSelectedOption(priceId);
openCheckout?.({ priceId, giftToUserId: giftToUser.id });
return;
}
const option = initialPaymentOption || productOptions?.[0]?.priceId;
// Auto-select if user is not plus or it is organization checkout
if (option && (!isPlus || isOrganization)) {
setSelectedOption(option);
openCheckout?.({ priceId: option });
}
}, [
giftOneYear,
giftToUser,
initialPaymentOption,
openCheckout,
isPaddleReady,
isPlus,
productOptions,
selectedOption,
isOrganization,
]);
return (
<>
<div className="flex flex-1 justify-center gap-20 pt-10">
<div className="flex w-[28.5rem] flex-col">
{!giftToUser && (
<PlusProductToggle
options={[
{ priceType: PurchaseType.Plus, label: 'Personal' },
{
priceType: PurchaseType.Organization,
label: 'Team',
},
]}
onSelect={() => setSelectedOption(null)}
className="self-start"
/>
)}
<PlusInfo
productOptions={productOptions ?? []}
selectedOption={selectedOption}
onChange={onChangeCheckoutOption}
shouldShowPlusHeader={shouldShowPlusHeader}
/>
</div>
<div className="flex flex-col gap-4">
<PlusCheckoutContainer
checkoutRef={ref}
className={{
container:
'min-h-40 w-[28.5rem] rounded-16 border border-border-subtlest-tertiary bg-background-default p-5 empty:min-h-[50vh] empty:bg-surface-float',
element: 'h-[35rem]',
}}
/>
{!isPricesPending && !giftToUser && (
<div className="flex justify-center">
<PlusTrustRefund />
</div>
)}
</div>
</div>
{isApiLanding && !isOrganization && !giftToUser && <PlusApiShowcase />}
<PlusFAQs />
</>
);
};