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

107 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";
import { useQueryData } from "@/hooks/useQueryData";
import { useMutationData } from "@/hooks/useMutationData";
import { BASE_URL, API_ENDPOINTS } from "@/config/api";
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-09-10 09:59:17 +07:00
type Version = {
id?: string;
version: string;
fileName: string;
folderPath: string;
updatedAt?: string;
2025-09-24 16:13:57 +07:00
requestUpdateAt?: string;
2025-09-10 09:59:17 +07:00
};
export const Route = createFileRoute("/_authenticated/agent/")({
head: () => ({ meta: [{ title: "Quản lý Agent" }] }),
component: AgentsPage,
});
function AgentsPage() {
// Lấy danh sách version
const { data, isLoading } = useQueryData({
queryKey: ["agent-version"],
url: BASE_URL + API_ENDPOINTS.APP_VERSION.GET_VERSION,
});
2025-09-24 16:13:57 +07:00
const versionList: Version[] = Array.isArray(data)
? data
: data
? [data]
: [];
2025-09-10 09:59:17 +07:00
// Mutation upload
const uploadMutation = useMutationData<FormData>({
url: BASE_URL + API_ENDPOINTS.APP_VERSION.UPLOAD,
method: "POST",
2025-09-24 16:13:57 +07:00
invalidate: [["agent-version"]],
2025-09-10 09:59:17 +07:00
onSuccess: () => toast.success("Upload thành công!"),
2025-09-24 16:13:57 +07:00
onError: (error) => {
console.error("Upload error:", error)
toast.error("Upload thất bại!")
},
2025-09-10 09:59:17 +07:00
});
2025-09-24 16:13:57 +07:00
// Mutation update
2025-09-10 09:59:17 +07:00
const updateMutation = useMutationData<void>({
url: BASE_URL + API_ENDPOINTS.DEVICE_COMM.UPDATE_AGENT,
method: "POST",
onSuccess: () => toast.success("Đã gửi yêu cầu update!"),
onError: () => toast.error("Gửi yêu cầu thất bại!"),
});
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-10 09:59:17 +07:00
];
2025-09-24 16:13:57 +07:00
const handleUpload = async (
fd: FormData,
config?: { onUploadProgress?: (e: AxiosProgressEvent) => void }
) => {
return uploadMutation.mutateAsync({
data: fd,
config
});
};
const handleUpdate = async () => {
return updateMutation.mutateAsync({
data: undefined,
});
};
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-09-24 16:13:57 +07:00
}