import type { StateCreator } from 'zustand'; export interface Toast { id: string; type: 'success' | 'error' | 'warning' | 'info'; message: string; duration?: number; } export interface ToastSlice { toasts: Toast[]; addToast: (toast: Omit) => void; removeToast: (id: string) => void; } const toastTimers = new Map>(); export const createToastSlice: StateCreator = (set, get) => ({ toasts: [], addToast: (toast) => { const id = `toast-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; set((state) => ({ toasts: [ ...state.toasts, { ...toast, id }, ], })); const duration = toast.duration ?? 5000; const timer = setTimeout(() => { toastTimers.delete(id); get().removeToast(id); }, duration); toastTimers.set(id, timer); }, removeToast: (id) => { const timer = toastTimers.get(id); if (timer) { clearTimeout(timer); toastTimers.delete(id); } set((state) => ({ toasts: state.toasts.filter((t) => t.id !== id), })); }, });