Merge branch 'renovate/all-minor-patch' into 'main'
[ProtonMail-WebClient.git] / packages / cross-storage / account-impl / guestInstance.ts
blob77ba741ff3caa0bc283706776854a68097be5fe5
1 import { getAppHref } from '@proton/shared/lib/apps/helper';
2 import { APPS } from '@proton/shared/lib/constants';
3 import { appMode } from '@proton/shared/lib/webpack.constants';
5 import create from '../lib/guest';
6 import type {
7     GetLocalStorageKeysMessageResponse,
8     GetLocalStorageMessageResponse,
9     ProtonMessages,
10     RemoveLocalStorageMessageResponse,
11     SetLocalStorageMessageResponse,
12 } from './interface';
13 import { Action } from './interface';
15 const createProtonInstance = (url: string) => create<ProtonMessages>(url);
17 type CrossStorageInstance = ReturnType<typeof createProtonInstance>;
19 export let instance: ReturnType<typeof createHandlers>;
21 const createHandlers = ({ postAndGetMessage, supported }: CrossStorageInstance) => {
22     const getLocalStorage = (key: string) => {
23         return postAndGetMessage<GetLocalStorageMessageResponse>({
24             type: Action.getLocalStorage,
25             payload: {
26                 key,
27             },
28         });
29     };
31     const getLocalStorageKeys = () => {
32         return postAndGetMessage<GetLocalStorageKeysMessageResponse>({
33             type: Action.getLocalStorageKeys,
34         });
35     };
37     const setLocalStorage = (key: string, value: string): Promise<void | undefined> => {
38         return postAndGetMessage<SetLocalStorageMessageResponse>({
39             type: Action.setLocalStorage,
40             payload: {
41                 key,
42                 value,
43             },
44         });
45     };
47     const removeLocalStorage = (key: string): Promise<void | undefined> => {
48         return postAndGetMessage<RemoveLocalStorageMessageResponse>({
49             type: Action.removeLocalStorage,
50             payload: {
51                 key,
52             },
53         });
54     };
56     return {
57         setLocalStorage,
58         getLocalStorage,
59         getLocalStorageKeys,
60         removeLocalStorage,
61         supported,
62     };
65 export const setupGuestCrossStorage = () => {
66     if (appMode !== 'sso') {
67         return;
68     }
69     instance = createHandlers(createProtonInstance(getAppHref('/storage.html', APPS.PROTONACCOUNT)));