"use client"; import { useSyncExternalStore, useCallback } from "react"; import { createPortal } from "react-dom"; import { Icon } from "./Icon"; type ToastVariant = "success" | "error" | "info"; interface ToastItem { id: number; message: string; variant: ToastVariant; } // Module-level store — no Provider needed let toasts: ToastItem[] = []; let nextId = 0; const listeners = new Set<() => void>(); function notify() { listeners.forEach((l) => l()); } function addToast(message: string, variant: ToastVariant = "success") { const id = nextId++; toasts = [...toasts, { id, message, variant }]; notify(); setTimeout(() => { removeToast(id); }, 3000); } function removeToast(id: number) { toasts = toasts.filter((t) => t.id !== id); notify(); } function subscribe(listener: () => void) { listeners.add(listener); return () => listeners.delete(listener); } function getSnapshot() { return toasts; } const EMPTY_TOASTS: ToastItem[] = []; function getServerSnapshot(): ToastItem[] { return EMPTY_TOASTS; } export function toast(message: string, variant?: ToastVariant) { addToast(message, variant); } export function Toaster() { const items = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); if (items.length === 0) return null; if (typeof document === "undefined") return null; return createPortal(