"use client"; import { createContext, useCallback, useContext, useState, type ReactNode } from "react"; type ToastVariant = "success" | "error" | "info"; type ToastItem = { id: number; message: string; variant: ToastVariant }; type ToastContextValue = { showToast: (message: string, variant?: ToastVariant) => void }; const ToastContext = createContext(null); const VARIANT_STYLES: Record = { success: "bg-success-text", error: "bg-danger-solid", info: "bg-info-text", }; export function ToastProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]); const showToast = useCallback((message: string, variant: ToastVariant = "success") => { const id = Date.now() + Math.random(); setToasts((prev) => [...prev, { id, message, variant }]); setTimeout(() => setToasts((prev) => prev.filter((t) => t.id !== id)), 4000); }, []); return ( {children}
{toasts.map((t) => (
{t.message}
))}
); } export function useToast(): ToastContextValue { const ctx = useContext(ToastContext); if (!ctx) throw new Error("useToast must be used within a ToastProvider"); return ctx; }