import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient, } from "@tanstack/react-query"; import { getNotifications, getUnreadCount, markNotificationsRead, markAllNotificationsRead, } from "../services/notification-service"; export const NOTIFICATION_KEY = ["notifications"]; export const UNREAD_COUNT_KEY = ["notifications", "unread-count"]; export function useNotificationsQuery(type?: string) { return useInfiniteQuery({ queryKey: [...NOTIFICATION_KEY, type], queryFn: ({ pageParam }) => getNotifications({ cursor: pageParam, type }), initialPageParam: undefined as string | undefined, getNextPageParam: (lastPage) => lastPage.meta.hasNextPage ? lastPage.meta.nextCursor : undefined, staleTime: 0, gcTime: 0, placeholderData: keepPreviousData, }); } export function useUnreadCountQuery() { return useQuery({ queryKey: UNREAD_COUNT_KEY, queryFn: getUnreadCount, }); } export function useMarkReadMutation() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (notificationIds: string[]) => markNotificationsRead(notificationIds), onSuccess: () => { queryClient.invalidateQueries({ queryKey: NOTIFICATION_KEY }); }, }); } export function useMarkAllReadMutation() { const queryClient = useQueryClient(); return useMutation({ mutationFn: markAllNotificationsRead, onSuccess: () => { queryClient.invalidateQueries({ queryKey: NOTIFICATION_KEY }); }, }); }