TTMT.ManageWebGUI/src/routes/_auth/agent/index.tsx

110 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-09-10 09:59:17 +07:00
import { createFileRoute } from "@tanstack/react-router";
import { AppManagerTemplate } from "@/template/app-manager-template";
2025-12-22 14:53:19 +07:00
import {
useGetAgentVersion,
useGetRoomList,
useUploadSoftware,
useUpdateAgent,
} from "@/hooks/queries";
2025-09-10 09:59:17 +07:00
import { toast } from "sonner";
import type { ColumnDef } from "@tanstack/react-table";
2025-09-24 16:13:57 +07:00
import type { AxiosProgressEvent } from "axios";
2025-12-03 18:26:36 +07:00
import type { Version } from "@/types/file";
2026-03-04 14:41:34 +07:00
import { ErrorFetchingPage } from "@/components/pages/error-fetching-page";
2025-09-10 09:59:17 +07:00
2025-12-22 14:53:19 +07:00
export const Route = createFileRoute("/_auth/agent/")({
2025-09-10 09:59:17 +07:00
head: () => ({ meta: [{ title: "Quản lý Agent" }] }),
component: AgentsPage,
2026-03-04 14:41:34 +07:00
errorComponent: ErrorFetchingPage,
loader: async ({ context }) => {
context.breadcrumbs = [
{ title: "Quản lý Agent", path: "/_auth/agent/" },
];
},
2025-09-10 09:59:17 +07:00
});
function AgentsPage() {
// Lấy danh sách version
2025-12-22 14:53:19 +07:00
const { data, isLoading } = useGetAgentVersion();
2025-09-10 09:59:17 +07:00
2025-09-26 17:56:55 +07:00
// Lấy danh sách phòng
2025-12-22 14:53:19 +07:00
const { data: roomData } = useGetRoomList();
2025-09-26 17:56:55 +07:00
2025-09-24 16:13:57 +07:00
const versionList: Version[] = Array.isArray(data)
? data
: data
2025-09-26 17:56:55 +07:00
? [data]
: [];
2025-09-10 09:59:17 +07:00
2025-12-22 14:53:19 +07:00
const uploadMutation = useUploadSoftware();
2025-09-10 09:59:17 +07:00
2025-12-22 14:53:19 +07:00
const updateMutation = useUpdateAgent();
2025-09-10 09:59:17 +07:00
2025-09-26 17:56:55 +07:00
const handleUpload = async (
fd: FormData,
2026-03-04 14:41:34 +07:00
config?: { onUploadProgress?: (e: AxiosProgressEvent) => void },
2025-09-26 17:56:55 +07:00
) => {
2025-12-22 14:53:19 +07:00
try {
await uploadMutation.mutateAsync({
formData: fd,
onUploadProgress: config?.onUploadProgress,
});
toast.success("Upload thành công!");
} catch (error: any) {
console.error("Upload error:", error);
toast.error("Upload thất bại!");
}
2025-09-26 17:56:55 +07:00
};
2025-10-31 16:52:56 +07:00
const handleUpdate = async (roomNames: string[]) => {
2025-11-19 14:55:14 +07:00
try {
for (const roomName of roomNames) {
2025-10-31 16:52:56 +07:00
await updateMutation.mutateAsync({
2025-12-22 14:53:19 +07:00
roomName,
2026-03-04 14:41:34 +07:00
data: {},
2025-10-31 16:52:56 +07:00
});
}
2025-11-19 14:55:14 +07:00
toast.success("Đã gửi yêu cầu update cho các phòng đã chọn!");
} catch (e) {
toast.error("Có lỗi xảy ra khi cập nhật!");
2025-10-31 16:52:56 +07:00
}
2025-09-26 17:56:55 +07:00
};
// Cột bảng
2025-09-10 09:59:17 +07:00
const columns: ColumnDef<Version>[] = [
2025-09-24 16:13:57 +07:00
{ accessorKey: "version", header: "Phiên bản" },
{ accessorKey: "fileName", header: "Tên file" },
{ accessorKey: "folderPath", header: "Đường dẫn" },
2025-09-10 09:59:17 +07:00
{
accessorKey: "updatedAt",
header: "Thời gian cập nhật",
cell: ({ getValue }) =>
getValue()
? new Date(getValue() as string).toLocaleString("vi-VN")
: "N/A",
},
2025-09-24 16:13:57 +07:00
{
accessorKey: "requestUpdateAt",
header: "Thời gian yêu cầu cập nhật",
cell: ({ getValue }) =>
getValue()
? new Date(getValue() as string).toLocaleString("vi-VN")
: "N/A",
2025-09-26 17:56:55 +07:00
},
2025-09-10 09:59:17 +07:00
];
return (
<AppManagerTemplate<Version>
title="Quản lý Agent"
description="Quản lý và theo dõi các phiên bản Agent"
data={versionList}
isLoading={isLoading}
columns={columns}
2025-09-24 16:13:57 +07:00
onUpload={handleUpload}
onUpdate={handleUpdate}
2025-09-10 09:59:17 +07:00
updateLoading={updateMutation.isPending}
2025-11-19 14:55:14 +07:00
rooms={roomData}
2025-09-10 09:59:17 +07:00
/>
);
2025-09-26 17:56:55 +07:00
}