Files
MikrotikManager/components/data-grids/certificates-data-grid.tsx
T

276 lines
9.5 KiB
TypeScript

"use client"
import { DataGridContainer, DataGridScrollArea, DataGridTable, dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid"
import { useMemo, useEffect, 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 { Button } from "@/components/ui/button"
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,
DEFAULT_DATA_GRID_LAYOUT,
} 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: <CircleHelpIcon aria-hidden="true" className="size-4 text-muted-foreground" />,
badge: "bg-muted text-muted-foreground border-border",
row: "",
},
valid: {
label: "Действителен",
icon: <BadgeCheckIcon aria-hidden="true" className="size-4 text-success" />,
badge: "bg-success/10 text-success border-success/20",
row: "",
},
expired: {
label: "Истёк",
icon: <ShieldOffIcon aria-hidden="true" className="size-4 text-destructive" />,
badge: "bg-destructive/10 text-destructive border-destructive/20",
row: "bg-destructive/5",
},
revoked: {
label: "Отозван",
icon: <ShieldAlertIcon aria-hidden="true" className="size-4 text-warning" />,
badge: "bg-warning/10 text-warning border-warning/20",
row: "bg-warning/5",
},
}
interface CertificatesDataGridProps {
certificates: CertificateView[]
serverMap: Map<string, Server>
isLoading?: boolean
emptyTitle?: string
emptyDescription?: string
}
function CertificatesDataGrid({ certificates, serverMap, isLoading, emptyTitle = "Нет сертификатов", emptyDescription = "Выпустите или импортируйте сертификат" }: CertificatesDataGridProps) {
const columns = useMemo<ColumnDef<DataGridFeatures, CertificateView>[]>(
() => [
{
id: "name",
accessorKey: "name",
size: 160,
header: ({ column }) => (
<DataGridSortHeader column={column} title="Сертификат" className="ml-1" />
),
cell: ({ row }) => {
const cert = row.original
const cfg = STATUS_CONFIG[cert.status]
const expanded = row.getIsExpanded()
return (
<div className="flex items-start gap-2 min-w-0">
<Button type="button" variant="ghost" size="icon" className="size-11 sm:size-8 shrink-0" aria-label={"Детали сертификата " + cert.name} aria-expanded={expanded} onClick={event => { event.stopPropagation(); row.toggleExpanded() }}>
{expanded ? (
<ChevronDownIcon aria-hidden="true" className="size-3.5 mt-0.5 shrink-0 text-muted-foreground" />
) : (
<ChevronRightIcon aria-hidden="true" className="size-3.5 mt-0.5 shrink-0 text-muted-foreground/40" />
)}
</Button>
<div className="min-w-0">
<div className="flex items-center gap-2 min-w-0">
<span className="hidden sm:inline shrink-0">{cfg.icon}</span>
<span className="font-medium text-sm truncate" title={cert.name}>
{cert.name}
</span>
</div>
<p className="text-xs text-muted-foreground font-mono mt-0.5 truncate" title={cert.commonName}>
{cert.commonName}
</p>
</div>
</div>
)
},
meta: {
headerTitle: "Сертификат",
headerClassName: DATA_GRID_CELL_PAD_FIRST,
cellClassName: DATA_GRID_CELL_PAD_FIRST,
expandedContent: (row: CertificateView) => <CertificateExpandedDetail cert={row} />,
},
},
{
id: "server",
accessorKey: "serverId",
header: ({ column }) => <DataGridSortHeader column={column} title="Сервер" />,
cell: ({ row }) => {
const cert = row.original
const server = serverMap.get(cert.serverId)
return server ? (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground min-w-0">
<Flag code={server.country} size={12} />
<span className="font-mono truncate">{server.name}</span>
</div>
) : (
<div className="flex items-center gap-1.5 text-xs text-muted-foreground min-w-0">
<ServerIcon className="size-3.5" />
<span className="font-mono truncate">{cert.serverName ?? cert.serverId}</span>
</div>
)
},
meta: {
headerTitle: "Сервер",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "issuedBy",
accessorKey: "issuedBy",
header: ({ column }) => <DataGridSortHeader column={column} title="Издатель" />,
cell: ({ row }) => (
<p className="text-xs text-muted-foreground truncate min-w-0" title={row.original.issuedBy}>
{row.original.issuedBy}
</p>
),
meta: {
headerTitle: "Издатель",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "daysLeft",
accessorKey: "daysLeft",
header: ({ column }) => <DataGridSortHeader column={column} title="Срок" />,
cell: ({ row }) => (
<div className="min-w-0">
<CertDaysCell cert={row.original} />
</div>
),
meta: {
headerTitle: "Срок",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "usage",
accessorKey: "usage",
header: () => <span className="text-xs font-medium text-muted-foreground">Использование</span>,
enableSorting: false,
cell: ({ row }) => (
<div className="flex flex-wrap gap-1 min-w-0 overflow-hidden">
{row.original.usage.map((u) => (
<span
key={u}
className="text-[10px] font-mono px-1.5 py-0.5 rounded bg-muted text-muted-foreground border"
>
{u}
</span>
))}
</div>
),
meta: {
headerTitle: "Использование",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "status",
size: 120,
accessorKey: "status",
header: ({ column }) => <DataGridSortHeader column={column} title="Статус" />,
cell: ({ row }) => {
const cfg = STATUS_CONFIG[row.original.status]
return (
<span
className={cn(
"text-[11px] font-mono px-2 py-0.5 rounded border whitespace-nowrap",
cfg.badge,
)}
>
{cfg.label}
</span>
)
},
meta: {
headerTitle: "Статус",
headerClassName: DATA_GRID_CELL_PAD_LAST,
cellClassName: DATA_GRID_CELL_PAD_LAST,
},
},
],
[serverMap],
)
const table = useTable({
features: dataGridFeatures,
manualPagination: false,
autoResetPageIndex: false,
autoResetExpanded: false,
initialState: { pagination: { pageIndex: 0, pageSize: 50 }, columnPinning: { start: ["name"], end: [] } },
data: certificates,
columns,
getRowId: (row) => row.id,
getRowCanExpand: () => true,
})
const { pageIndex, pageSize } = table.state.pagination
useEffect(() => {
const lastPage = Math.max(0, Math.ceil(certificates.length / pageSize) - 1)
if (pageIndex > lastPage) table.setPageIndex(lastPage)
}, [certificates.length, pageSize, pageIndex, table])
if (!isLoading && certificates.length === 0) {
return (
<EmptyState
icon={<ShieldCheckIcon className="size-4" />}
title={emptyTitle}
description={emptyDescription}
className="border-0 py-12"
/>
)
}
return (
<DataGridShell
table={table}
recordCount={certificates.length}
tableLayout={{ ...DEFAULT_DATA_GRID_LAYOUT, columnsPinnable: true }}
pagination={certificates.length > 50 || pageSize !== 50}
isLoading={isLoading}
loadingMode="skeleton"
onRowClick={(row) => table.getRow(row.id).toggleExpanded()}
>
<DataGridContainer border={false} className="w-full rounded-none border-0">
<DataGridScrollArea orientation="horizontal">
<DataGridTable />
</DataGridScrollArea>
</DataGridContainer>
</DataGridShell>
)
}
export { CertificatesDataGrid, STATUS_CONFIG, type CertificatesDataGridProps }