Files
lcbp3/frontend/hooks/use-notification.ts
T
admin 11984bfa29
CI Pipeline / build (push) Failing after 12m41s
Build and Deploy / deploy (push) Failing after 2m44s
260322:1648 Correct Coresspondence / Doing RFA / Correct CI
2026-03-22 16:48:12 +07:00

32 lines
912 B
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { notificationService } from '@/lib/services/notification.service';
import { toast } from 'sonner';
export const notificationKeys = {
all: ['notifications'] as const,
unread: () => [...notificationKeys.all, 'unread'] as const,
};
export function useNotifications() {
return useQuery({
queryKey: notificationKeys.unread(),
queryFn: notificationService.getUnread,
refetchInterval: 60 * 1000, // Poll every 1 minute
staleTime: 30 * 1000,
});
}
export function useMarkNotificationRead() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: notificationService.markAsRead,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: notificationKeys.unread() });
},
onError: () => {
toast.error('Failed to mark notification as read');
},
});
}