import { useEffect } from 'react' ; import { usePrevious } from 'react-use' ; import { useSnapshot } from 'valtio' ; import { fetchEvents , maybeLoadCalendarAPI } from './gapi' ; import { CalendarState } from './types' ; param calendarState export function useFetchCalendarEvents ( calendarState ) { const { activeStartDate } = useSnapshot ( calendarState ) ; const prevActiveStartDate = usePrevious ( activeStartDate ) ; useEffect ( ( ) => { async function fetchCalendarEventsAsync ( ) { if ( prevActiveStartDate && activeStartDate . isSame ( prevActiveStartDate , 'month' ) ) { return ; } calendarState . events = [ ] ; await maybeLoadCalendarAPI ( process . env . GOOGLE_CALENDAR_API_KEY ?? '' ) ; calendarState . events = await fetchEvents ( process . env . GOOGLE_CALENDAR_ID ?? '' , activeStartDate ) ; } function fetchCalendarEvents ( ) { fetchCalendarEventsAsync ( ) ; } fetchCalendarEvents ( ) ; } , [ activeStartDate , calendarState , prevActiveStartDate ] ) ; }