Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / store / _views / usePublicFolderView.tsx
blob792d6949cc652c0d81546a7260389fdb727b3eec
1 import { useEffect, useMemo } from 'react';
3 import { useLoading } from '@proton/hooks';
4 import { SORT_DIRECTION } from '@proton/shared/lib/constants';
5 import { LayoutSetting } from '@proton/shared/lib/interfaces/drive/userSettings';
7 import { useSelectionControls } from '../../components/FileBrowser';
8 import { sendErrorReport } from '../../utils/errorHandling';
9 import { usePublicLinksListing } from '../_links';
10 import { useAbortSignal, useLinkName, useMemoArrayNoMatterTheOrder, useSortingWithDefault } from './utils';
11 import { SortField } from './utils/useSorting';
13 export const DEFAULT_SORT = {
14     sortField: SortField.name,
15     sortOrder: SORT_DIRECTION.ASC,
18 /**
19  * usePublicFolderView provides data for folder view (file browser of folder).
20  * It is very similar to useFolderView for private app, this is just for the
21  * publicly shared folder.
22  */
23 export default function usePublicFolderView(token: string, linkId: string) {
24     const folderName = useLinkName(token, linkId);
26     const abortSignal = useAbortSignal([token, linkId]);
28     const [isLoading, withLoading] = useLoading(true);
30     const linksListing = usePublicLinksListing();
31     const { links: children, isDecrypting } = linksListing.getCachedChildren(abortSignal, token, linkId);
32     const cachedChildren = useMemoArrayNoMatterTheOrder(children);
34     const { sortedList, sortParams, setSorting } = useSortingWithDefault(cachedChildren, DEFAULT_SORT);
36     const sortedListForSelection = useMemo(() => {
37         return sortedList.map((item) => ({
38             id: item.linkId,
39             disabled: item.isLocked,
40             data: item,
41         }));
42     }, [sortedList]);
44     const selectionControls = useSelectionControls({ itemIds: sortedListForSelection.map((item) => item.id) });
46     useEffect(() => {
47         const ac = new AbortController();
48         void withLoading(linksListing.loadChildren(ac.signal, token, linkId).catch(sendErrorReport));
49         return () => {
50             ac.abort();
51         };
52     }, [token, linkId]);
54     return {
55         layout: LayoutSetting.List,
56         folderName,
57         items: sortedList,
58         sortParams,
59         setSorting,
60         selectionControls,
61         isLoading: isLoading || isDecrypting,
62     };