import { createFileRoute, useParams } from "@tanstack/react-router"; import { useQueryData } from "@/hooks/useQueryData"; import { API_ENDPOINTS, BASE_URL } from "@/config/api"; import { flexRender, getCoreRowModel, getPaginationRowModel, useReactTable, type ColumnDef, } from "@tanstack/react-table"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useDeviceEvents } from "@/hooks/useDeviceEvents"; import { ChevronLeft, ChevronRight, Clock, Hash, Loader2, MapPin, Monitor, Wifi, WifiOff, } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Button } from "@/components/ui/button"; export const Route = createFileRoute("/_authenticated/room/$roomName/")({ head: ({ params }) => ({ meta: [{ title: `Danh sách thiết bị phòng ${params.roomName}` }], }), component: RoomDetailComponent, }); function RoomDetailComponent() { const { roomName } = useParams({ from: "/_authenticated/room/$roomName/" }); const { data: devices = [], isLoading } = useQueryData({ queryKey: ["devices", roomName], url: BASE_URL + API_ENDPOINTS.DEVICE_COMM.GET_DEVICE_FROM_ROOM(roomName), }); // Lắng nghe SSE và update state useDeviceEvents(roomName); const columns: ColumnDef[] = [ { header: "STT", cell: ({ row }) => (
{row.index + 1}
), }, { header: () => (
MAC Address
), accessorKey: "macAddress", cell: ({ getValue }) => ( {getValue() as string} ), }, { 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: () => (
Địa chỉ IP
), accessorKey: "ipAddress", cell: ({ getValue }) => ( {getValue() as string} ), }, { 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 } }, }); if (isLoading) { return (

Đang tải danh sách phòng...

); } const onlineDevices = devices.filter( (device: any) => !device.isOffline ).length; const offlineDevices = devices.length - onlineDevices; return (

Phòng: {roomName}

Quản lý và theo dõi thiết bị trong phòng

{onlineDevices}
Online
{offlineDevices}
Offline
{devices.length}
Tổng cộng
Danh sách thiết bị {devices.length === 0 ? (

Không có thiết bị

Phòng này chưa có thiết bị nào được kết nối.

) : ( <>
{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() )} ))} ))}
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()}
)}
); }