All files / src/presentation/stores toast.store.ts

90% Statements 9/10
100% Branches 1/1
85.71% Functions 6/7
83.33% Lines 5/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                    5x       7x 7x     7x           3x      
import { create } from "zustand";
import { generateToastId } from "./toast-id-generator";
 
export type ToastType = "success" | "error" | "info";
 
export interface Toast {
  id: string;
  type: ToastType;
  message: string;
  duration?: number; // en ms, optionnel
}
 
interface ToastStore {
  toasts: Toast[];
  addToast: (toast: Omit<Toast, "id">) => void;
  removeToast: (id: string) => void;
}
 
export const useToastStore = create<ToastStore>((set, get) => ({
  toasts: [],
 
  addToast: ({ type, message, duration = 2000 }) => {
    const id = generateToastId();
    set((state) => ({ toasts: [...state.toasts, { id, type, message, duration }] }));
 
    // auto-remove after given duration
    setTimeout(() => {
      get().removeToast(id);
    }, duration);
  },
 
  removeToast: (id) => {
    set((state) => ({ toasts: state.toasts.filter((t) => t.id !== id) }));
  },
}));