"use client"
import { DataGridContainer, DataGridScrollArea, DataGridTable, dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid"
import { useMemo, useEffect } from "react"
import {
type ColumnDef,
useTable,
} from "@tanstack/react-table"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/reui/badge"
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,
DEFAULT_DATA_GRID_LAYOUT,
} from "@/components/data-grids/shared/data-grid-layout"
import { DataGridSortHeader } from "@/components/data-grids/shared/data-grid-sort-header"
import { UsersExpandedDetail } from "@/components/data-grids/users-expanded-detail"
import { EmptyState } from "@/components/empty-state"
import { ALL_SECTIONS, ROLE_LABEL, type AppUser } from "@/lib/users"
import {
CableIcon,
ChevronDownIcon,
ChevronRightIcon,
PencilIcon,
TrashIcon,
UsersIcon,
} from "lucide-react"
function AvatarCircle({ avatar, active }: { avatar: string; active: boolean }) {
return (
{avatar}
)
}
interface UsersDataGridProps {
users: AppUser[]
serversCount: number | null
allSectionsCount?: number
isLoading?: boolean
actionsDisabled?: boolean
emptyTitle?: string
emptyDescription?: string
onEdit: (user: AppUser) => void
onDelete: (user: AppUser) => void
}
function UsersDataGrid({
users,
serversCount,
allSectionsCount = ALL_SECTIONS.length,
isLoading,
actionsDisabled,
emptyTitle = "Нет пользователей",
emptyDescription = "Добавьте пользователя, чтобы привязать интерфейсы и учитывать трафик",
onEdit,
onDelete,
}: UsersDataGridProps) {
const columns = useMemo[]>(
() => [
{
id: "name",
accessorKey: "name",
size: 180,
header: ({ column }) => (
),
cell: ({ row }) => {
const u = row.original
const expanded = row.getIsExpanded()
return (
{u.name}
{u.email || u.login}
)
},
meta: {
headerTitle: "Пользователь",
headerClassName: DATA_GRID_CELL_PAD_FIRST,
cellClassName: DATA_GRID_CELL_PAD_FIRST,
expandedContent: (row: AppUser) => (
),
},
},
{
id: "role",
accessorKey: "role",
header: ({ column }) => ,
cell: ({ row }) => (
{ROLE_LABEL[row.original.role]}
),
meta: {
headerTitle: "Роль",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "last",
accessorKey: "last",
header: ({ column }) => ,
cell: ({ row }) => (
{row.original.last}
),
meta: {
headerTitle: "Последний вход",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "ifaces",
accessorFn: (u) => u.bindings.length,
header: ({ column }) => ,
cell: ({ row }) => (
{row.original.bindings.length}
),
meta: {
headerTitle: "Интерфейсы",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "status",
accessorKey: "active",
header: ({ column }) => ,
cell: ({ row }) => (
{row.original.active ? "Активен" : "Заблокирован"}
),
meta: {
headerTitle: "Статус",
headerClassName: DATA_GRID_CELL_PAD,
cellClassName: DATA_GRID_CELL_PAD,
},
},
{
id: "actions",
size: 106,
enableSorting: false,
header: () => Действия,
cell: ({ row }) => {
const u = row.original
return (
e.stopPropagation()}
onKeyDown={(e) => e.stopPropagation()}
>
)
},
meta: {
headerTitle: "Действия",
headerClassName: DATA_GRID_CELL_PAD_LAST,
cellClassName: DATA_GRID_CELL_PAD_LAST,
},
},
],
[allSectionsCount, actionsDisabled, onDelete, onEdit, serversCount],
)
const table = useTable({
features: dataGridFeatures,
manualPagination: false,
autoResetPageIndex: false,
autoResetExpanded: false,
initialState: { pagination: { pageIndex: 0, pageSize: 50 }, columnPinning: { start: ["name"], end: [] } },
data: users,
columns,
getRowId: (row) => row.id,
getRowCanExpand: () => true,
})
const { pageIndex, pageSize } = table.state.pagination
useEffect(() => {
const lastPage = Math.max(0, Math.ceil(users.length / pageSize) - 1)
if (pageIndex > lastPage) table.setPageIndex(lastPage)
}, [users.length, pageSize, pageIndex, table])
if (!isLoading && users.length === 0) {
return (
}
title={emptyTitle}
description={emptyDescription}
className="border-0 py-12"
/>
)
}
return (
50 || pageSize !== 50}
tableLayout={{ ...DEFAULT_DATA_GRID_LAYOUT, columnsPinnable: true }}
isLoading={isLoading}
loadingMode="skeleton"
onRowClick={(row) => table.getRow(row.id).toggleExpanded()}
>
)
}
export { UsersDataGrid, type UsersDataGridProps }