import { useState } from 'react' import { Badge, Group, Text } from '@mantine/core' import { useNotifications } from '@/features/notifications/hooks/useNotifications' import { DataTable, type Column } from '@/components/ui/DataTable' import NotificationDetailsDrawer from './NotificationDetailsDrawer' const LIMIT = 20 const STATUS_LABELS: Record = { pending: { label: 'В очереди', color: 'gray' }, sending: { label: 'Отправляется', color: 'blue' }, completed: { label: 'Завершено', color: 'green' }, failed: { label: 'Ошибка', color: 'red' }, } export default function NotificationsHistoryTable() { const [page, setPage] = useState(1) const [selectedId, setSelectedId] = useState(null) const { data, isLoading, isFetching } = useNotifications({ offset: (page - 1) * LIMIT, limit: LIMIT, }) const total = data?.total_count ?? 0 const columns: Column[] = [ { key: 'id', header: 'ID', width: 80, cell: (r) => #{r.id} }, { key: 'title', header: 'Заголовок', cell: (r) => {r.title} }, { key: 'status', header: 'Статус', width: 140, cell: (r) => { const statusInfo = STATUS_LABELS[r.status || ''] || { label: r.status || '—', color: 'gray' } return {statusInfo.label} } }, { key: 'delivery', header: 'Доставлено', width: 180, cell: (r) => ( {r.sent_count ?? 0} {(r.failed_count ?? 0) > 0 && {r.failed_count} ошибок} из {r.total_count ?? 0} ) }, { key: 'date', header: 'Дата отправки', width: 160, cell: (r) => {r.sent_at ? new Date(r.sent_at).toLocaleString('ru-RU') : '—'} }, ] return ( <> r.id} loading={isLoading} refreshing={isFetching} onRowClick={(r) => setSelectedId(r.id)} empty={{ title: 'Уведомления не отправлялись' }} pagination={{ page, pageSize: LIMIT, total, onChange: setPage }} /> setSelectedId(null)} /> ) }