"use client" import { dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid" import { useMemo, type ReactNode } from "react" import { type ColumnDef, useTable, } from "@tanstack/react-table" import type { Server } from "@/lib/data" import type { CertificateView } from "@/lib/certificate-view" import { CertDaysCell } from "@/components/data-grids/certificate-days-cell" import { Flag } from "@/components/flag" import { cn } from "@/lib/utils" import { DataGridShell } from "@/components/data-grids/shared/data-grid-shell" import { DATA_GRID_CELL_PAD, DATA_GRID_CELL_PAD_FIRST, DATA_GRID_CELL_PAD_LAST, } from "@/components/data-grids/shared/data-grid-layout" import { DataGridSortHeader } from "@/components/data-grids/shared/data-grid-sort-header" import { CertificateExpandedDetail, } from "@/components/data-grids/certificate-expanded-detail" import { EmptyState } from "@/components/empty-state" import { BadgeCheckIcon, ChevronDownIcon, ChevronRightIcon, ServerIcon, ShieldAlertIcon, ShieldCheckIcon, ShieldOffIcon, CircleHelpIcon, } from "lucide-react" const STATUS_CONFIG: Record< CertificateView["status"], { label: string; icon: ReactNode; badge: string; row: string } > = { unknown: { label: "Неизвестен", icon: , badge: "bg-muted text-muted-foreground border-border", row: "", }, valid: { label: "Действителен", icon: , badge: "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400 border-emerald-500/20", row: "", }, expired: { label: "Истёк", icon: , badge: "bg-red-500/10 text-red-600 dark:text-red-400 border-red-500/20", row: "bg-red-500/5", }, revoked: { label: "Отозван", icon: , badge: "bg-amber-500/10 text-amber-600 dark:text-amber-400 border-amber-500/20", row: "bg-amber-500/5", }, } interface CertificatesDataGridProps { certificates: CertificateView[] serverMap: Map isLoading?: boolean emptyTitle?: string emptyDescription?: string } function CertificatesDataGrid({ certificates, serverMap, isLoading, emptyTitle = "Нет сертификатов", emptyDescription = "Выпустите или импортируйте сертификат" }: CertificatesDataGridProps) { const columns = useMemo[]>( () => [ { id: "name", accessorKey: "name", header: ({ column }) => ( ), cell: ({ row }) => { const cert = row.original const cfg = STATUS_CONFIG[cert.status] const expanded = row.getIsExpanded() return (
{expanded ? ( ) : ( )}
{cfg.icon} {cert.name}

{cert.commonName}

) }, meta: { headerTitle: "Сертификат", headerClassName: DATA_GRID_CELL_PAD_FIRST, cellClassName: DATA_GRID_CELL_PAD_FIRST, expandedContent: (row: CertificateView) => , }, }, { id: "server", accessorKey: "serverId", header: ({ column }) => , cell: ({ row }) => { const cert = row.original const server = serverMap.get(cert.serverId) return server ? (
{server.name}
) : (
{cert.serverName ?? cert.serverId}
) }, meta: { headerTitle: "Сервер", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "issuedBy", accessorKey: "issuedBy", header: ({ column }) => , cell: ({ row }) => (

{row.original.issuedBy}

), meta: { headerTitle: "Издатель", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "daysLeft", accessorKey: "daysLeft", header: ({ column }) => , cell: ({ row }) => (
), meta: { headerTitle: "Срок", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "usage", accessorKey: "usage", header: () => Использование, enableSorting: false, cell: ({ row }) => (
{row.original.usage.map((u) => ( {u} ))}
), meta: { headerTitle: "Использование", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "status", accessorKey: "status", header: ({ column }) => , cell: ({ row }) => { const cfg = STATUS_CONFIG[row.original.status] return ( {cfg.label} ) }, meta: { headerTitle: "Статус", headerClassName: DATA_GRID_CELL_PAD_LAST, cellClassName: DATA_GRID_CELL_PAD_LAST, }, }, ], [serverMap], ) const table = useTable({ features: dataGridFeatures, manualPagination: true, data: certificates, columns, getRowId: (row) => row.id, getRowCanExpand: () => true, }) if (!isLoading && certificates.length === 0) { return ( } title={emptyTitle} description={emptyDescription} className="border-0 py-12" /> ) } return ( table.getRow(row.id).toggleExpanded()} /> ) } export { CertificatesDataGrid, STATUS_CONFIG, type CertificatesDataGridProps }