'use client'
import * as React from "react"
import { X } from "lucide-react"
import { cn } from "@/lib/utils"
export interface ToastProps {
id: string
message: string
type?: 'success' | 'error' | 'info' | 'warning'
duration?: number
onClose: (id: string) => void
}
export function Toast({ id, message, type = 'info', duration = 3000, onClose }: ToastProps) {
React.useEffect(() => {
const timer = setTimeout(() => {
onClose(id)
}, duration)
return () => clearTimeout(timer)
}, [id, duration, onClose])
const bgColors = {
success: 'bg-green-600',
error: 'bg-red-600',
info: 'bg-blue-600',
warning: 'bg-yellow-600'
}
return (
{message}
onClose(id)}
className="rounded-full p-1 hover:bg-white/20 transition-colors"
>
)
}
export interface ToastContextType {
addToast: (message: string, type?: 'success' | 'error' | 'info' | 'warning') => void
}
const ToastContext = React.createContext(null)
export function ToastProvider({ children }: { children: React.ReactNode }) {
const [toasts, setToasts] = React.useState>>([])
const addToast = React.useCallback((message: string, type: 'success' | 'error' | 'info' | 'warning' = 'info') => {
const id = Math.random().toString(36).substring(7)
setToasts(prev => [...prev, { id, message, type }])
}, [])
const removeToast = React.useCallback((id: string) => {
setToasts(prev => prev.filter(toast => toast.id !== id))
}, [])
return (
{children}
{toasts.map(toast => (
))}
)
}
export function useToast() {
const context = React.useContext(ToastContext)
if (!context) {
throw new Error('useToast must be used within ToastProvider')
}
return context
}