Files
Todo/components/Toast.tsx
Paul Bailey 72f98c783a Fix toast auto-dismiss and add debugging
- Added console logging to debug toast lifecycle
- Improved toast ID generation with timestamp
- Limited toasts to max 3 at a time
- Toasts should now auto-dismiss after 3 seconds
2025-12-03 00:37:43 +00:00

59 lines
1.6 KiB
TypeScript

'use client'
import { useEffect } from 'react'
import { CheckCircle, XCircle, AlertCircle, X } from 'lucide-react'
export type ToastType = 'success' | 'error' | 'info'
interface ToastProps {
message: string
type: ToastType
onClose: () => void
duration?: number
}
export default function Toast({ message, type, onClose, duration = 3000 }: ToastProps) {
useEffect(() => {
if (duration > 0) {
console.log(`[Toast] Setting timer for ${duration}ms`)
const timer = setTimeout(() => {
console.log('[Toast] Auto-dismissing')
onClose()
}, duration)
return () => {
console.log('[Toast] Clearing timer')
clearTimeout(timer)
}
}
}, [duration, onClose])
const icons = {
success: <CheckCircle size={20} className="flex-shrink-0" />,
error: <XCircle size={20} className="flex-shrink-0" />,
info: <AlertCircle size={20} className="flex-shrink-0" />,
}
const styles = {
success: 'bg-green-50 border-green-200 text-green-800',
error: 'bg-red-50 border-red-200 text-red-800',
info: 'bg-blue-50 border-blue-200 text-blue-800',
}
return (
<div
className={`flex items-center gap-3 px-4 py-3 rounded-lg border shadow-lg ${styles[type]} animate-slide-in-right`}
role="alert"
>
{icons[type]}
<p className="flex-1 text-sm font-medium">{message}</p>
<button
onClick={onClose}
className="text-current opacity-70 hover:opacity-100 transition-opacity tap-target-sm"
aria-label="Close notification"
>
<X size={18} />
</button>
</div>
)
}