"use client"; import { SuccessTickIcon } from "@/app/utils/svgs/cart/successTickIcon"; import { useEffect, useMemo, useRef } from "react"; import type { ReactNode } from "react"; export type ToastType = "success" | "error" | "info"; interface ToastProps { message: string; subParagraph?: string; type?: ToastType; duration?: number; // ms onClose?: () => void; } const typeStyles: Record = { success: { bg: "bg-white", ring: "ring-1 ring-green-200", icon: SuccessTickIcon, bar: "bg-green-500", }, error: { bg: "bg-white", ring: "ring-1 ring-red-200", icon: ( ), bar: "bg-red-500", }, info: { bg: "bg-white", ring: "ring-1 ring-blue-200", icon: ( ), bar: "bg-blue-500", }, }; export default function Toast({ message, subParagraph, type = "info", duration = 3000, onClose }: ToastProps) { const timerRef = useRef(null); const style = useMemo(() => typeStyles[type], [type]); useEffect(() => { if (duration <= 0) return; timerRef.current = setTimeout(() => onClose?.(), duration); return () => { if (timerRef.current) clearTimeout(timerRef.current); }; }, [duration, onClose]); return (
{style.icon}

{message}

{ subParagraph && (

{subParagraph}

) }
{onClose && ( )}
{duration > 0 && ( )}
); }