"use client" import { useMemo } from "react" import { type ColumnDef, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table" import type { Server } from "@/lib/data" import { cn } from "@/lib/utils" import { FormToggle } from "@/components/form-kit" 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, Trash2Icon } from "lucide-react" export type SchedType = "ping" | "bandwidth" | "both" export interface SchedRule { id: string srcId: string tunnelId: string type: SchedType intervalMin: number enabled: boolean lastRun: string | null nextRunMin: number | null } const TYPE_LABEL: Record = { ping: "Ping", bandwidth: "BW-тест", both: "Ping + BW", } interface ProbesScheduleDataGridProps { rules: SchedRule[] serverOptions: Server[] tunnelName: (srcId: string, tunnelId: string) => string | undefined onToggleEnabled: (id: string, enabled: boolean) => void onDelete: (id: string) => void } function ProbesScheduleDataGrid({ rules, serverOptions, tunnelName, onToggleEnabled, onDelete, }: ProbesScheduleDataGridProps) { const serverMap = useMemo( () => new Map(serverOptions.map((s) => [s.id, s])), [serverOptions], ) const columns = useMemo[]>( () => [ { id: "enabled", accessorKey: "enabled", header: () => Вкл, enableSorting: false, cell: ({ row }) => (
e.stopPropagation()}> onToggleEnabled(row.original.id, v)} />
), size: 48, meta: { headerClassName: DATA_GRID_CELL_PAD_FIRST, cellClassName: DATA_GRID_CELL_PAD_FIRST, }, }, { id: "tunnel", accessorKey: "tunnelId", header: ({ column }) => , cell: ({ row }) => ( {tunnelName(row.original.srcId, row.original.tunnelId) ?? row.original.tunnelId} ), meta: { headerTitle: "Туннель", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "server", accessorKey: "srcId", header: ({ column }) => , cell: ({ row }) => ( {serverMap.get(row.original.srcId)?.name ?? row.original.srcId} ), meta: { headerTitle: "Сервер", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "type", accessorKey: "type", header: ({ column }) => , cell: ({ row }) => ( {TYPE_LABEL[row.original.type]} ), meta: { headerTitle: "Тип", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "intervalMin", accessorKey: "intervalMin", header: ({ column }) => , cell: ({ row }) => ( каждые {row.original.intervalMin} мин ), meta: { headerTitle: "Интервал", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "lastRun", header: () => ( Последний / следующий ), enableSorting: false, cell: ({ row }) => { const rule = row.original return (
{rule.lastRun && {rule.lastRun}} {rule.nextRunMin != null && rule.enabled && ( · через {rule.nextRunMin} мин )}
) }, meta: { headerTitle: "Последний / следующий", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "actions", header: () => Действия, enableSorting: false, cell: ({ row }) => ( ), size: 48, meta: { headerClassName: DATA_GRID_CELL_PAD_LAST, cellClassName: DATA_GRID_CELL_PAD_LAST, }, }, ], [onDelete, onToggleEnabled, serverMap, tunnelName], ) const table = useReactTable({ data: rules, columns, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getRowId: (row) => row.id, }) if (rules.length === 0) { return ( } title="Нет правил расписания" description="Добавьте правило ping или bandwidth-теста" className="border-0 py-10" /> ) } return } export { ProbesScheduleDataGrid, type ProbesScheduleDataGridProps }