Yaltopia-Tickets-App/lib/toast-store.ts

47 lines
1.3 KiB
TypeScript

import { create } from "zustand";
export type ToastType = "success" | "error" | "warning" | "info";
export interface ToastState {
visible: boolean;
type: ToastType;
title: string;
message: string;
duration?: number;
show: (params: {
type: ToastType;
title: string;
message: string;
duration?: number;
}) => void;
hide: () => void;
showToast: (message: string, type?: ToastType) => void;
}
export const useToast = create<ToastState>((set) => ({
visible: false,
type: "info",
title: "",
message: "",
duration: 4000,
show: ({ type, title, message, duration = 4000 }) => {
set({ visible: true, type, title, message, duration });
},
hide: () => set({ visible: false }),
showToast: (message, type = "info") => {
const title = type.charAt(0).toUpperCase() + type.slice(1);
set({ visible: true, type, title, message, duration: 4000 });
},
}));
export const toast = {
success: (title: string, message: string) =>
useToast.getState().show({ type: "success", title, message }),
error: (title: string, message: string) =>
useToast.getState().show({ type: "error", title, message }),
warning: (title: string, message: string) =>
useToast.getState().show({ type: "warning", title, message }),
info: (title: string, message: string) =>
useToast.getState().show({ type: "info", title, message }),
};