Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / store / _views / useLinkDetailsView.tsx
blob908f6954d3efc67d6f840e08ab02767d02f38498
1 import { useEffect, useState } from 'react';
3 import { useLoading } from '@proton/hooks';
4 import { SHARE_MEMBER_PERMISSIONS } from '@proton/shared/lib/drive/permissions';
6 import { sendErrorReport } from '../../utils/errorHandling';
7 import { useActions } from '../_actions';
8 import type { DecryptedLink, SignatureIssues } from '../_links';
9 import { useLink } from '../_links';
10 import { useShareUrl } from '../_shares';
11 import { useDirectSharingInfo } from '../_shares/useDirectSharingInfo';
13 /**
14  * useLinkDetailsView loads link if not cached yet with all signature issues
15  * and number of accesses to shared URL.
16  */
17 export default function useLinkDetailsView(shareId: string, linkId: string) {
18     const { checkLinkSignatures } = useActions();
19     const { loadShareUrlNumberOfAccesses } = useShareUrl();
20     const { getLink } = useLink();
21     const { isSharedWithMe, getSharePermissions } = useDirectSharingInfo();
23     // permissions load will be during the withLoading process, but we prefer to set owner by default,
24     // so even if it's wrong permissions, BE will prevent any unauthorized actions
25     const [permissions, setPermissions] = useState<SHARE_MEMBER_PERMISSIONS>(SHARE_MEMBER_PERMISSIONS.OWNER);
26     const [link, setLink] = useState<DecryptedLink>();
27     const [error, setError] = useState<any>();
28     const [isSharedWithMeLink, setIsSharedWithMeLink] = useState(false);
29     const [isLinkLoading, withLoadingLink] = useLoading();
31     const [signatureIssues, setSignatureIssues] = useState<SignatureIssues>();
32     const [signatureNetworkError, setSignatureNetworkError] = useState<boolean>(false);
33     const [isSignatureIssuesLoading, withLoadingSignatureIssues] = useLoading();
35     const [numberOfAccesses, setNumberOfAccesses] = useState<number>();
36     const [isNumberOfAccessesLoading, withLoadingNumberOfAccesses] = useLoading();
38     useEffect(() => {
39         const abortController = new AbortController();
40         void withLoadingLink(async () => {
41             try {
42                 await getSharePermissions(abortController.signal, shareId).then(setPermissions);
44                 const link = await getLink(abortController.signal, shareId, linkId);
45                 setLink(link);
46                 void withLoadingSignatureIssues(
47                     checkLinkSignatures(abortController.signal, shareId, linkId)
48                         .then(setSignatureIssues)
49                         .catch(() => {
50                             setSignatureNetworkError(true);
51                         })
52                 );
53                 const sharedWithMe = await isSharedWithMe(abortController.signal, shareId);
54                 setIsSharedWithMeLink(sharedWithMe);
55                 if (link.shareId && !sharedWithMe) {
56                     void withLoadingNumberOfAccesses(
57                         loadShareUrlNumberOfAccesses(abortController.signal, shareId, linkId)
58                             .then(setNumberOfAccesses)
59                             .catch(sendErrorReport)
60                     );
61                 }
62             } catch (err) {
63                 setError(err);
64                 sendErrorReport(err);
65             }
66         });
68         return () => {
69             abortController.abort();
70         };
71     }, [shareId, linkId]);
73     return {
74         permissions,
75         isLinkLoading,
76         isSignatureIssuesLoading,
77         isNumberOfAccessesLoading,
78         isSharedWithMeLink,
79         error,
80         link,
81         signatureIssues,
82         signatureNetworkError,
83         numberOfAccesses,
84     };