refactor [2]
This commit is contained in:
35
frontend/src/stores/toastStore.ts
Normal file
35
frontend/src/stores/toastStore.ts
Normal 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),
|
||||||
|
})),
|
||||||
|
}));
|
||||||
Reference in New Issue
Block a user