TTMT.ManageWebGUI/src/routes/_auth/rooms/$roomName/index.tsx

135 lines
4.8 KiB
TypeScript
Raw Normal View History

import { createFileRoute, useParams, useNavigate } 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, FolderCheck } from "lucide-react";
2025-10-20 16:46:17 +07:00
import { Button } from "@/components/ui/button";
2025-12-22 14:53:19 +07:00
import { useGetDeviceFromRoom } from "@/hooks/queries";
2025-12-03 18:26:36 +07:00
import { useDeviceEvents } from "@/hooks/useDeviceEvents";
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";
2026-01-18 22:52:19 +07:00
import { CommandActionButtons } from "@/components/buttons/command-action-buttons";
2025-08-11 23:21:36 +07:00
2026-03-04 14:41:34 +07:00
export const Route = createFileRoute("/_auth/rooms/$roomName/")({
2025-08-14 12:16:32 +07:00
head: ({ params }) => ({
meta: [{ title: `Danh sách thiết bị phòng ${params.roomName}` }],
}),
loader: async ({ context, params }) => {
context.breadcrumbs = [
{ title: "Danh sách phòng", path: "/rooms/" },
{ title: `Phòng ${params.roomName}`, path: `/rooms/${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() {
2026-03-04 14:41:34 +07:00
const { roomName } = useParams({ from: "/_auth/rooms/$roomName/" });
2025-10-31 16:52:56 +07:00
const [viewMode, setViewMode] = useState<"grid" | "table">("grid");
2025-12-03 18:26:36 +07:00
// SSE real-time updates
useDeviceEvents(roomName);
// Folder status from SS
2025-12-22 14:53:19 +07:00
const { data: devices = [] } = useGetDeviceFromRoom(roomName);
2025-08-14 12:16:32 +07:00
2025-11-19 14:55:14 +07:00
const parseMachineNumber = useMachineNumber();
const navigate = useNavigate();
2025-12-03 18:26:36 +07:00
2025-11-19 14:55:14 +07:00
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">
2026-01-18 22:52:19 +07:00
<CardHeader className="bg-muted/50 space-y-4">
{/* Hàng 1: Thông tin phòng và controls */}
<div className="flex items-center justify-between w-full gap-4">
<div className="flex items-center gap-2">
<Monitor className="h-5 w-5" />
<CardTitle>Danh sách thiết bị phòng {roomName}</CardTitle>
</div>
2025-12-03 18:26:36 +07:00
2026-01-18 22:52:19 +07:00
<div className="flex items-center gap-2 bg-background rounded-lg p-1 border shrink-0">
2025-12-03 18:26:36 +07:00
<Button
variant={viewMode === "grid" ? "default" : "ghost"}
size="sm"
onClick={() => setViewMode("grid")}
className="flex items-center gap-2"
>
<LayoutGrid className="h-4 w-4" />
đ
</Button>
<Button
variant={viewMode === "table" ? "default" : "ghost"}
size="sm"
onClick={() => setViewMode("table")}
className="flex items-center gap-2"
>
<TableIcon className="h-4 w-4" />
Bảng
</Button>
</div>
2026-01-18 22:52:19 +07:00
</div>
{/* Hàng 2: Thực thi lệnh */}
<div className="flex items-center justify-between w-full gap-4">
<div className="flex items-center gap-2 text-sm font-semibold">
Thực thi lệnh
</div>
<div className="flex items-center gap-3 justify-end">
2026-01-18 22:52:19 +07:00
{/* Command Action Buttons */}
{devices.length > 0 && (
<>
<CommandActionButtons roomName={roomName} />
<div className="h-8 w-px bg-border" />
<Button
onClick={() =>
navigate({
to: "/rooms/$roomName/folder-status/",
params: { roomName },
} as any)
}
2026-01-18 22:52:19 +07:00
variant="outline"
size="sm"
className="flex items-center gap-2 shrink-0"
>
<FolderCheck className="h-4 w-4" />
Kiểm tra thư mục Setup
2026-01-18 22:52:19 +07:00
</Button>
</>
)}
</div>
2025-10-20 16:46:17 +07:00
</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-12-03 18:26:36 +07:00
<DeviceGrid
devices={sortedDevices}
/>
2025-09-26 17:56:55 +07:00
) : (
2025-12-03 18:26:36 +07:00
<DeviceTable
devices={sortedDevices}
/>
2025-09-26 17:56:55 +07:00
)}
2025-08-14 12:16:32 +07:00
</CardContent>
</Card>
2025-08-14 12:16:32 +07:00
</div>
);
2025-08-11 23:21:36 +07:00
}