Benachrichtigungen: Antworten auf eigene Beiträge
Bisher erfuhr man nur durch Nachsehen, ob jemand geantwortet hat. Jetzt legt jede Antwort eine Benachrichtigung für den Autor des Elternbeitrags an (Zeit, Typ, Text mit Auszug, interner Link, gesehen-Flag). Die Glocke in der Navigation zählt die ungesehenen; die Liste markiert beim Ansehen alles als gesehen. Der Auszug zitiert die Antwort, deshalb hängt die Benachrichtigung an deren pid: Bearbeiten zieht den Text nach, Löschen (auch des Kontos) nimmt sie mit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
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";
|
||||
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,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user