'use client' import * as React from 'react' import { X } from 'lucide-react' import { cn } from '@/lib/utils' interface Toast { id: string title?: string description?: string variant?: 'default' | 'destructive' } interface ToastContext { toasts: Toast[] toast: (props: Omit) => void dismiss: (id: string) => void } const ToastContext = React.createContext(undefined) export function ToastProvider({ children }: { children: React.ReactNode }) { const [toasts, setToasts] = React.useState([]) const toast = React.useCallback((props: Omit) => { const id = Math.random().toString(36).slice(2) setToasts((prev) => [...prev, { ...props, id }]) setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)) }, 5000) }, []) const dismiss = React.useCallback((id: string) => { setToasts((prev) => prev.filter((t) => t.id !== id)) }, []) return ( {children} ) } function ToastViewport({ toasts, onDismiss }: { toasts: Toast[]; onDismiss: (id: string) => void }) { return (
{toasts.map((t) => ( ))}
) } function ToastItem({ toast, onDismiss }: { toast: Toast; onDismiss: (id: string) => void }) { return (
{toast.title &&
{toast.title}
} {toast.description &&
{toast.description}
}
) } export function useToast() { const context = React.useContext(ToastContext) if (!context) { throw new Error('useToast must be used within a ToastProvider') } return context }