"use client" import { useMemo } from "react" import { type ColumnDef, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table" import type { CommRec } from "@/lib/route-optimizer-data" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" 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 { ArrowRightIcon, PinIcon, RefreshCwIcon } from "lucide-react" function Chip({ children, color }: { children: React.ReactNode; color?: string }) { return ( {children} ) } function ProbChip({ prob, best }: { prob: number; best?: boolean }) { return ( {prob}% ) } interface RouteOptimizerCommRecsDataGridProps { recs: CommRec[] homeId: string pinned: Set applied: Set applying: Set onPin: (key: string) => void onApply: (community: string, homeId: string) => void } function RouteOptimizerCommRecsDataGrid({ recs, homeId, pinned, applied, applying, onPin, onApply, }: RouteOptimizerCommRecsDataGridProps) { const rows = useMemo( () => recs.map((r, idx) => ({ ...r, _rowKey: `${homeId}::${r.community}::${idx}` })), [homeId, recs], ) const columns = useMemo[]>( () => [ { id: "community", accessorKey: "community", header: ({ column }) => ( ), cell: ({ row }) => (
{row.original.community}
{row.original.communityName}
), meta: { headerTitle: "Community", headerClassName: DATA_GRID_CELL_PAD_FIRST, cellClassName: DATA_GRID_CELL_PAD_FIRST, }, }, { id: "current", header: () => ( Текущий (WAN → JH → Exit) ), enableSorting: false, cell: ({ row }) => { const r = row.original if (!r.current) return return (
{r.current.wan} {r.current.jh} {r.current.exit} ({r.current.gateway})
) }, meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD }, }, { id: "recommended", header: () => Рекомендуемый, enableSorting: false, cell: ({ row }) => { const r = row.original const pinKey = `${homeId}::${r.community}` const isPinned = pinned.has(pinKey) if (!r.recommended) return return (
{r.recommended.wan} {r.recommended.jh} {r.recommended.exit} {r.shouldSwitch && !isPinned && ( +{(r.recommended.prob ?? 0) - (r.current?.prob ?? 0)}% )}
) }, meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD }, }, { id: "probability", header: () => ( P(тек / рек) ), enableSorting: false, cell: ({ row }) => { const r = row.original const pinKey = `${homeId}::${r.community}` return (
/
) }, meta: { headerClassName: cn(DATA_GRID_CELL_PAD, "text-center"), cellClassName: cn(DATA_GRID_CELL_PAD, "text-center"), }, }, { id: "actions", header: () => ( Действие ), enableSorting: false, cell: ({ row }) => { const r = row.original const pinKey = `${homeId}::${r.community}` const isPinned = pinned.has(pinKey) const isApplied = applied.has(pinKey) const isApplying = applying.has(pinKey) const canApply = r.shouldSwitch && !isPinned && !isApplied return (
e.stopPropagation()}> {isPinned && } {canApply && ( )} {isApplied && ( ✓ применено )}
) }, meta: { headerClassName: cn(DATA_GRID_CELL_PAD_LAST, "text-right"), cellClassName: DATA_GRID_CELL_PAD_LAST, }, }, ], [applied, applying, homeId, onApply, onPin, pinned], ) const table = useReactTable({ data: rows, columns, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getRowId: (row) => row._rowKey, }) return ( ) } export { RouteOptimizerCommRecsDataGrid, type RouteOptimizerCommRecsDataGridProps }