feat: add toast notification system with success, error, and info variants
This commit is contained in:
33
src/stores/toast-store.ts
Normal file
33
src/stores/toast-store.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
}
|
||||
|
||||
let nextId = 0;
|
||||
|
||||
export const useToastStore = create<ToastState>((set) => ({
|
||||
toasts: [],
|
||||
|
||||
addToast: (message, type = "info") => {
|
||||
const id = String(++nextId);
|
||||
set((s) => ({ toasts: [...s.toasts, { id, message, type }] }));
|
||||
setTimeout(() => {
|
||||
set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) }));
|
||||
}, 3000);
|
||||
},
|
||||
|
||||
removeToast: (id) => {
|
||||
set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) }));
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user