import { useQuery, useQueryClient } from "@tanstack/react-query"; import { api } from "./api"; import { useMe } from "./auth"; // Benachrichtigungen (notes/api.md): der Zähler am Glocken-Icon fragt // angemeldet alle 30 Sekunden nach; die Liste markiert beim Abruf alles als // gesehen, danach steht der Zähler sofort auf 0. export type Notification = { id: number; kind: "reply" | "bump"; // Was passiert ist, etwa "@bob hat auf deinen Beitrag geantwortet." message: string; // Anfang des zitierten Beitrags, '' = keiner. excerpt: string; // Interner Pfad, etwa /e/123. link: string; created_at: number; // Stand vor dem Abruf: false = neu. seen: boolean; }; const pollUnread = 30_000; export function useUnreadNotifications() { const me = useMe().data; return ( useQuery({ queryKey: ["notifications", "unread"], queryFn: async () => (await api.get<{ unread: number }>("/notifications/unread")).unread, enabled: !!me, refetchInterval: pollUnread, }).data ?? 0 ); } export function useNotifications() { const me = useMe().data; const qc = useQueryClient(); return useQuery({ queryKey: ["notifications", "list"], queryFn: async () => { const list = (await api.get<{ notifications: Notification[] }>("/notifications")).notifications; qc.setQueryData(["notifications", "unread"], 0); return list; }, enabled: !!me, // Jeder Besuch der Seite gilt als Ansehen. staleTime: 0, }); }