2025-12-08 16:31:30 +03:00

71 lines
2.1 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
import { CheckCircle, XCircle, Info, X } from 'lucide-react'
type ToastType = 'success' | 'error' | 'info'
interface ToastProps {
message: string
type?: ToastType
duration?: number
onClose: () => void
}
export default function Toast({ message, type = 'info', duration = 3000, onClose }: ToastProps) {
const [isVisible, setIsVisible] = useState(true)
useEffect(() => {
const timer = setTimeout(() => {
setIsVisible(false)
setTimeout(onClose, 300)
}, duration)
return () => clearTimeout(timer)
}, [duration, onClose])
const icons = {
success: <CheckCircle className="text-green-500" size={20} />,
error: <XCircle className="text-red-500" size={20} />,
info: <Info className="text-blue-500" size={20} />
}
return (
<div
className={`fixed bottom-4 right-4 z-50 transition-all duration-300 ${
isVisible ? 'translate-y-0 opacity-100' : 'translate-y-2 opacity-0'
}`}
>
<div className="bg-celo-dark border border-white/20 rounded-xl shadow-xl p-4 flex items-center gap-3 max-w-sm">
{icons[type]}
<p className="flex-1 text-white text-sm">{message}</p>
<button
onClick={() => {
setIsVisible(false)
setTimeout(onClose, 300)
}}
className="p-1 hover:bg-white/10 rounded transition"
aria-label="Close"
>
<X size={16} className="text-white" />
</button>
</div>
</div>
)
}
export function useToast() {
const [toasts, setToasts] = useState<Array<{ id: string; message: string; type: ToastType }>>([])
const showToast = (message: string, type: ToastType = 'info') => {
const id = Math.random().toString(36).substr(2, 9)
setToasts(prev => [...prev, { id, message, type }])
}
const removeToast = (id: string) => {
setToasts(prev => prev.filter(t => t.id !== id))
}
return { toasts, showToast, removeToast, success: (msg: string) => showToast(msg, 'success'), error: (msg: string) => showToast(msg, 'error') }
}