'use client';
import React from 'react';
import {
Box,
Group,
Text,
ActionIcon,
ThemeIcon,
Stack,
Button,
rem,
Avatar,
Progress,
Badge,
Anchor,
} 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 NotificationItemProps {
notification: Notification;
onRead?: () => void;
onDismiss?: () => void;
onAction?: (action: any) => void;
}
export function NotificationItem({
notification,
onRead,
onDismiss,
onAction,
}: NotificationItemProps) {
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';
}
};
const timeAgo = (date: Date) => {
const seconds = Math.floor((new Date().getTime() - date.getTime()) / 1000);
if (seconds < 60) return 'just now';
if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
return `${Math.floor(seconds / 86400)}d ago`;
};
const handleClick = () => {
if (!notification.readAt && onRead) {
onRead();
}
};
return (
{notification.avatar ? (
{notification.avatar.icon || notification.avatar.name}
) : (
{getIcon(notification.type)}
)}
{notification.title}
{notification.badge && (
{notification.badge.label}
)}
{timeAgo(notification.createdAt)}
{onDismiss && (
{
e.stopPropagation();
onDismiss();
}}
>
)}
{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) => (
))}
)}
);
}