Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / pass / components / Invite / InviteProvider.tsx
blob789f21cc716b17e9f71110bc2b8539262307d81f
1 import type { FC, PropsWithChildren } from 'react';
2 import { useEffect, useMemo, useState } from 'react';
3 import { useSelector } from 'react-redux';
5 import { useNavigation } from '@proton/pass/components/Navigation/NavigationProvider';
6 import { selectMostRecentInvite } from '@proton/pass/store/selectors/invites';
7 import type { MaybeNull } from '@proton/pass/types';
8 import type { Invite } from '@proton/pass/types/data/invites';
10 import { InviteContext, type InviteContextValue } from './InviteContext';
11 import { VaultAccessManager } from './VaultAccessManager';
12 import { VaultInviteCreate, type VaultInviteCreateValues } from './VaultInviteCreate';
13 import { VaultInviteRespond } from './VaultInviteRespond';
15 type InviteContextState =
16     | ({ view: 'invite' } & VaultInviteCreateValues<false>)
17     | ({ view: 'invite-new' } & VaultInviteCreateValues<true>)
18     | { view: 'manage'; shareId: string };
20 export const InviteProvider: FC<PropsWithChildren> = ({ children }) => {
21     const { setFilters } = useNavigation();
23     const [state, setState] = useState<MaybeNull<InviteContextState>>(null);
24     const [invite, setInvite] = useState<MaybeNull<Invite>>(null);
25     const latestInvite = useSelector(selectMostRecentInvite);
27     const handles = useMemo(
28         () => ({
29             close: () => setState(null),
30             createInvite: (props: VaultInviteCreateValues<false>) => setState({ view: 'invite', ...props }),
31             createSharedVault: (props: VaultInviteCreateValues<true>) => setState({ view: 'invite-new', ...props }),
32             manageAccess: (shareId: string) => setState({ view: 'manage', shareId }),
33             onInviteResponse: () => setInvite(null),
34         }),
35         []
36     );
38     const contextValue = useMemo<InviteContextValue>(
39         () => ({
40             ...handles,
41             latestInvite,
42             onShareDisabled: (disabledShareId: string) => {
43                 const shareId = (() => {
44                     switch (state?.view) {
45                         case 'invite':
46                             return state.vault.shareId;
47                         case 'manage':
48                             return state.shareId;
49                         default:
50                             return null;
51                     }
52                 })();
54                 if (disabledShareId === shareId) {
55                     setInvite(null);
56                     setState(null);
57                 }
58             },
59             respondToInvite: setInvite,
60         }),
61         [state, latestInvite]
62     );
64     useEffect(() => {
65         /* If the latest invite was promoted from a new user invite,
66          * auto prompt the "respond to invite" modal */
67         if (latestInvite?.fromNewUser) setInvite(latestInvite);
68     }, [latestInvite]);
70     return (
71         <InviteContext.Provider value={contextValue}>
72             {(() => {
73                 switch (state?.view) {
74                     case 'invite':
75                         return <VaultInviteCreate withVaultCreation={false} {...state} />;
76                     case 'invite-new':
77                         return (
78                             <VaultInviteCreate
79                                 withVaultCreation
80                                 {...state}
81                                 onVaultCreated={(selectedShareId) => setFilters({ selectedShareId })}
82                             />
83                         );
84                     case 'manage':
85                         return <VaultAccessManager shareId={state.shareId} />;
86                     default:
87                         return null;
88                 }
89             })()}
91             {invite && <VaultInviteRespond {...invite} />}
92             {children}
93         </InviteContext.Provider>
94     );