1 import type { Dispatch, SetStateAction } from 'react';
2 import { useCallback } from 'react';
4 import { getDateOrDateTimeProperty, propertyToUTCDate } from '@proton/shared/lib/calendar/vcalConverter';
5 import { startOfDay } from '@proton/shared/lib/date-fns-utc';
7 convertUTCDateTimeToZone,
9 fromUTCDateToLocalFakeUTCDate,
11 } from '@proton/shared/lib/date/timezone';
12 import type { CalendarEventSharedData, VcalVeventComponent } from '@proton/shared/lib/interfaces/calendar';
14 import { getViewEventDateProperties } from '../containers/calendar/eventHelper';
15 import type { EventTargetAction } from '../containers/calendar/interface';
16 import { generateEventUniqueId } from '../helpers/event';
19 onChangeDate: (newDate: Date) => void;
21 setEventTargetAction: Dispatch<SetStateAction<EventTargetAction | undefined>>;
22 preventPopover?: boolean;
25 export const useOpenCalendarEvents = ({ onChangeDate, tzid, setEventTargetAction, preventPopover }: Props) => {
26 const navigateToEvent = useCallback(
27 (utcDate: Date, isAllDay: boolean) => {
28 const fakeUTCDate = fromUTCDateToLocalFakeUTCDate(utcDate, isAllDay, tzid);
30 onChangeDate(startOfDay(fakeUTCDate));
32 [tzid, setEventTargetAction]
35 const goToEvent = useCallback(
36 (eventData: CalendarEventSharedData, eventComponent: VcalVeventComponent) => {
37 const { utcStart, isAllDay, isAllPartDay } = getViewEventDateProperties(eventComponent);
38 const startInTzid = toUTCDate(convertUTCDateTimeToZone(fromUTCDate(utcStart), tzid));
40 navigateToEvent(utcStart, isAllDay);
41 setEventTargetAction({
42 uniqueId: `${generateEventUniqueId(eventData.CalendarID, eventData.ID)}`,
49 [setEventTargetAction, onChangeDate, tzid, navigateToEvent]
52 const goToOccurrence = useCallback(
54 eventData: CalendarEventSharedData,
55 eventComponent: VcalVeventComponent,
56 occurrence: { localStart: Date; occurrenceNumber: number }
58 const { isAllDay, isAllPartDay } = getViewEventDateProperties(eventComponent);
59 const withOccurrenceDtstart = getDateOrDateTimeProperty(eventComponent.dtstart, occurrence.localStart);
61 const utcDate = propertyToUTCDate(withOccurrenceDtstart);
62 const startInTzid = toUTCDate(convertUTCDateTimeToZone(fromUTCDate(utcDate), tzid));
64 navigateToEvent(utcDate, isAllDay);
65 setEventTargetAction({
66 uniqueId: `${generateEventUniqueId(eventData.CalendarID, eventData.ID)}-${occurrence.occurrenceNumber}`,
73 [setEventTargetAction, onChangeDate, navigateToEvent]
76 return { goToEvent, goToOccurrence };
79 export default useOpenCalendarEvents;