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 50 51 52 53 54 | 4x 58x 5x 58x 58x 6x | import { useQuery } from '@tanstack/react-query';
import type { NotificationCounts } from '../services/notification.service';
import { fetchNotificationCounts } from '../services/notification.service';
import {
DEFAULT_NOTIFICATION_COUNTS,
NOTIFICATION_SETTINGS,
QUERY_KEYS,
} from '@/constants/notification';
interface UseNotificationCountsReturn {
counts: NotificationCounts;
loading: boolean;
error: string | null;
refetch: () => void;
}
export const useNotificationCounts = (
autoRefresh = NOTIFICATION_SETTINGS.DEFAULT_AUTO_REFRESH,
refreshInterval = NOTIFICATION_SETTINGS.DEFAULT_REFRESH_INTERVAL
): UseNotificationCountsReturn => {
const {
data: counts,
isLoading: loading,
error,
refetch,
} = useQuery({
queryKey: [QUERY_KEYS.NOTIFICATION_COUNTS],
queryFn: fetchNotificationCounts,
refetchInterval: autoRefresh ? refreshInterval : false,
refetchIntervalInBackground: true,
staleTime: NOTIFICATION_SETTINGS.STALE_TIME,
retryDelay: (attemptIndex: number) =>
Math.min(
NOTIFICATION_SETTINGS.RETRY_BASE_DELAY * 2 ** attemptIndex,
NOTIFICATION_SETTINGS.MAX_RETRY_DELAY
),
});
// Provide default values if data is undefined
const defaultCounts: NotificationCounts = {
...DEFAULT_NOTIFICATION_COUNTS,
lastUpdated: new Date().toISOString(),
};
return {
counts: counts || defaultCounts,
loading,
error: error ? error.message || String(error) : null,
refetch: () => {
refetch();
},
};
};
|