-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
PremiumFeature.tsx
157 lines (139 loc) · 5.01 KB
/
PremiumFeature.tsx
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { ReactComponent as ProPlanIcon } from 'assets/icons/pro-enterprise-feature-badge.svg';
import { Box, Button, Link, styled, Typography } from '@mui/material';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
import { ConditionallyRender } from '../ConditionallyRender/ConditionallyRender';
const PremiumFeatureWrapper = styled(Box, {
shouldForwardProp: prop => prop !== 'tooltip',
})<{ tooltip?: boolean }>(({ theme, tooltip }) => ({
display: 'flex',
flexDirection: 'column',
alignItems: tooltip ? 'start' : 'center',
textAlign: tooltip ? 'left' : 'center',
backgroundColor: tooltip ? 'transparent' : theme.palette.secondaryContainer,
borderRadius: tooltip ? 0 : theme.shape.borderRadiusLarge,
padding: tooltip ? theme.spacing(1, 0.5) : theme.spacing(7.5, 1),
}));
const StyledTitle = styled(Typography)(({ theme }) => ({
display: 'inline-flex',
alignItems: 'center',
fontWeight: theme.fontWeight.bold,
gap: theme.spacing(1),
}));
const StyledBody = styled('div', {
shouldForwardProp: prop => prop !== 'tooltip',
})<{ tooltip?: boolean }>(({ theme, tooltip }) => ({
margin: tooltip ? theme.spacing(1, 0) : theme.spacing(3, 0, 5, 0),
}));
const StyledTypography = styled(Typography)(({ theme }) => ({
fontSize: theme.fontSizes.smallBody,
}));
const StyledButtonContainer = styled('div')(() => ({
display: 'flex',
}));
const StyledLink = styled(Link)(({ theme }) => ({
fontSize: theme.fontSizes.smallBody,
}));
enum FeaturePlan {
PRO = 'Pro & Enterprise',
ENTERPRISE = 'Enterprise',
}
const PremiumFeatures = {
'adding-new-projects': {
plan: FeaturePlan.PRO,
url: '',
label: 'Adding new projects',
},
access: {
plan: FeaturePlan.PRO,
url: 'https://docs.getunleash.io/reference/rbac',
label: 'Access',
},
'change-requests': {
plan: FeaturePlan.ENTERPRISE,
url: 'https://docs.getunleash.io/reference/change-requests',
label: 'Change Requests',
},
};
type PremiumFeatureType = keyof typeof PremiumFeatures;
const UPGRADE_URL = 'https://www.getunleash.io/plans';
export interface PremiumFeatureProps {
feature: PremiumFeatureType;
tooltip?: boolean;
}
export const PremiumFeature = ({ feature, tooltip }: PremiumFeatureProps) => {
const { url, plan, label } = PremiumFeatures[feature];
const tracker = usePlausibleTracker();
const handleClick = () => {
tracker.trackEvent('upgrade_plan_clicked', {
props: { feature: label },
});
};
const featureLabel = Boolean(url) ? (
<StyledLink href={url} target="_blank">
{label}
</StyledLink>
) : (
label
);
const featureMessage = (
<>
{featureLabel} is a feature available for the{' '}
<strong>{plan}</strong>{' '}
{plan === FeaturePlan.PRO ? 'plans' : 'plan'}
</>
);
const upgradeUrl = `${UPGRADE_URL}?feature=${feature}`;
return (
<PremiumFeatureWrapper tooltip={tooltip}>
<StyledTitle>
<ProPlanIcon />
{`${plan} feature`}
</StyledTitle>
<ConditionallyRender
condition={Boolean(tooltip)}
show={
<>
<StyledBody tooltip>
<StyledTypography>
{featureMessage}. You need to upgrade your plan
if you want to use it
</StyledTypography>
</StyledBody>
<StyledButtonContainer>
<StyledLink
href={upgradeUrl}
target="_blank"
onClick={handleClick}
>
Upgrade now
</StyledLink>
</StyledButtonContainer>
</>
}
elseShow={
<>
<StyledBody>
<StyledTypography>
{featureMessage}
</StyledTypography>
<StyledTypography>
You need to upgrade your plan if you want to use
it
</StyledTypography>
</StyledBody>
<StyledButtonContainer>
<Button
variant="outlined"
href={upgradeUrl}
target="_blank"
onClick={handleClick}
>
Upgrade now
</Button>
</StyledButtonContainer>
</>
}
/>
</PremiumFeatureWrapper>
);
};