1 import { useEffect, useMemo } from 'react';
3 import useLoading from '@proton/hooks/useLoading';
5 import type { SharedWithMeItem } from '../../components/sections/SharedWithMe/SharedWithMe';
6 import { sendErrorReport } from '../../utils/errorHandling';
7 import { useDriveEventManager } from '../_events';
8 import { useInvitationsListing } from '../_invitations/useInvitationsListing';
10 export const useInvitationsView = () => {
11 const [isLoading, withLoading] = useLoading(true);
12 const { getCachedInvitations, loadInvitations } = useInvitationsListing();
13 const driveEventManager = useDriveEventManager();
15 const cachedInvitations = getCachedInvitations();
17 const invitationsBrowserItems: SharedWithMeItem[] = useMemo(
19 cachedInvitations.reduce<SharedWithMeItem[]>((acc, item) => {
21 isFile: item.link.isFile,
23 mimeType: item.link.mimeType,
24 rootShareId: item.share.shareId,
25 id: item.share.shareId,
26 // DecrypptedLinkName will always be defined as it's retrieve in the listing
27 name: item.decryptedLinkName || '',
28 invitationDetails: item,
29 sharedBy: item.invitation.inviterEmail,
32 isLocked: item.isLocked,
33 linkId: item.link.linkId,
35 volumeId: item.share.volumeId,
43 const abortController = new AbortController();
44 const unsubscribe = driveEventManager.eventHandlers.subscribeToCore((event) => {
45 if (event.DriveShareRefresh?.Action === 2) {
46 loadInvitations(abortController.signal).catch(sendErrorReport);
51 abortController.abort();
53 }, [driveEventManager.eventHandlers.subscribeToCore]);
56 const abortController = new AbortController();
57 void withLoading(loadInvitations(abortController.signal).catch(sendErrorReport));
60 abortController.abort();
64 return { invitations: cachedInvitations, invitationsBrowserItems, isLoading };