| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import { toast } from "sonner";
- import { ApiClientError } from "@/lib/frontend/apiClient";
- function normalizeText(value) {
- if (typeof value !== "string") return null;
- const s = value.trim();
- return s ? s : null;
- }
- function buildToastOptions({ description, ...rest }) {
- const opts = { ...rest };
- const desc = normalizeText(description);
- if (desc) opts.description = desc;
- return opts;
- }
- export function notifySuccess({ title, description, ...options } = {}) {
- const t = normalizeText(title);
- if (!t) return null;
- return toast.success(t, buildToastOptions({ description, ...options }));
- }
- export function notifyError({ title, description, ...options } = {}) {
- const t = normalizeText(title);
- if (!t) return null;
- return toast.error(t, buildToastOptions({ description, ...options }));
- }
- export function notifyInfo({ title, description, ...options } = {}) {
- const t = normalizeText(title);
- if (!t) return null;
- return toast.info(t, buildToastOptions({ description, ...options }));
- }
- export function notifyWarning({ title, description, ...options } = {}) {
- const t = normalizeText(title);
- if (!t) return null;
- return toast.warning(t, buildToastOptions({ description, ...options }));
- }
- export function notifyLoading({ title, description, ...options } = {}) {
- const t = normalizeText(title);
- if (!t) return null;
- return toast.loading(t, buildToastOptions({ description, ...options }));
- }
- export function dismissToast(toastId) {
- return toast.dismiss(toastId);
- }
- export function mapApiErrorToToast(
- err,
- {
- overrides = null,
- fallbackTitle = "Fehler",
- fallbackDescription = "Bitte versuchen Sie es erneut.",
- } = {},
- ) {
- // Allow call sites (like Change Password) to override messages per error code.
- if (err instanceof ApiClientError) {
- const code = String(err.code || "");
- if (overrides && overrides[code]) {
- const o = overrides[code] || {};
- return {
- title: normalizeText(o.title) || fallbackTitle,
- description: normalizeText(o.description) || null,
- };
- }
- if (code === "CLIENT_NETWORK_ERROR") {
- return {
- title: "Netzwerkfehler",
- description:
- "Bitte prüfen Sie Ihre Verbindung und versuchen Sie es erneut.",
- };
- }
- if (code === "AUTH_UNAUTHENTICATED") {
- return {
- title: "Sitzung abgelaufen",
- description: "Bitte melden Sie sich erneut an.",
- };
- }
- if (code.startsWith("VALIDATION_")) {
- return {
- title: "Ungültige Eingabe",
- description:
- "Bitte prüfen Sie Ihre Eingaben und versuchen Sie es erneut.",
- };
- }
- }
- return {
- title: normalizeText(fallbackTitle) || "Fehler",
- description: normalizeText(fallbackDescription) || null,
- };
- }
- export function notifyApiError(
- err,
- {
- overrides = null,
- fallbackTitle = "Fehler",
- fallbackDescription = "Bitte versuchen Sie es erneut.",
- ...toastOptions
- } = {},
- ) {
- const mapped = mapApiErrorToToast(err, {
- overrides,
- fallbackTitle,
- fallbackDescription,
- });
- return toast.error(
- mapped.title,
- buildToastOptions({ description: mapped.description, ...toastOptions }),
- );
- }
|