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';
10 * useLinksDetailsView loads links if not cached yet and provides some
11 * aggregated information such as their count or size.
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();
21 const ac = new AbortController();
23 const linksByShareId: Record<string, string[]> = {};
25 selectedLinks.forEach(({ rootShareId, linkId }) => {
26 if (!linksByShareId[rootShareId]) {
27 linksByShareId[rootShareId] = [];
30 linksByShareId[rootShareId].push(linkId);
33 void withLoading(async () => {
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) {
43 console.error(new Error('Failed to load links meta in details modal'), {
45 linkIds: linkIds.filter((id) => !meta.links.find((link) => link.linkId === id)),
52 loadedLinks.push(...meta.links);
55 setLinks(loadedLinks);
71 size: links.reduce((sum, current) => sum + current.size, 0),