Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / drive / src / app / store / _views / useLinksDetailsView.tsx
blob95ebd3e470322c047b226f0732fde068e41ffdb2
1 import { useEffect, useState } from 'react';
3 import { useLoading } from '@proton/hooks';
5 import { sendErrorReport } from '../../utils/errorHandling';
6 import type { DecryptedLink } from '../_links';
7 import { useLinksListing } from '../_links';
9 /**
10  * useLinksDetailsView loads links if not cached yet and provides some
11  * aggregated information such as their count or size.
12  */
13 export default function useLinksDetailsView(selectedLinks: { rootShareId: string; linkId: string }[]) {
14     const { loadLinksMeta } = useLinksListing();
16     const [links, setLinks] = useState<DecryptedLink[]>([]);
17     const [hasError, setHasError] = useState<any>();
18     const [isLoading, withLoading] = useLoading();
20     useEffect(() => {
21         const ac = new AbortController();
23         const linksByShareId: Record<string, string[]> = {};
25         selectedLinks.forEach(({ rootShareId, linkId }) => {
26             if (!linksByShareId[rootShareId]) {
27                 linksByShareId[rootShareId] = [];
28             }
30             linksByShareId[rootShareId].push(linkId);
31         });
33         void withLoading(async () => {
34             try {
35                 const loadedLinks: DecryptedLink[] = [];
37                 for (const shareId of Object.keys(linksByShareId)) {
38                     const linkIds = linksByShareId[shareId];
39                     const meta = await loadLinksMeta(ac.signal, 'details', shareId, linkIds);
41                     if (meta.errors.length > 0) {
42                         setHasError(true);
43                         console.error(new Error('Failed to load links meta in details modal'), {
44                             shareId,
45                             linkIds: linkIds.filter((id) => !meta.links.find((link) => link.linkId === id)),
46                             errors: meta.errors,
47                         });
49                         return;
50                     }
52                     loadedLinks.push(...meta.links);
53                 }
55                 setLinks(loadedLinks);
56             } catch (e) {
57                 setHasError(true);
58                 sendErrorReport(e);
59             }
60         });
62         return () => {
63             ac.abort();
64         };
65     }, []);
67     return {
68         isLoading,
69         hasError,
70         count: links.length,
71         size: links.reduce((sum, current) => sum + current.size, 0),
72     };