import { toast } from "sonner"; /** Fire-and-forget report of a user-visible error to the per-user error log. * Listeners (the notification bell) are only notified once the entry is * durably stored, so the badge never counts entries the user cannot see. */ const listeners = new Set<() => void>(); export function subscribeErrorLogged(listener: () => void): () => void { listeners.add(listener); return () => { listeners.delete(listener); }; } export function reportError( source: string, message: string, details?: string ): void { void fetch("/api/error-logs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ source, message, ...(details ? { details } : {}), }), }) .then((response) => { if (!response.ok) return; for (const listener of listeners) listener(); }) .catch(() => {}); } /** toast.error + error-log report. Use for operational failures; pure form * validation hints should keep using toast.error directly. */ export function errorToast( source: string, message: string, details?: string ): void { toast.error(message); reportError(source, message, details); }