"use client" import { dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid" import { useMemo, useEffect } from "react" import { type ColumnDef, useTable, } from "@tanstack/react-table" 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 { EmptyState } from "@/components/empty-state" import { ClockIcon } from "lucide-react" import type {SpeedProbeApiRow} from "@/lib/probe-schedule-response" export type {SpeedProbeApiRow} from "@/lib/probe-schedule-response" interface ProbesSpeedProbesDataGridProps { rows: SpeedProbeApiRow[] serverName: (id: string) => string } function ProbesSpeedProbesDataGrid({ rows, serverName }: ProbesSpeedProbesDataGridProps) { const columns = useMemo[]>( () => [ { id: "src", header: ({ column }) => , accessorFn: (row) => row.srcServerId, cell: ({ row }) => { const r = row.original return ( {serverName(r.srcServerId)} {r.srcInterface ? ` · ${r.srcInterface}` : ""} ) }, meta: { headerTitle: "Источник", headerClassName: DATA_GRID_CELL_PAD_FIRST, cellClassName: DATA_GRID_CELL_PAD_FIRST, }, }, { id: "dst", accessorFn: (row) => row.dstServerId, header: ({ column }) => , cell: ({ row }) => { const r = row.original return ( {serverName(r.dstServerId)} {r.dstInterface ? ` · ${r.dstInterface}` : ""} ) }, meta: { headerTitle: "Назначение", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "protocol", accessorKey: "protocol", header: ({ column }) => , cell: ({ row }) => {row.original.protocol.toUpperCase()}, meta: { headerTitle: "Протокол", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "durationSec", accessorKey: "durationSec", header: ({ column }) => , cell: ({ row }) => {row.original.durationSec}s, meta: { headerTitle: "Сек", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "enabled", accessorKey: "enabled", header: ({ column }) => , cell: ({ row }) => {row.original.enabled ? "да" : "нет"}, meta: { headerTitle: "Вкл", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "lastRunAt", accessorKey: "lastRunAt", header: ({ column }) => , cell: ({ row }) => { const r = row.original return ( {r.lastRunAt ?? "—"} {r.lastStatus === "done" && ( TX: {r.lastTxAvgMbps==null?"неизвестно":r.lastTxAvgMbps.toFixed(1)+" Мбит/с"} · RX: {r.lastRxAvgMbps==null?"неизвестно":r.lastRxAvgMbps.toFixed(1)+" Мбит/с"} )} {r.lastStatus === "error" && r.lastError && ( {r.lastError} )} ) }, meta: { headerTitle: "Последний запуск", headerClassName: DATA_GRID_CELL_PAD_LAST, cellClassName: DATA_GRID_CELL_PAD_LAST, }, }, ], [serverName], ) const table = useTable({ features: dataGridFeatures, manualPagination: false, autoResetPageIndex: false, initialState: { pagination: { pageIndex: 0, pageSize: 50 } }, data: rows, columns, getRowId: (row) => row.id, }) const { pageIndex, pageSize } = table.state.pagination useEffect(() => { const lastPage = Math.max(0, Math.ceil(rows.length / pageSize) - 1) if (pageIndex > lastPage) table.setPageIndex(lastPage) }, [rows.length, pageSize, pageIndex, table]) if (rows.length === 0) { return ( } title="Нет записей speed-test" description="Добавьте пробы в разделе мониторинга." className="border-0 py-10" /> ) } return ( 50 || table.state.pagination.pageSize !== 50} tableClassNames={{ headerRow: "border-b border-border", bodyRow: cn("group/row", "[&[data-disabled=true]]:opacity-50"), }} /> ) } export { ProbesSpeedProbesDataGrid, type ProbesSpeedProbesDataGridProps }