1 import { useCallback, useEffect } from 'react';
3 import type { Action, ThunkDispatch } from '@reduxjs/toolkit';
5 import { baseUseDispatch, baseUseSelector, baseUseStore } from '@proton/react-redux-store';
6 import type { ProtonThunkArguments } from '@proton/redux-shared-store-types';
7 import type { CalendarBootstrap } from '@proton/shared/lib/interfaces/calendar';
8 import noop from '@proton/utils/noop';
10 import { type CalendarsBootstrapState, calendarBootstrapThunk, selectCalendarsBootstrap } from './index';
12 export const useGetCalendarBootstrap = () => {
13 const dispatch = baseUseDispatch<ThunkDispatch<CalendarsBootstrapState, ProtonThunkArguments, Action>>();
14 return useCallback((calendarID: string) => dispatch(calendarBootstrapThunk({ calendarID })), [dispatch]);
17 export const useCalendarBootstrap = (calendarID: string | undefined): [CalendarBootstrap | undefined, boolean] => {
18 const state = baseUseSelector(selectCalendarsBootstrap);
19 const getCalendarBoostrap = useGetCalendarBootstrap();
25 getCalendarBoostrap(calendarID).catch(noop);
29 return [undefined, false];
32 const value = state[calendarID]?.value;
33 return [value, value === undefined];
36 export const useReadCalendarBootstrap = () => {
37 const store = baseUseStore();
39 (calendarID: string) => {
40 const state = selectCalendarsBootstrap(store.getState());
41 return state[calendarID]?.value;