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';
14 * useLinkDetailsView loads link if not cached yet with all signature issues
15 * and number of accesses to shared URL.
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();
39 const abortController = new AbortController();
40 void withLoadingLink(async () => {
42 await getSharePermissions(abortController.signal, shareId).then(setPermissions);
44 const link = await getLink(abortController.signal, shareId, linkId);
46 void withLoadingSignatureIssues(
47 checkLinkSignatures(abortController.signal, shareId, linkId)
48 .then(setSignatureIssues)
50 setSignatureNetworkError(true);
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)
69 abortController.abort();
71 }, [shareId, linkId]);
76 isSignatureIssuesLoading,
77 isNumberOfAccessesLoading,
82 signatureNetworkError,