TTMT.ManageWebGUI/src/routes/_authenticated/room/$roomName/index.tsx

83 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-08-14 12:16:32 +07:00
import { createFileRoute, useParams } from "@tanstack/react-router";
2025-10-20 16:46:17 +07:00
import { useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { LayoutGrid, TableIcon, Monitor } from "lucide-react";
import { Button } from "@/components/ui/button";
2025-08-14 12:16:32 +07:00
import { useQueryData } from "@/hooks/useQueryData";
import { API_ENDPOINTS, BASE_URL } from "@/config/api";
2025-11-19 14:55:14 +07:00
import { DeviceGrid } from "@/components/grids/device-grid";
import { DeviceTable } from "@/components/tables/device-table";
import { useMachineNumber } from "@/hooks/useMachineNumber";
2025-08-11 23:21:36 +07:00
2025-08-14 12:16:32 +07:00
export const Route = createFileRoute("/_authenticated/room/$roomName/")({
head: ({ params }) => ({
meta: [{ title: `Danh sách thiết bị phòng ${params.roomName}` }],
}),
2025-10-20 16:46:17 +07:00
component: RoomDetailPage,
2025-08-14 12:16:32 +07:00
});
2025-08-11 23:21:36 +07:00
2025-10-20 16:46:17 +07:00
function RoomDetailPage() {
2025-08-14 12:16:32 +07:00
const { roomName } = useParams({ from: "/_authenticated/room/$roomName/" });
2025-10-31 16:52:56 +07:00
const [viewMode, setViewMode] = useState<"grid" | "table">("grid");
2025-10-20 16:46:17 +07:00
const { data: devices = [] } = useQueryData({
2025-08-14 12:16:32 +07:00
queryKey: ["devices", roomName],
url: BASE_URL + API_ENDPOINTS.DEVICE_COMM.GET_DEVICE_FROM_ROOM(roomName),
});
2025-11-19 14:55:14 +07:00
const parseMachineNumber = useMachineNumber();
const sortedDevices = [...devices].sort((a, b) => {
return parseMachineNumber(a.id) - parseMachineNumber(b.id);
});
2025-08-14 12:16:32 +07:00
return (
2025-09-26 17:56:55 +07:00
<div className="w-full px-6 space-y-6">
<Card className="shadow-sm">
2025-10-20 16:46:17 +07:00
<CardHeader className="bg-muted/50 flex items-center justify-between">
2025-09-26 17:56:55 +07:00
<CardTitle className="flex items-center gap-2">
<Monitor className="h-5 w-5" />
2025-10-20 16:46:17 +07:00
Danh sách thiết bị phòng {roomName}
2025-09-26 17:56:55 +07:00
</CardTitle>
2025-10-20 16:46:17 +07:00
<div className="flex items-center gap-2 bg-background rounded-lg p-1 border">
<Button
2025-10-31 16:52:56 +07:00
variant={viewMode === "grid" ? "default" : "ghost"}
2025-10-20 16:46:17 +07:00
size="sm"
2025-10-31 16:52:56 +07:00
onClick={() => setViewMode("grid")}
2025-10-20 16:46:17 +07:00
className="flex items-center gap-2"
>
2025-10-31 16:52:56 +07:00
<LayoutGrid className="h-4 w-4" />
đ
2025-10-20 16:46:17 +07:00
</Button>
<Button
2025-10-31 16:52:56 +07:00
variant={viewMode === "table" ? "default" : "ghost"}
2025-10-20 16:46:17 +07:00
size="sm"
2025-10-31 16:52:56 +07:00
onClick={() => setViewMode("table")}
2025-10-20 16:46:17 +07:00
className="flex items-center gap-2"
>
2025-10-31 16:52:56 +07:00
<TableIcon className="h-4 w-4" />
Bảng
2025-10-20 16:46:17 +07:00
</Button>
</div>
2025-08-14 12:16:32 +07:00
</CardHeader>
2025-10-20 16:46:17 +07:00
2025-09-26 17:56:55 +07:00
<CardContent className="p-0">
{devices.length === 0 ? (
<div className="flex flex-col items-center justify-center py-12">
<Monitor className="h-12 w-12 text-muted-foreground mb-4" />
<h3 className="text-lg font-semibold mb-2">Không thiết bị</h3>
<p className="text-muted-foreground text-center max-w-sm">
Phòng này chưa thiết bị nào đưc kết nối.
</p>
</div>
2025-10-20 16:46:17 +07:00
) : viewMode === "grid" ? (
2025-11-19 14:55:14 +07:00
<DeviceGrid devices={sortedDevices} />
2025-09-26 17:56:55 +07:00
) : (
2025-11-19 14:55:14 +07:00
<DeviceTable devices={sortedDevices} />
2025-09-26 17:56:55 +07:00
)}
2025-08-14 12:16:32 +07:00
</CardContent>
</Card>
</div>
);
2025-08-11 23:21:36 +07:00
}