Files
openpylon/src/stores/toast-store.ts

34 lines
751 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;
}
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) }));
},
}));