refactor [2]

This commit is contained in:
Egor Pozharov
2026-04-14 17:11:00 +06:00
parent 9b90a8aa7f
commit cb3f91c17f

View File

@@ -0,0 +1,35 @@
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),
})),
}));