88 lines
3.1 KiB
TypeScript
88 lines
3.1 KiB
TypeScript
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<string, { label: string; color: string }> = {
|
||
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<number | null>(null)
|
||
|
||
const { data, isLoading, isFetching } = useNotifications({
|
||
offset: (page - 1) * LIMIT,
|
||
limit: LIMIT,
|
||
})
|
||
|
||
const total = data?.total_count ?? 0
|
||
|
||
const columns: Column<any>[] = [
|
||
{
|
||
key: 'id',
|
||
header: 'ID',
|
||
width: 80,
|
||
cell: (r) => <Text size="sm" fw={500}>#{r.id}</Text>
|
||
},
|
||
{
|
||
key: 'title',
|
||
header: 'Заголовок',
|
||
cell: (r) => <Text size="sm" fw={500}>{r.title}</Text>
|
||
},
|
||
{
|
||
key: 'status',
|
||
header: 'Статус',
|
||
width: 140,
|
||
cell: (r) => {
|
||
const statusInfo = STATUS_LABELS[r.status || ''] || { label: r.status || '—', color: 'gray' }
|
||
return <Badge color={statusInfo.color} variant="light">{statusInfo.label}</Badge>
|
||
}
|
||
},
|
||
{
|
||
key: 'delivery',
|
||
header: 'Доставлено',
|
||
width: 180,
|
||
cell: (r) => (
|
||
<Group gap={6}>
|
||
<Badge color="green" variant="light" size="sm">{r.sent_count ?? 0}</Badge>
|
||
{(r.failed_count ?? 0) > 0 && <Badge color="red" variant="light" size="sm">{r.failed_count} ошибок</Badge>}
|
||
<Text size="xs" c="dimmed">из {r.total_count ?? 0}</Text>
|
||
</Group>
|
||
)
|
||
},
|
||
{
|
||
key: 'date',
|
||
header: 'Дата отправки',
|
||
width: 160,
|
||
cell: (r) => <Text size="sm" c="dimmed">{r.sent_at ? new Date(r.sent_at).toLocaleString('ru-RU') : '—'}</Text>
|
||
},
|
||
]
|
||
|
||
return (
|
||
<>
|
||
<DataTable
|
||
columns={columns}
|
||
rows={data?.data ?? []}
|
||
getRowId={(r) => r.id}
|
||
loading={isLoading}
|
||
refreshing={isFetching}
|
||
onRowClick={(r) => setSelectedId(r.id)}
|
||
empty={{ title: 'Уведомления не отправлялись' }}
|
||
pagination={{ page, pageSize: LIMIT, total, onChange: setPage }}
|
||
/>
|
||
|
||
<NotificationDetailsDrawer
|
||
notificationId={selectedId}
|
||
opened={selectedId !== null}
|
||
onClose={() => setSelectedId(null)}
|
||
/>
|
||
</>
|
||
)
|
||
} |