Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / applications / calendar / src / app / hooks / useBusySlots.ts
blobed73f69d52fb61d0c0b3ce3ee50aba0cdb56a57b
1 import { useEffect, useRef } from 'react';
3 import { getUnixTime } from 'date-fns';
5 import { useBusySlotsAvailable } from '@proton/components';
6 import { VIEWS } from '@proton/shared/lib/calendar/constants';
8 import type { CalendarViewEventTemporaryEvent } from '../containers/calendar/interface';
9 import { busySlotsActions, busySlotsSliceName } from '../store/busySlots/busySlotsSlice';
10 import { useCalendarDispatch, useCalendarStore } from '../store/hooks';
12 interface Props {
13     temporaryEvent: CalendarViewEventTemporaryEvent | undefined;
14     dateRange: [Date, Date];
15     tzid: string;
16     view: VIEWS;
19 const useBusySlots = ({ temporaryEvent, dateRange, tzid, view }: Props) => {
20     const preventFetchRef = useRef(false);
21     const isBusySlotsAvailable = useBusySlotsAvailable();
22     const store = useCalendarStore();
23     const dispatch = useCalendarDispatch();
25     const updateMetadata = () => {
26         if (isBusySlotsAvailable) {
27             dispatch(
28                 busySlotsActions.setMetadata({
29                     viewStartDate: getUnixTime(dateRange[0]),
30                     viewEndDate: getUnixTime(dateRange[1]),
31                     tzid,
32                     view,
33                 })
34             );
35         }
36     };
38     useEffect(() => {
39         updateMetadata();
40     }, [view, dateRange[0], dateRange[1], tzid]);
42     useEffect(() => {
43         if (!isBusySlotsAvailable) {
44             return;
45         }
47         const attendees = temporaryEvent?.tmpData?.attendees || [];
48         if (attendees.length > 0) {
49             if (preventFetchRef.current) {
50                 preventFetchRef.current = false;
51                 return;
52             }
54             if (!store.getState()[busySlotsSliceName].metadata && view !== VIEWS.MONTH) {
55                 updateMetadata();
56             }
57             dispatch(busySlotsActions.setAttendees(attendees.map((attendee) => attendee.email)));
58         }
60         if (!temporaryEvent) {
61             dispatch(busySlotsActions.reset());
62             preventFetchRef.current = false;
63         }
64     }, [temporaryEvent?.tmpData.attendees.join(',')]);
66     return preventFetchRef;
69 export default useBusySlots;