32 lines
886 B
TypeScript
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) })),
|
|
}));
|