Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / hooks / useOpenEventsFromMail.tsx
blob1659583571eda48f1b2ad0aca2e18e21bacca33f
1 import type { Dispatch, SetStateAction } from 'react';
2 import { useCallback, useEffect } from 'react';
4 import { c } from 'ttag';
6 import { useCalendarModelEventManager, useNotifications } from '@proton/components';
7 import { getIsDrawerPostMessage } from '@proton/shared/lib/drawer/helpers';
8 import { DRAWER_EVENTS } from '@proton/shared/lib/drawer/interfaces';
9 import type { Address } from '@proton/shared/lib/interfaces';
10 import type { VisualCalendar } from '@proton/shared/lib/interfaces/calendar';
12 import type { EventTargetAction } from '../containers/calendar/interface';
13 import useOpenCalendarEvents from './useOpenCalendarEvents';
14 import { useOpenEvent } from './useOpenEvent';
16 interface Props {
17     calendars: VisualCalendar[];
18     addresses: Address[];
19     onChangeDate: (newDate: Date) => void;
20     tzid: string;
21     setEventTargetAction: Dispatch<SetStateAction<EventTargetAction | undefined>>;
24 export const useOpenEventsFromMail = ({ calendars, addresses, onChangeDate, tzid, setEventTargetAction }: Props) => {
25     const { call } = useCalendarModelEventManager();
26     const { createNotification } = useNotifications();
27     const openEvent = useOpenEvent();
28     const { goToEvent, goToOccurrence } = useOpenCalendarEvents({
29         onChangeDate,
30         tzid,
31         setEventTargetAction,
32         preventPopover: true,
33     });
35     const handleLinkError = () => {
36         createNotification({
37             type: 'error',
38             // translator: event here is for calendar event
39             text: c('Error').t`Event not found`,
40         });
41     };
43     const handleEvents = useCallback(
44         (event: MessageEvent) => {
45             if (!getIsDrawerPostMessage(event)) {
46                 return;
47             }
49             switch (event.data.type) {
50                 case DRAWER_EVENTS.CALENDAR_OPEN_EVENT:
51                     {
52                         const { calendarID, eventID, recurrenceID } = event.data.payload;
54                         void openEvent({
55                             calendars,
56                             addresses,
57                             calendarID,
58                             eventID,
59                             recurrenceId: recurrenceID ? recurrenceID.toString() : null,
60                             onGoToEvent: goToEvent,
61                             onGoToOccurrence: goToOccurrence,
62                             onEventNotFoundError: handleLinkError,
63                         });
64                     }
65                     break;
66                 case DRAWER_EVENTS.CALL_CALENDAR_EVENT_MANAGER:
67                     {
68                         void call([event.data.payload.calendarID]);
69                     }
70                     break;
71                 case DRAWER_EVENTS.SHOW:
72                     {
73                         // When showing again the cached calendar app, we need to call the event manager for all calendars to get all updates
74                         const allCalendarIDs = calendars.map(({ ID }) => ID);
75                         void call(allCalendarIDs);
76                     }
77                     break;
78                 default:
79                     break;
80             }
81         },
82         [calendars, addresses, goToEvent, goToOccurrence]
83     );
85     useEffect(() => {
86         window.addEventListener('message', handleEvents);
88         return () => {
89             window.removeEventListener('message', handleEvents);
90         };
91     }, [handleEvents]);