import { createFileRoute, useParams } from "@tanstack/react-router"; import { useQueryData } from "@/hooks/useQueryData"; import { API_ENDPOINTS, BASE_URL } from "@/config/api"; import { flexRender, getCoreRowModel, 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 { useQueryClient } from "@tanstack/react-query"; import { useDeviceEvents } from "@/hooks/useDeviceEvents"; 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 queryClient = useQueryClient(); 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({ onDeviceOnlineInRoom: (deviceId, room) => { if (room === roomName) { queryClient.setQueryData( ["devices", roomName], (oldDevices: any[] = []) => oldDevices.map((d) => d.macAddress === deviceId ? { ...d, isOffline: false } : d ) ); } }, onDeviceOfflineInRoom: (deviceId, room) => { if (room === roomName) { queryClient.setQueryData( ["devices", roomName], (oldDevices: any[] = []) => oldDevices.map((d) => d.macAddress === deviceId ? { ...d, isOffline: true } : d ) ); } }, }); const columns: ColumnDef[] = [ { header: "STT", cell: ({ row }) => row.index + 1, }, { header: "MAC Address", accessorKey: "macAddress", }, { header: "Thời gian thiết bị", accessorKey: "deviceTime", cell: ({ getValue }) => { const date = new Date(getValue() as string); return date.toLocaleString(); }, }, { header: "Phiên bản", accessorKey: "version", }, { header: "Địa chỉ IP", accessorKey: "ipAddress", }, { header: "Trạng thái", accessorKey: "isOffline", cell: ({ getValue }) => getValue() ? ( Offline ) : ( Online ), }, ]; const table = useReactTable({ data: devices, columns, getCoreRowModel: getCoreRowModel(), }); if (isLoading) return
Đang tải thiết bị...
; return (

Phòng: {roomName}

Danh sách thiết bị trong phòng

Thiết bị {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() )} ))} ))}
); }