mirror of
https://github.com/block/buzz.git
synced 2026-11-29 04:32:08 +00:00
Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import {
|
|
cancelReminder,
|
|
completeReminder,
|
|
createReminder,
|
|
fetchReminders,
|
|
snoozeReminder,
|
|
} from "@/features/reminders/lib/reminderService";
|
|
import { countDue } from "@/features/reminders/lib/reminderFilters";
|
|
import type {
|
|
Reminder,
|
|
ReminderTarget,
|
|
} from "@/features/reminders/lib/reminderTypes";
|
|
|
|
export const remindersQueryKey = (pubkey: string) =>
|
|
["reminders", pubkey] as const;
|
|
|
|
/** Re-exported so the inbox badge has one import for the due count. */
|
|
export const countDueReminders = countDue;
|
|
|
|
/**
|
|
* The single source of truth for a user's reminders. Badge, channel overlay,
|
|
* panel, and fire-on-due detection all read this one query, so invalidating it
|
|
* (see {@link useReminderMutations}) keeps every surface consistent.
|
|
*/
|
|
export function useRemindersQuery(pubkey: string | undefined) {
|
|
return useQuery({
|
|
enabled: Boolean(pubkey),
|
|
queryKey: remindersQueryKey(pubkey ?? ""),
|
|
queryFn: () => fetchReminders(pubkey ?? ""),
|
|
staleTime: 30_000,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Wraps every reminder write so the shared query is invalidated on success —
|
|
* the consistency spine the panel/badge/overlay all depend on. A mutation that
|
|
* skipped invalidation would leave those surfaces stale until the next refetch.
|
|
*/
|
|
export function useReminderMutations(pubkey: string) {
|
|
const queryClient = useQueryClient();
|
|
const invalidate = () =>
|
|
queryClient.invalidateQueries({ queryKey: remindersQueryKey(pubkey) });
|
|
|
|
const create = useMutation({
|
|
mutationFn: (input: {
|
|
target: ReminderTarget;
|
|
notBefore: number;
|
|
note?: string;
|
|
}) => createReminder(input.target, input.notBefore, input.note),
|
|
onSuccess: invalidate,
|
|
});
|
|
const complete = useMutation({
|
|
mutationFn: (reminder: Reminder) => completeReminder(pubkey, reminder),
|
|
onSuccess: invalidate,
|
|
});
|
|
const snooze = useMutation({
|
|
mutationFn: (input: { reminder: Reminder; notBefore: number }) =>
|
|
snoozeReminder(pubkey, input.reminder, input.notBefore),
|
|
onSuccess: invalidate,
|
|
});
|
|
const cancel = useMutation({
|
|
mutationFn: (reminder: Reminder) => cancelReminder(pubkey, reminder),
|
|
onSuccess: invalidate,
|
|
});
|
|
|
|
return { create, complete, snooze, cancel };
|
|
}
|