Bei Antworten steht statt "Jemand" der Handle. Damit er nach einer Umbenennung stimmt, speichert die Benachrichtigung nur die uid (actor) und den Auszug; den Text setzt der Abruf zusammen. Die Tabelle war noch nirgends ausgerollt, daher direkt in neuer Form statt per ALTER. Neu ist der Repost eines eigenen Beitrags (kind bump), in derselben Transaktion wie der Bump. Wer repostet, wird nicht gespeichert, die Meldung sagt daher "Jemand". Eigene Reposts lösen nichts aus. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
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";
|
|
message: 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,
|
|
});
|
|
}
|