"use client" import { useMemo } from "react" import { type ColumnDef, getCoreRowModel, useReactTable, } from "@tanstack/react-table" import type { SchedulerJobStatusDto } from "@/lib/scheduler-settings" import { FormToggle } from "@/components/form-kit" import { Badge } from "@/components/reui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" 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 { cn } from "@/lib/utils" import { RefreshCwIcon } from "lucide-react" export interface SchedulerJobGridRow { id: string jobKey: string label: string description: string fixedSchedule: boolean enabled: boolean intervalValue: string intervalReadOnly: boolean intervalDisabled: boolean defaultInterval: number job?: SchedulerJobStatusDto onEnabledChange?: (enabled: boolean) => void onIntervalChange: (value: string) => void onRunNow: () => void runNowLoading: boolean saveBusy: boolean } interface DataCollectionSchedulerDataGridProps { rows: SchedulerJobGridRow[] } function DataCollectionSchedulerDataGrid({ rows }: DataCollectionSchedulerDataGridProps) { const columns = useMemo[]>( () => [ { id: "task", accessorKey: "label", header: () => Задача, enableSorting: false, cell: ({ row }) => (
{row.original.label}

{row.original.description}

{row.original.jobKey}

), meta: { headerClassName: DATA_GRID_CELL_PAD_FIRST, cellClassName: cn(DATA_GRID_CELL_PAD_FIRST, "align-top"), }, }, { id: "enabled", accessorKey: "enabled", header: () => ( Вкл ), enableSorting: false, cell: ({ row }) => { const { fixedSchedule, enabled, saveBusy, onEnabledChange } = row.original return (
{ if (fixedSchedule || saveBusy) return onEnabledChange?.(v) }} />
) }, size: 56, meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: cn(DATA_GRID_CELL_PAD, "align-top") }, }, { id: "interval", accessorKey: "intervalValue", header: () => Интервал (с), enableSorting: false, cell: ({ row }) => { const r = row.original return (
r.onIntervalChange(e.target.value)} className="h-8 text-sm tabular-nums" inputMode="numeric" readOnly={r.intervalReadOnly} disabled={r.intervalDisabled} placeholder={String(r.defaultInterval)} />
) }, meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: cn(DATA_GRID_CELL_PAD, "align-top") }, }, { id: "lastRun", accessorFn: (row) => row.job?.lastFinishedAt ?? "", header: () => Последний прогон, enableSorting: false, cell: ({ row }) => { const j = row.original.job return (
{j?.lastFinishedAt ? new Date(j.lastFinishedAt).toLocaleString("ru-RU") : "—"} {j?.lastDurationMs != null && ( {j.lastDurationMs} мс )}
) }, meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: cn(DATA_GRID_CELL_PAD, "align-top") }, }, { id: "status", accessorFn: (row) => row.job?.lastStatus ?? "", header: () => Статус, enableSorting: false, cell: ({ row }) => { const j = row.original.job return (
{j?.running ? ( выполняется ) : null} {j?.lastStatus ? ( {j.lastStatus} ) : null} {j?.lastError ? ( {j.lastError} ) : null}
) }, meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: cn(DATA_GRID_CELL_PAD, "align-top") }, }, { id: "runNow", header: () => ( Сейчас ), enableSorting: false, cell: ({ row }) => { const r = row.original return (
) }, meta: { headerClassName: DATA_GRID_CELL_PAD_LAST, cellClassName: cn(DATA_GRID_CELL_PAD_LAST, "align-top"), }, }, ], [], ) const table = useReactTable({ data: rows, columns, getCoreRowModel: getCoreRowModel(), getRowId: (row) => row.id, }) return ( ) } export { DataCollectionSchedulerDataGrid, type DataCollectionSchedulerDataGridProps }