'use client'; import React, { useEffect } from 'react'; import { Box, Group, Text, ActionIcon, ThemeIcon, Stack, Button, Progress, Badge, Anchor, Avatar, Transition, Paper, } from '@mantine/core'; import { IconX, IconCheck, IconAlertCircle, IconInfoCircle, IconExclamationCircle, IconExternalLink, IconClock, IconCircleCheck, IconCircleX, IconCircleDashed, IconCircleOff, } from '@tabler/icons-react'; import { Notification, NotificationType } from '../../types'; interface ToastProps { notification: Notification; onDismiss?: () => void; onAction?: (action: any) => void; position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'; duration?: number; } export function Toast({ notification, onDismiss, onAction, position = 'top-right', duration = 5000, }: ToastProps) { const [mounted, setMounted] = React.useState(false); useEffect(() => { setMounted(true); if (duration && duration > 0 && notification.type !== 'loading') { const timer = setTimeout(() => { handleDismiss(); }, duration); return () => clearTimeout(timer); } }, [duration, notification.type]); const handleDismiss = () => { setMounted(false); setTimeout(() => { onDismiss?.(); }, 300); }; const getIcon = (type: NotificationType) => { switch (type) { case 'success': return ; case 'error': return ; case 'warning': return ; case 'loading': return ; case 'info': default: return ; } }; const getStatusIcon = (type: string) => { switch (type) { case 'pending': return ; case 'processing': return ; case 'completed': return ; case 'failed': return ; case 'cancelled': return ; default: return ; } }; const getColor = (type: NotificationType) => { switch (type) { case 'success': return 'green'; case 'error': return 'red'; case 'warning': return 'yellow'; case 'loading': return 'blue'; case 'info': default: return 'blue'; } }; const getStatusColor = (type: string) => { switch (type) { case 'pending': return 'gray'; case 'processing': return 'blue'; case 'completed': return 'green'; case 'failed': return 'red'; case 'cancelled': return 'orange'; default: return 'gray'; } }; return ( {(styles) => ( {notification.avatar ? ( {notification.avatar.icon || notification.avatar.name} ) : ( {getIcon(notification.type)} )} {notification.title} {notification.badge && ( {notification.badge.label} )} {notification.message && ( {notification.message} )} {notification.status && ( {notification.status.icon || getStatusIcon(notification.status.type)} {notification.status.label || notification.status.type} )} {notification.progress && ( {notification.progress.label && ( {notification.progress.label} )} )} {notification.link && ( e.stopPropagation()} > {notification.link.label || notification.link.href} {notification.link.external && } )} {notification.actions && notification.actions.length > 0 && ( {notification.actions.map((action, index) => ( ))} )} )} ); }