Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / useIsPaidUserCookie.ts
blob91788bff36ee6b8dce558ca18a03d6fefda373f9
1 import { useEffect } from 'react';
3 import { useUser } from '@proton/account/user/hooks';
4 import { getCookie, setCookie } from '@proton/shared/lib/helpers/cookies';
5 import { getSecondLevelDomain } from '@proton/shared/lib/helpers/url';
7 const COOKIE_NAME = 'no-offer';
8 const today = new Date();
9 const lastDayOfTheYear = new Date(today.getFullYear(), 11, 31, 23, 59, 59);
10 const cookieDomain = `.${getSecondLevelDomain(window.location.hostname)}`;
12 /**
13  * Set a cookie for non eligible user to BF offer
14  * Used by proton.me website to hide BF banner
15  */
16 const useIsPaidUserCookie = () => {
17     const [user, loadingUser] = useUser();
18     const loading = loadingUser;
20     useEffect(() => {
21         if (loading) {
22             return;
23         }
25         const cookie = getCookie(COOKIE_NAME);
26         const isPaid = user.isPaid;
27         const shouldSet = isPaid;
29         if (shouldSet && cookie !== '1') {
30             setCookie({
31                 cookieName: COOKIE_NAME,
32                 cookieValue: '1',
33                 cookieDomain,
34                 expirationDate: lastDayOfTheYear.toUTCString(),
35                 path: '/',
36             });
37         } else if (!shouldSet && cookie === '1') {
38             setCookie({
39                 cookieName: COOKIE_NAME,
40                 cookieValue: undefined,
41                 cookieDomain,
42                 expirationDate: new Date(0).toUTCString(),
43                 path: '/',
44             });
45         }
46     }, [user, loading]);
49 export default useIsPaidUserCookie;