aboutsummaryrefslogtreecommitdiff
path: root/src/stores/useToastStore.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/useToastStore.ts')
-rw-r--r--src/stores/useToastStore.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/stores/useToastStore.ts b/src/stores/useToastStore.ts
new file mode 100644
index 0000000..ab45325
--- /dev/null
+++ b/src/stores/useToastStore.ts
@@ -0,0 +1,31 @@
+import { create } from 'zustand'
+
+export type ToastType = 'success' | 'error' | 'info' | 'warning'
+
+export interface Toast {
+ id: string
+ message: string
+ type: ToastType
+}
+
+interface ToastState {
+ toasts: Toast[]
+ addToast: (message: string, type?: ToastType, duration?: number) => void
+ removeToast: (id: string) => void
+}
+
+let counter = 0
+
+export const useToastStore = create<ToastState>((set, get) => ({
+ toasts: [],
+ addToast: (message, type = 'info', duration = 3500) => {
+ const id = `toast-${++counter}`
+ set((s) => ({ toasts: [...s.toasts, { id, message, type }] }))
+ setTimeout(() => {
+ get().removeToast(id)
+ }, duration)
+ },
+ removeToast: (id) => {
+ set((s) => ({ toasts: s.toasts.filter((t) => t.id !== id) }))
+ },
+}))