1 import { useCallback, useEffect, useRef } from 'react';
3 import { useLoading } from '@proton/hooks';
4 import { SORT_DIRECTION } from '@proton/shared/lib/constants';
6 import { sendErrorReport } from '../../utils/errorHandling';
7 import { useLinksListing } from '../_links';
8 import { useUserSettings } from '../_settings';
9 import { useDefaultShare, useDriveSharingFlags } from '../_shares';
10 import { useAbortSignal, useMemoArrayNoMatterTheOrder, useSortingWithDefault } from './utils';
11 import type { SortField } from './utils/useSorting';
13 const DEFAULT_SORT = {
14 sortField: 'name' as SortField,
15 sortOrder: SORT_DIRECTION.ASC,
19 * useSharedLinksView provides data for shared links by URL view (file browser of shared links).
21 export default function useSharedLinksView(shareId: string) {
22 const { getDefaultShare } = useDefaultShare();
23 const volumeId = useRef<string>();
24 const [isLoading, withLoading] = useLoading(true);
25 const { isSharingInviteAvailable } = useDriveSharingFlags();
26 const linksListing = useLinksListing();
27 const loadSharedLinks = useCallback(
28 async (signal: AbortSignal) => {
29 const defaultShare = await getDefaultShare(signal);
30 volumeId.current = defaultShare.volumeId;
31 if (isSharingInviteAvailable) {
32 await linksListing.loadLinksSharedByMeLink(signal, defaultShare.volumeId);
34 await linksListing.loadLinksSharedByLinkLEGACY(signal, defaultShare.volumeId);
37 [isSharingInviteAvailable]
38 ); //TODO: No all deps params as too much work needed in linksListing
39 const abortSignal = useAbortSignal([shareId, withLoading, loadSharedLinks]);
41 const { links: sharedLinks, isDecrypting } = linksListing.getCachedSharedByLink(abortSignal, volumeId.current);
42 const cachedSharedLinks = useMemoArrayNoMatterTheOrder(sharedLinks);
44 const { layout } = useUserSettings();
45 const { sortedList, sortParams, setSorting } = useSortingWithDefault(cachedSharedLinks, DEFAULT_SORT);
48 const ac = new AbortController();
49 void withLoading(loadSharedLinks(ac.signal).catch(sendErrorReport));
60 isLoading: isLoading || isDecrypting,