Files
MikrotikManager/components/data-grids/gre-tunnels-data-grid.tsx
T

413 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client"
import { DataGridContainer, DataGridScrollArea, DataGridTable, dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid"
import { createContext, useContext, useEffect, useMemo } from "react"
import {
type ColumnDef,
useTable,
} from "@tanstack/react-table"
import type {
GrePool,
GreTunnel,
GreStatus,
IpsecEncAlg,
IpsecAuthAlg,
IpsecDhGroup,
IkeVersion,
Server,
} from "@/lib/data"
import { Flag } from "@/components/flag"
import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
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 {
CodeXmlIcon,
LockIcon,
LockOpenIcon,
MoreHorizontalIcon,
NetworkIcon,
PencilIcon,
PowerIcon,
Trash2Icon,
} from "lucide-react"
const ENC_LABELS: Record<IpsecEncAlg, string> = {
"aes-128": "AES-128",
"aes-192": "AES-192",
"aes-256": "AES-256",
}
const AUTH_LABELS: Record<IpsecAuthAlg, string> = {
sha1: "SHA-1",
sha256: "SHA-256",
sha512: "SHA-512",
}
const DH_LABELS: Record<IpsecDhGroup, string> = {
modp1024: "DH-2 (1024)",
modp2048: "DH-14 (2048)",
modp4096: "DH-16 (4096)",
ecp256: "ECP-256",
ecp384: "ECP-384",
ecp521: "ECP-521",
}
const IKE_LABELS: Record<IkeVersion, string> = { ikev1: "IKEv1", ikev2: "IKEv2" }
const STATUS_MAP: Record<GreStatus, { label: string; dot: string }> = {
up: { label: "Up", dot: "bg-emerald-500" },
degraded: { label: "Degraded", dot: "bg-amber-500" },
down: { label: "Down", dot: "bg-red-500" },
}
function TunnelStatus({ status }: { status: GreStatus }) {
const s = STATUS_MAP[status] ?? STATUS_MAP.degraded
return (
<span className="inline-flex items-center gap-1.5 text-sm">
<span className={cn("size-1.5 rounded-full", s.dot)} />
{s.label}
</span>
)
}
function IpsecBadge({ secured }: { secured: boolean }) {
return secured ? (
<span className="inline-flex items-center gap-1 text-xs font-medium border rounded px-2 py-0.5 bg-emerald-500/10 text-emerald-400 border-emerald-500/20">
<LockIcon className="size-3" /> IPsec
</span>
) : (
<span className="inline-flex items-center gap-1 text-xs font-medium border rounded px-2 py-0.5 bg-muted text-muted-foreground border-border">
<LockOpenIcon className="size-3" /> Открытый
</span>
)
}
interface GreTunnelsDataGridProps {
tunnels: GreTunnel[]
servers: Server[]
pools: GrePool[]
onCodePreview: (tunnel: GreTunnel) => void
onEdit?: (tunnel: GreTunnel) => void
onToggle?: (tunnel: GreTunnel) => void
onDelete?: (tunnel: GreTunnel) => void
mutationsLocked?: boolean
canManage?: (serverId: string) => boolean
unknownServerIds?: Set<string>
emptyTitle?: string
emptyDescription?: string
}
type GreActions = Pick<GreTunnelsDataGridProps, "onCodePreview" | "onEdit" | "onToggle" | "onDelete" | "mutationsLocked" | "canManage">
const GreActionsContext = createContext<GreActions | null>(null)
// Keep the cell component identity stable while permissions and callbacks update.
function GreActionsCell({ row }: { row: { original: GreTunnel } }) {
const actions = useContext(GreActionsContext)
if (!actions) throw new Error("GRE actions context is missing")
const { onCodePreview, onEdit, onToggle, onDelete, mutationsLocked, canManage } = actions
const t = row.original
return (
<div className="flex items-center gap-1 justify-end">
<Button
type="button"
variant="ghost"
size="icon"
className={cn(
"size-11 shrink-0",
)}
title="Предпросмотр кода RouterOS"
aria-label={`Просмотр кода: ${t.name}`}
onClick={(e) => {
e.stopPropagation()
onCodePreview(t)
}}
>
<CodeXmlIcon className="size-3.5" />
</Button>
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button
type="button"
variant="outline"
size="icon"
className={cn(
"size-11 shrink-0 border-border/60 bg-background/80 text-muted-foreground shadow-none",
"opacity-100",
"data-popup-open:opacity-100",
)}
aria-label={`Действия: ${t.name}`}
>
<MoreHorizontalIcon className="size-4" />
</Button>
}
/>
<DropdownMenuContent side="bottom" align="end">
<DropdownMenuGroup>
<DropdownMenuLabel>{t.name}</DropdownMenuLabel>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => onCodePreview(t)}>
<CodeXmlIcon className="size-4" /> Просмотр кода
</DropdownMenuItem>
<DropdownMenuItem disabled={mutationsLocked || (canManage && !canManage(t.serverId)) || !onEdit} onClick={() => onEdit?.(t)}>
<PencilIcon className="size-4" /> Редактировать
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem disabled={mutationsLocked || (canManage && !canManage(t.serverId)) || !onToggle} onClick={() => onToggle?.(t)}>
<PowerIcon className="size-4" />
{t.enabled ? "Выключить" : "Включить"}
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
variant="destructive"
disabled={mutationsLocked || (canManage && !canManage(t.serverId)) || !onDelete}
onClick={() => onDelete?.(t)}
>
<Trash2Icon className="size-4" /> Удалить туннель
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
)
}
function GreTunnelsDataGrid({
tunnels,
servers,
pools,
onCodePreview,
onEdit,
onToggle,
onDelete,
mutationsLocked = false,
unknownServerIds, emptyTitle, emptyDescription, canManage,
}: GreTunnelsDataGridProps) {
const serverMap = useMemo(() => new Map(servers.map((s) => [s.id, s])), [servers])
const poolMap = useMemo(() => new Map(pools.map((p) => [p.id, p])), [pools])
const columns = useMemo<ColumnDef<DataGridFeatures, GreTunnel>[]>(
() => [
{
id: "name",
size: 160,
accessorKey: "name",
header: ({ column }) => (
<DataGridSortHeader column={column} title="Интерфейс / Сервер" className="ml-1" />
),
cell: ({ row }) => {
const t = row.original
const srv = serverMap.get(t.serverId)
return (
<div className="min-w-0">
<p className="font-medium font-mono text-[13px] break-all">{t.name}</p>
<p className="text-xs text-muted-foreground mt-0.5 flex items-center gap-1">
{srv && <Flag code={srv.country} />}
{srv?.name ?? t.serverId}
</p>
</div>
)
},
meta: {
headerTitle: "Интерфейс / Сервер",
headerClassName: DATA_GRID_CELL_PAD_FIRST,
cellClassName: DATA_GRID_CELL_PAD_FIRST,
},
},
{
id: "endpoints",
size: 144,
header: () => <span className="text-xs font-medium text-muted-foreground">Эндпоинты</span>,
enableSorting: false,
cell: ({ row }) => {
const t = row.original
return (
<div>
<p className="font-mono text-xs">
{t.localAddress === "0.0.0.0" ? (
<span className="text-muted-foreground">авто</span>
) : (
t.localAddress
)}
</p>
<p className="font-mono text-xs text-muted-foreground">→ {t.remoteAddress}</p>
</div>
)
},
meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "innerIp",
size: 136,
header: () => <span className="text-xs font-medium text-muted-foreground">Внутренний IP</span>,
enableSorting: false,
cell: ({ row }) => {
const t = row.original
return (
<div>
<p className="font-mono text-xs">{t.localInnerIp}</p>
<p className="font-mono text-xs text-muted-foreground">{t.remoteInnerIp}</p>
</div>
)
},
meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "pool",
size: 96,
accessorKey: "poolId",
header: ({ column }) => <DataGridSortHeader column={column} title="Пул" />,
cell: ({ row }) => (
<span className="text-xs text-muted-foreground font-mono">
{poolMap.get(row.original.poolId)?.name ?? "—"}
</span>
),
meta: { headerTitle: "Пул", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "ipsec",
size: 112,
header: () => <span className="text-xs font-medium text-muted-foreground">IPsec</span>,
enableSorting: false,
cell: ({ row }) => <IpsecBadge secured={!!row.original.ipsec} />,
meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "encryption",
size: 160,
header: () => <span className="text-xs font-medium text-muted-foreground">Шифрование</span>,
enableSorting: false,
cell: ({ row }) => {
const t = row.original
if (!t.ipsec) return <span className="text-xs text-muted-foreground">—</span>
const enc = t.ipsec.encAlg ? ENC_LABELS[t.ipsec.encAlg] : undefined
const auth = t.ipsec.authAlg ? AUTH_LABELS[t.ipsec.authAlg] : undefined
const dh = t.ipsec.dhGroup ? DH_LABELS[t.ipsec.dhGroup] : undefined
const ike = t.ipsec.ikeVersion ? IKE_LABELS[t.ipsec.ikeVersion] : undefined
if (!enc && !auth && !dh && !ike) {
return <span className="text-xs text-muted-foreground">PSK · auto</span>
}
return (
<div className="flex flex-col gap-0.5">
<span className="text-xs font-mono">
{[enc, auth].filter(Boolean).join(" / ") || "PSK"}
</span>
<span className="text-xs text-muted-foreground font-mono">
{[dh?.split(" ")[0], ike].filter(Boolean).join(" · ")}
{t.ipsec.pfs ? " · PFS" : ""}
</span>
</div>
)
},
meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "mtu",
size: 72,
accessorKey: "mtu",
header: ({ column }) => <DataGridSortHeader column={column} title="MTU" />,
cell: ({ row }) => (
<span className="font-mono text-xs text-center block">{row.original.mtu}</span>
),
meta: {
headerTitle: "MTU",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: cn(DATA_GRID_CELL_PAD, "text-center"),
},
},
{
id: "keepalive",
size: 104,
header: () => <span className="text-xs font-medium text-muted-foreground">Keepalive</span>,
enableSorting: false,
cell: ({ row }) => {
const t = row.original
return (
<span className="font-mono text-xs text-muted-foreground">
{t.keepaliveInterval === 0 ? "откл." : `${t.keepaliveInterval}с / ${t.keepaliveRetries}`}
</span>
)
},
meta: { headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "status",
size: 112,
accessorKey: "status",
header: ({ column }) => <DataGridSortHeader column={column} title="Статус" />,
cell: ({ row }) => unknownServerIds?.has(row.original.serverId) ? <span className="text-sm text-muted-foreground">Неизвестно</span> : <TunnelStatus status={row.original.status} />,
meta: { headerTitle: "Статус", headerClassName: DATA_GRID_CELL_PAD, cellClassName: DATA_GRID_CELL_PAD },
},
{
id: "actions",
header: () => <span className="sr-only">Действия</span>,
enableSorting: false,
cell: GreActionsCell,
size: 112,
meta: { headerClassName: DATA_GRID_CELL_PAD_LAST, cellClassName: DATA_GRID_CELL_PAD_LAST },
},
],
[unknownServerIds, poolMap, serverMap],
)
const table = useTable({
features: dataGridFeatures,
manualPagination: false,
autoResetPageIndex: false,
initialState: { pagination: { pageIndex: 0, pageSize: 50 }, columnPinning: { start: ["name"], end: [] } },
data: tunnels,
columns,
getRowId: row => JSON.stringify([row.serverId, row.id]),
})
const { pageIndex, pageSize } = table.state.pagination
useEffect(() => {
const lastPage = Math.max(0, Math.ceil(tunnels.length / pageSize) - 1)
if (pageIndex > lastPage) table.setPageIndex(lastPage)
}, [tunnels.length, pageIndex, pageSize, table])
if (tunnels.length === 0) {
return (
<EmptyState
icon={<NetworkIcon className="size-4" />}
title={emptyTitle ?? "Нет GRE-туннелей"}
description={emptyDescription ?? "Измените фильтр или добавьте туннель"}
className="border-0 py-16"
/>
)
}
return (
<GreActionsContext.Provider value={{ onCodePreview, onEdit, onToggle, onDelete, mutationsLocked, canManage }}>
<DataGridShell
table={table}
recordCount={tunnels.length}
pagination={tunnels.length > 50 || pageSize !== 50}
tableLayout={{ dense: true, columnsPinnable: true }}
tableClassNames={{ headerRow: "border-b border-border", bodyRow: "group/row" }}
>
<DataGridContainer border={false} className="w-full rounded-none border-0">
<DataGridScrollArea orientation="horizontal"><DataGridTable /></DataGridScrollArea>
</DataGridContainer>
</DataGridShell>
</GreActionsContext.Provider>
)
}
export { GreTunnelsDataGrid, type GreTunnelsDataGridProps }