"use client" import { useMemo } from "react" import { type ColumnDef, getCoreRowModel, getSortedRowModel, useReactTable, } 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 { CheckIcon, ChevronRightIcon, CopyIcon, ServerIcon, TagIcon, } from "lucide-react" export type CommunityType = "standard" | "no-export" | "no-advertise" | "local-as" | "custom" export interface CommunityRow { id: string value: string name: string description: string type: CommunityType filterIds: string[] serverCount: number prefixCount: number action: "permit" | "deny" | "local-pref" | "metric" actionValue?: number enabled: boolean } const TYPE_LABELS: Record = { standard: "Стандартный", "no-export": "No-export", "no-advertise": "No-advertise", "local-as": "Local-AS", custom: "Кастомный", } const ACTION_LABELS: Record = { permit: "Permit", deny: "Deny", "local-pref": "Local-pref", metric: "MED/Metric", } const ACTION_COLOR: Record = { permit: "text-emerald-500", deny: "text-red-500", "local-pref": "text-blue-500", metric: "text-amber-500", } interface CommunitiesDataGridProps { communities: CommunityRow[] selectedId?: string | null copiedValue?: string | null onSelect: (community: CommunityRow) => void onCopy: (value: string) => void } function CommunitiesDataGrid({ communities, selectedId, copiedValue, onSelect, onCopy, }: CommunitiesDataGridProps) { const columns = useMemo[]>( () => [ { id: "value", accessorKey: "value", header: ({ column }) => ( ), cell: ({ row }) => { const c = row.original return (
{c.value}
) }, meta: { headerTitle: "Community", headerClassName: DATA_GRID_CELL_PAD_FIRST, cellClassName: DATA_GRID_CELL_PAD_FIRST, }, }, { id: "name", accessorKey: "name", header: ({ column }) => , cell: ({ row }) => (

{row.original.name}

{row.original.description}

), meta: { headerTitle: "Имя / описание", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "type", accessorKey: "type", header: ({ column }) => , cell: ({ row }) => ( {TYPE_LABELS[row.original.type]} ), meta: { headerTitle: "Тип", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "action", accessorKey: "action", header: ({ column }) => , cell: ({ row }) => { const c = row.original return ( {ACTION_LABELS[c.action]} {c.actionValue !== undefined ? ` ${c.actionValue}` : ""} ) }, meta: { headerTitle: "Действие", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD, }, }, { id: "prefixCount", accessorKey: "prefixCount", header: ({ column }) => ( ), cell: ({ row }) => ( {row.original.prefixCount.toLocaleString("ru-RU")} ), meta: { headerTitle: "Маршрутов", headerClassName: cn(DATA_GRID_CELL_PAD, "text-right"), cellClassName: cn(DATA_GRID_CELL_PAD, "text-right"), }, }, { id: "serverCount", accessorKey: "serverCount", header: ({ column }) => ( ), cell: ({ row }) => (
{row.original.serverCount.toLocaleString("ru-RU")}
), meta: { headerTitle: "Серверов", headerClassName: cn(DATA_GRID_CELL_PAD, "text-right"), cellClassName: cn(DATA_GRID_CELL_PAD, "text-right"), }, }, { id: "chevron", header: () => Детали, enableSorting: false, cell: () => , size: 40, meta: { headerClassName: DATA_GRID_CELL_PAD_LAST, cellClassName: DATA_GRID_CELL_PAD_LAST, }, }, ], [copiedValue, onCopy], ) const rowSelection = useMemo( () => (selectedId ? { [selectedId]: true } : {}), [selectedId], ) const table = useReactTable({ data: communities, columns, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getRowId: (row) => row.id, enableRowSelection: true, state: { rowSelection }, }) if (communities.length === 0) { return ( } title="Ничего не найдено" className="border-0 py-16" /> ) } return ( onSelect(row)} tableClassNames={{ headerRow: "border-b border-border", bodyRow: cn( "group/row", "data-[state=selected]:bg-primary/5", "[&:has([data-community-disabled=true])]:opacity-50", ), }} /> ) } export { CommunitiesDataGrid, type CommunitiesDataGridProps, TYPE_LABELS, ACTION_LABELS, ACTION_COLOR, }