Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 1x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 22x 22x 22x 22x 22x 22x 22x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 22x 22x 22x 22x 22x 4x 4x 4x 22x 22x | import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { lunchMenusService } from '@/services/lunchMenus';
import { getAuthToken, seedDayMap, type DailyCommentCountsMap } from '@/utils';
import { SUMMARY_RECONCILE_MS } from '@/constants/date';
import { toast } from 'sonner';
const QUERY_KEYS = {
summaryByDay: (fromDate: string, toDate: string) =>
['lunchMenus', 'summaryByDay', fromDate, toDate] as const,
};
/** Hook to fetch day counts and manage the day map state for a date range. */
export const useDayCountsMap = (
fromDate: string,
toDate: string,
firebaseUser: any,
) => {
const { data: stats, isLoading } = useQuery({
queryKey: QUERY_KEYS.summaryByDay(fromDate, toDate),
queryFn: async () => {
const token = await getAuthToken();
const { data, error } = await lunchMenusService.summaryByDay(
fromDate,
toDate,
token,
);
if (error?.message) toast.error(error.message);
return data ?? [];
},
enabled: !!firebaseUser && !!fromDate && !!toDate,
refetchOnWindowFocus: true,
refetchInterval: SUMMARY_RECONCILE_MS,
});
const [map, setMap] = useState<DailyCommentCountsMap>({});
// Derived during render to avoid a stale-map flash
const [seededStats, setSeededStats] = useState(stats);
if (stats !== seededStats) {
setSeededStats(stats);
if (stats) setMap(seedDayMap(stats));
}
return { map, setMap, isLoading };
};
|