import { toast } from "sonner"; import type { PlausibilityWarning } from "./types"; /** Warnings about food safety must not time out: they stay until dismissed. */ function mustStay(warnings: readonly PlausibilityWarning[]): boolean { return warnings.some( (warning) => warning.severity === "WARNUNG" || warning.severity === "BESTAETIGEN", ); } /** * After a successful save: " ยท 1 Warnung" plus one line per text. The * record is saved either way; warnings only point at something to check. * A WARNUNG or BESTAETIGEN stays until the person dismisses it. Does nothing * for an empty list - the caller shows its own success then. */ export function toastWarnings( warnings: readonly PlausibilityWarning[], savedLabel = "Gespeichert", ): void { if (warnings.length === 0) return; const count = warnings.length; const title = `${savedLabel} \u00b7 ${count} ${count === 1 ? "Warnung" : "Warnungen"}`; const description = warnings.map((warning) => warning.message).join("\n"); if (!mustStay(warnings)) { toast.warning(title, { description, classNames: { description: "whitespace-pre-line" }, }); return; } toast.warning(title, { description, duration: Infinity, closeButton: true, action: { label: "Verstanden", onClick: () => undefined }, classNames: { description: "whitespace-pre-line", actionButton: "min-h-11 min-w-11", closeButton: "size-11", }, }); }