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';
13 temporaryEvent: CalendarViewEventTemporaryEvent | undefined;
14 dateRange: [Date, Date];
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) {
28 busySlotsActions.setMetadata({
29 viewStartDate: getUnixTime(dateRange[0]),
30 viewEndDate: getUnixTime(dateRange[1]),
40 }, [view, dateRange[0], dateRange[1], tzid]);
43 if (!isBusySlotsAvailable) {
47 const attendees = temporaryEvent?.tmpData?.attendees || [];
48 if (attendees.length > 0) {
49 if (preventFetchRef.current) {
50 preventFetchRef.current = false;
54 if (!store.getState()[busySlotsSliceName].metadata && view !== VIEWS.MONTH) {
57 dispatch(busySlotsActions.setAttendees(attendees.map((attendee) => attendee.email)));
60 if (!temporaryEvent) {
61 dispatch(busySlotsActions.reset());
62 preventFetchRef.current = false;
64 }, [temporaryEvent?.tmpData.attendees.join(',')]);
66 return preventFetchRef;
69 export default useBusySlots;