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';
17 calendars: VisualCalendar[];
19 onChangeDate: (newDate: Date) => void;
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({
35 const handleLinkError = () => {
38 // translator: event here is for calendar event
39 text: c('Error').t`Event not found`,
43 const handleEvents = useCallback(
44 (event: MessageEvent) => {
45 if (!getIsDrawerPostMessage(event)) {
49 switch (event.data.type) {
50 case DRAWER_EVENTS.CALENDAR_OPEN_EVENT:
52 const { calendarID, eventID, recurrenceID } = event.data.payload;
59 recurrenceId: recurrenceID ? recurrenceID.toString() : null,
60 onGoToEvent: goToEvent,
61 onGoToOccurrence: goToOccurrence,
62 onEventNotFoundError: handleLinkError,
66 case DRAWER_EVENTS.CALL_CALENDAR_EVENT_MANAGER:
68 void call([event.data.payload.calendarID]);
71 case DRAWER_EVENTS.SHOW:
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);
82 [calendars, addresses, goToEvent, goToOccurrence]
86 window.addEventListener('message', handleEvents);
89 window.removeEventListener('message', handleEvents);