Files
smart-go/web/stores/toast-store.ts
T
2026-04-23 18:58:13 +08:00

32 lines
886 B
TypeScript

'use client';
import { create } from 'zustand';
export type ToastItem = { id: string; message: string; variant: 'error' | 'info' };
type ToastState = {
toasts: ToastItem[];
show: (message: string, variant?: ToastItem['variant']) => void;
dismiss: (id: string) => void;
};
export const useToastStore = create<ToastState>((set, get) => ({
toasts: [],
show: (message, variant = 'info') => {
const id =
typeof crypto !== 'undefined' && 'randomUUID' in crypto
? crypto.randomUUID()
: String(Date.now());
set((s) => ({ toasts: [...s.toasts, { id, message, variant }] }));
const duration = variant === 'error' ? 5200 : 3600;
if (typeof window !== 'undefined') {
window.setTimeout(() => {
get().dismiss(id);
}, duration);
}
},
dismiss: (id) => set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) })),
}));