36 lines
837 B
TypeScript
36 lines
837 B
TypeScript
import { create } from 'zustand';
|
|
|
|
export type ToastType = 'success' | 'error' | 'info';
|
|
|
|
interface Toast {
|
|
id: string;
|
|
message: string;
|
|
type: ToastType;
|
|
}
|
|
|
|
interface ToastState {
|
|
toasts: Toast[];
|
|
addToast: (message: string, type: ToastType) => void;
|
|
removeToast: (id: string) => void;
|
|
}
|
|
|
|
export const useToastStore = create<ToastState>((set) => ({
|
|
toasts: [],
|
|
addToast: (message, type) => {
|
|
const id = Math.random().toString(36).substring(2, 9);
|
|
set((state) => ({
|
|
toasts: [...state.toasts, { id, message, type }],
|
|
}));
|
|
// Auto remove after 5 seconds
|
|
setTimeout(() => {
|
|
set((state) => ({
|
|
toasts: state.toasts.filter((t) => t.id !== id),
|
|
}));
|
|
}, 5000);
|
|
},
|
|
removeToast: (id) =>
|
|
set((state) => ({
|
|
toasts: state.toasts.filter((t) => t.id !== id),
|
|
})),
|
|
}));
|