"use client"; import { useEffect, useState } from "react"; import type { ComponentPropsWithoutRef, ReactElement } from "react"; import { X } from "lucide-react"; import { IconButton } from "@/components/ui/button"; import { cn } from "@/lib/utils"; type ToastTone = "success" | "error" | "info"; type ToastItem = { id: number; message: string; tone: ToastTone; }; type ToastPayload = { message: string; tone?: ToastTone; }; const toastEventName = "neta:toast"; let toastId = 0; export function showToast(payload: ToastPayload) { if (typeof window === "undefined") { return; } window.dispatchEvent(new CustomEvent(toastEventName, { detail: payload })); } export function Toaster() { const [items, setItems] = useState([]); useEffect(() => { function handleToast(event: Event) { const customEvent = event as CustomEvent; const item = { id: ++toastId, message: customEvent.detail.message, tone: customEvent.detail.tone ?? "info", }; setItems((current) => [...current.slice(-2), item]); window.setTimeout(() => { setItems((current) => current.filter((toast) => toast.id !== item.id)); }, 4500); } window.addEventListener(toastEventName, handleToast); return () => window.removeEventListener(toastEventName, handleToast); }, []); return (
{items.map((item) => (

{item.message}

setItems((current) => current.filter((toast) => toast.id !== item.id))} >
))}
); } export type ToastProps = ComponentPropsWithoutRef<"div"> & { open?: boolean; onOpenChange?: (open: boolean) => void; variant?: "default" | "destructive"; }; export type ToastActionElement = ReactElement; export function ToastProvider({ children }: { children: React.ReactNode }) { return <>{children}; } export function Toast({ open = true, onOpenChange, variant, className, ...props }: ToastProps) { if (!open) { return null; } return (
); } export function ToastTitle({ className, ...props }: ComponentPropsWithoutRef<"div">) { return
; } export function ToastDescription({ className, ...props }: ComponentPropsWithoutRef<"div">) { return
; } export function ToastClose({ className, ...props }: ComponentPropsWithoutRef<"button">) { return ( ); } export function ToastViewport({ className, ...props }: ComponentPropsWithoutRef<"div">) { return (
); }