import { flexRender, getCoreRowModel, getPaginationRowModel, useReactTable, type ColumnDef, } from "@tanstack/react-table"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Wifi, WifiOff, Clock, MapPin, Monitor, ChevronLeft, ChevronRight } from "lucide-react"; import { useMachineNumber } from "../hooks/useMachineNumber"; interface DeviceTableProps { devices: any[]; } /** * Component hiển thị danh sách thiết bị ở dạng bảng */ export function DeviceTable({ devices }: DeviceTableProps) { const getMachineNumber = useMachineNumber(); const columns: ColumnDef[] = [ { header: "STT", cell: ({ row }) => { const machineNumber = getMachineNumber(row.original.id || ""); return (
{machineNumber}
); }, }, { header: () => (
Thời gian thiết bị
), accessorKey: "deviceTime", cell: ({ getValue }) => { const date = new Date(getValue() as string); return (
{date.toLocaleDateString("vi-VN")}
{date.toLocaleTimeString("vi-VN")}
); }, }, { header: "Phiên bản", accessorKey: "version", cell: ({ getValue }) => ( v{getValue() as string} ), }, { header: () => (
Phòng
), accessorKey: "room", cell: ({ getValue }) => {getValue() as string}, }, { header: () => (
Thông tin mạng
), accessorKey: "networkInfos", cell: ({ getValue }) => { const infos = getValue() as { macAddress?: string; ipAddress?: string }[]; if (!infos || infos.length === 0) { return Không có dữ liệu; } return (
{infos.map((info, idx) => (
{info.macAddress ?? "-"} {info.ipAddress ?? "-"}
))}
); }, }, { header: "Trạng thái", accessorKey: "isOffline", cell: ({ getValue }) => { const isOffline = getValue() as boolean; return ( {isOffline ? : } {isOffline ? "Offline" : "Online"} ); }, }, ]; const table = useReactTable({ data: devices, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), initialState: { pagination: { pageSize: 16 } }, }); return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {flexRender(header.column.columnDef.header, header.getContext())} ))} ))} {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} ))}
{/* Pagination */}
Hiển thị{" "} {table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1} -{" "} {Math.min( (table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize, devices.length )}{" "} trong tổng số {devices.length} thiết bị
Trang {table.getState().pagination.pageIndex + 1} của {table.getPageCount()}
); }