Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / store / _views / useVolumeLinkView.tsx
blob48c1d76d17598d652ae1456b377bbad6fd6063be
1 import { c } from 'ttag';
3 import { useNotifications } from '@proton/components';
4 import { queryResolveContextShare } from '@proton/shared/lib/api/drive/share';
5 import { API_CUSTOM_ERROR_CODES } from '@proton/shared/lib/errors';
7 import { sendErrorReport } from '../../utils/errorHandling';
8 import { useInvitationsActions } from '../_actions';
9 import { useDebouncedRequest } from '../_api';
10 import { EXTERNAL_INVITATIONS_ERROR_NAMES, useInvitations } from '../_invitations';
11 import type { DecryptedLink } from '../_links';
12 import { useLink } from '../_links';
13 import { type ShareInvitationDetails } from '../_shares';
14 import { useVolumesState } from '../_volumes';
16 export const useVolumeLinkView = () => {
17     const { getInvitationDetails, convertExternalInvitation } = useInvitations();
18     const { acceptInvitation } = useInvitationsActions();
19     const debouncedRequest = useDebouncedRequest();
20     const { getLink } = useLink();
22     const { createNotification } = useNotifications();
23     const volumeState = useVolumesState();
25     const getContextShareLinkDetails = async (
26         abortSignal: AbortSignal,
27         { volumeId, linkId }: { volumeId: string; linkId: string }
28     ) => {
29         const { ContextShareID: contextShareId } = await debouncedRequest<{
30             Code: number;
31             ContextShareID: string;
32         }>(queryResolveContextShare({ volumeId, linkId }));
33         return getLink(abortSignal, contextShareId, linkId);
34     };
36     const handleRedirectOrAcceptInvitation = async (
37         abortSignal: AbortSignal,
38         {
39             invitationId,
40             volumeId,
41             linkId,
42         }: {
43             invitationId: string;
44             volumeId: string;
45             linkId: string;
46         }
47     ): Promise<{ linkId: string; shareId: string; isFile: boolean; mimeType: string } | undefined> => {
48         try {
49             const invitationDetails: ShareInvitationDetails | undefined = await getInvitationDetails(abortSignal, {
50                 invitationId,
51                 volumeId,
52                 linkId,
53             }).catch(async (error) => {
54                 if (error.data?.Code === API_CUSTOM_ERROR_CODES.NOT_FOUND) {
55                     return undefined;
56                 }
57                 throw error;
58             });
60             if (!invitationDetails) {
61                 const link: DecryptedLink | undefined = await getContextShareLinkDetails(abortSignal, {
62                     volumeId,
63                     linkId,
64                 }).catch((error) => {
65                     if (error.data?.Code === API_CUSTOM_ERROR_CODES.NOT_FOUND) {
66                         return undefined;
67                     }
68                     throw error;
69                 });
70                 if (link?.shareId) {
71                     volumeState.setVolumeShareIds(volumeId, [link.shareId]);
72                     return {
73                         linkId: link.linkId,
74                         shareId: link.shareId,
75                         isFile: link.isFile,
76                         mimeType: link.mimeType,
77                     };
78                 }
79                 // This will happen if we can't find the invite and the file/folder does not exist
80                 return;
81             }
82             const acceptedInvitation = await acceptInvitation(abortSignal, invitationId, false);
83             if (!acceptedInvitation) {
84                 return;
85             }
86             return {
87                 linkId: invitationDetails.link.linkId,
88                 shareId: invitationDetails.share.shareId,
89                 isFile: invitationDetails.link.isFile,
90                 mimeType: invitationDetails.link.mimeType,
91             };
92         } catch (error) {
93             // This is to make TS work with error typing
94             let message;
95             if (error instanceof Error) {
96                 message = error.message;
97             }
98             if (message) {
99                 sendErrorReport(error);
100                 createNotification({
101                     type: 'error',
102                     text: message,
103                 });
104             }
105             throw error;
106         }
107     };
109     const handleConvertExternalInvitation = async (
110         abortSignal: AbortSignal,
111         {
112             externalInvitationId,
113             linkId,
114         }: {
115             externalInvitationId: string;
116             linkId: string;
117         }
118     ) => {
119         return convertExternalInvitation(abortSignal, {
120             externalInvitationId,
121             linkId,
122         })
123             .then((result) => {
124                 if (result?.code === 1000) {
125                     createNotification({
126                         type: 'success',
127                         text: c('Notification').t`An invitation has been sent`,
128                     });
129                 } else {
130                     createNotification({
131                         type: 'error',
132                         text: c('Notification').t`Failed to convert external invitation`,
133                     });
134                 }
135             })
136             .catch((error) => {
137                 if (error.message) {
138                     if (
139                         error.name !== EXTERNAL_INVITATIONS_ERROR_NAMES.DISABLED &&
140                         error.name !== EXTERNAL_INVITATIONS_ERROR_NAMES.NOT_FOUND
141                     ) {
142                         sendErrorReport(error);
143                     }
144                     createNotification({
145                         type: 'error',
146                         text: error.message,
147                     });
148                 }
149                 throw error;
150             });
151     };
153     return { handleRedirectOrAcceptInvitation, handleConvertExternalInvitation };