-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathUpgradeToPlus.tsx
More file actions
87 lines (81 loc) · 2.59 KB
/
UpgradeToPlus.tsx
File metadata and controls
87 lines (81 loc) · 2.59 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
import React, { useCallback } from 'react';
import type { ReactElement } from 'react';
import classNames from 'classnames';
import type { ButtonSize } from './buttons/Button';
import { ButtonColor, Button, ButtonVariant } from './buttons/Button';
import { DevPlusIcon } from './icons';
import Link from './utilities/Link';
import { plusUrl } from '../lib/constants';
import { useConditionalFeature, useViewSize, ViewSize } from '../hooks';
import { usePlusSubscription } from '../hooks/usePlusSubscription';
import type { TargetId } from '../lib/log';
import { LogEvent } from '../lib/log';
import { useAuthContext } from '../contexts/AuthContext';
import { AuthTriggers } from '../lib/auth';
import type { WithClassNameProps } from './utilities';
import { featurePlusApiLanding } from '../lib/featureManagement';
type Props = {
iconOnly?: boolean;
target: TargetId;
size?: ButtonSize;
variant?: ButtonVariant;
color?: ButtonColor;
} & WithClassNameProps;
export const UpgradeToPlus = ({
className,
color,
size,
iconOnly = false,
target,
variant,
...attrs
}: Props): ReactElement | null => {
const { isLoggedIn, showLogin } = useAuthContext();
const isLaptop = useViewSize(ViewSize.Laptop);
const isLaptopXL = useViewSize(ViewSize.LaptopXL);
const isFullCTAText = !isLaptop || isLaptopXL;
const { isPlus, logSubscriptionEvent } = usePlusSubscription();
const { value: isApiLanding } = useConditionalFeature({
feature: featurePlusApiLanding,
shouldEvaluate: !isPlus,
});
const ctaCopy = isApiLanding
? { full: 'Get API Access', short: 'API access' }
: { full: 'Level Up with Plus', short: 'Upgrade' };
const content = isFullCTAText ? ctaCopy.full : ctaCopy.short;
const defaultColor = isApiLanding ? ButtonColor.Bacon : ButtonColor.Avocado;
const onClick = useCallback(
(e: React.MouseEvent) => {
if (!isLoggedIn) {
e.preventDefault();
showLogin({ trigger: AuthTriggers.Plus });
return;
}
logSubscriptionEvent({
event_name: LogEvent.UpgradeSubscription,
target_id: target,
});
},
[isLoggedIn, logSubscriptionEvent, showLogin, target],
);
if (isPlus) {
return null;
}
return (
<Link passHref href={plusUrl}>
<Button
tag="a"
className={classNames(!iconOnly && 'flex-1', className)}
icon={<DevPlusIcon />}
size={size}
color={defaultColor}
variant={ButtonVariant.Primary}
onClick={onClick}
{...(variant && { variant, color })}
{...attrs}
>
{iconOnly ? null : content}
</Button>
</Link>
);
};