Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / hooks / useOpenCalendarEvents.tsx
blobc02a894595d8c3db397e330e5576b2d27c091213
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';
6 import {
7     convertUTCDateTimeToZone,
8     fromUTCDate,
9     fromUTCDateToLocalFakeUTCDate,
10     toUTCDate,
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';
18 interface Props {
19     onChangeDate: (newDate: Date) => void;
20     tzid: string;
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));
31         },
32         [tzid, setEventTargetAction]
33     );
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)}`,
43                 isAllDay,
44                 isAllPartDay,
45                 startInTzid,
46                 preventPopover,
47             });
48         },
49         [setEventTargetAction, onChangeDate, tzid, navigateToEvent]
50     );
52     const goToOccurrence = useCallback(
53         (
54             eventData: CalendarEventSharedData,
55             eventComponent: VcalVeventComponent,
56             occurrence: { localStart: Date; occurrenceNumber: number }
57         ) => {
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}`,
67                 isAllDay,
68                 isAllPartDay,
69                 startInTzid,
70                 preventPopover,
71             });
72         },
73         [setEventTargetAction, onChangeDate, navigateToEvent]
74     );
76     return { goToEvent, goToOccurrence };
79 export default useOpenCalendarEvents;