Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / shared / lib / helpers / upsell.ts
blob58c7ae6fb902a7b0f54bc16e2835987b50810399
1 import type { APP_NAMES, UPSELL_COMPONENT } from '@proton/shared/lib/constants';
2 import { APPS, APP_UPSELL_REF_PATH, PLANS } from '@proton/shared/lib/constants';
3 import { getPlan } from '@proton/shared/lib/helpers/subscription';
4 import type { Audience, Subscription, UserModel } from '@proton/shared/lib/interfaces';
6 /**
7  * Add an upsell ref param to a URL
8  */
9 export const addUpsellPath = (link: string, upsellPath?: string) => {
10     if (!upsellPath) {
11         return link;
12     }
14     const hasParams = link.includes('?');
15     return hasParams ? `${link}&ref=${upsellPath}` : `${link}?ref=${upsellPath}`;
18 export const getUpgradePath = ({
19     user,
20     subscription,
21     plan,
22     app,
23     audience,
24     target,
25 }: {
26     user?: UserModel;
27     plan?: PLANS;
28     subscription?: Subscription;
29     audience?: Audience;
30     app?: APP_NAMES;
31     target?: 'compare' | 'checkout';
32 }) => {
33     const params = new URLSearchParams();
34     if (plan) {
35         params.set('plan', plan);
36     }
37     if (audience) {
38         params.set('audience', audience);
39     }
40     if (!user || user.isFree) {
41         if (app === APPS.PROTONVPN_SETTINGS) {
42             return `/dashboard${params.size ? `?${params}` : ''}`;
43         }
44         return `/upgrade${params.size ? `?${params}` : ''}`;
45     }
46     const currentPlan = getPlan(subscription);
47     // A plan is needed to open the subscription modal
48     if (!params.has('plan')) {
49         params.set('plan', currentPlan?.Name ?? PLANS.BUNDLE);
50     }
51     params.set('target', target ?? 'compare');
52     return `/dashboard?${params}`;
55 /**
56  * Generate upsell ref from app component and feature
57  *
58  * @param app => Current app from which we open a link
59  * @param feature => feature identifier to include in the path
60  * @param component => Optional, ref component (e.g. banner, modal, button)
61  */
62 export const getUpsellRef = ({
63     app,
64     feature,
65     component,
66     isSettings = false,
67 }: {
68     app: APP_UPSELL_REF_PATH;
69     feature: string;
70     component?: UPSELL_COMPONENT;
71     isSettings?: boolean;
72 }) => {
73     const upsellComponent = component || '';
74     const upsellInSettings = isSettings ? '_settings' : '';
76     return `${app}${upsellComponent}${feature}${upsellInSettings}`;
79 /**
80  * Generate upsell ref from the current app, the "feature" identifier, the "component" and the "from app"
81  *
82  * @param app => Current app from which we open a link
83  * @param feature => feature identifier to include in the path
84  * @param component => Optional, ref component (e.g. banner, modal, button)
85  * @param fromApp => Optional, "parent app" of the current app (e.g. in mail settings, app=account and fromApp=mail)
86  *
87  * Return a ref string like "upsell_mail-banner-auto-reply_settings"
88  */
89 export const getUpsellRefFromApp = ({
90     app,
91     fromApp,
92     component,
93     feature,
94 }: {
95     app: APP_NAMES;
96     feature: string;
97     component?: UPSELL_COMPONENT;
98     fromApp?: APP_NAMES;
99 }) => {
100     if (app === APPS.PROTONMAIL) {
101         return getUpsellRef({ app: APP_UPSELL_REF_PATH.MAIL_UPSELL_REF_PATH, feature, component });
102     } else if (app === APPS.PROTONCALENDAR) {
103         return getUpsellRef({ app: APP_UPSELL_REF_PATH.CALENDAR_UPSELL_REF_PATH, feature, component });
104     } else if (app === APPS.PROTONDRIVE) {
105         return getUpsellRef({ app: APP_UPSELL_REF_PATH.DRIVE_UPSELL_REF_PATH, feature, component });
106     } else if (app === APPS.PROTONACCOUNT && fromApp) {
107         if (fromApp === APPS.PROTONMAIL) {
108             return getUpsellRef({
109                 app: APP_UPSELL_REF_PATH.MAIL_UPSELL_REF_PATH,
110                 feature,
111                 component,
112                 isSettings: true,
113             });
114         } else if (fromApp === APPS.PROTONCALENDAR) {
115             return getUpsellRef({
116                 app: APP_UPSELL_REF_PATH.CALENDAR_UPSELL_REF_PATH,
117                 feature,
118                 component,
119                 isSettings: true,
120             });
121         } else if (fromApp === APPS.PROTONDRIVE) {
122             return getUpsellRef({
123                 app: APP_UPSELL_REF_PATH.DRIVE_UPSELL_REF_PATH,
124                 feature,
125                 component,
126                 isSettings: true,
127             });
128         } else if (fromApp === APPS.PROTONPASS) {
129             return getUpsellRef({
130                 app: APP_UPSELL_REF_PATH.PASS_UPSELL_REF_PATH,
131                 feature,
132                 component,
133                 isSettings: true,
134             });
135         } else if (fromApp === APPS.PROTONVPN_SETTINGS) {
136             return getUpsellRef({ app: APP_UPSELL_REF_PATH.VPN_UPSELL_REF_PATH, feature, component, isSettings: true });
137         }
138     }
140     return undefined;