91 lines
2.4 KiB
TypeScript
91 lines
2.4 KiB
TypeScript
import { createFileRoute } from "@tanstack/react-router";
|
|
import { AppManagerTemplate } from "@/template/app-manager-template";
|
|
import {
|
|
useGetAgentVersion,
|
|
useGetRoomList,
|
|
useUploadSoftware,
|
|
useUpdateAgent,
|
|
} from "@/hooks/queries";
|
|
import { toast } from "sonner";
|
|
import type { AxiosProgressEvent } from "axios";
|
|
import type { Version } from "@/types/file";
|
|
import { ErrorFetchingPage } from "@/components/pages/error-fetching-page";
|
|
import { agentColumns } from "@/components/columns/agent-column";
|
|
export const Route = createFileRoute("/_auth/agent/")({
|
|
head: () => ({ meta: [{ title: "Quản lý Agent" }] }),
|
|
component: AgentsPage,
|
|
errorComponent: ErrorFetchingPage,
|
|
loader: async ({ context }) => {
|
|
context.breadcrumbs = [
|
|
{ title: "Quản lý Agent", path: "/_auth/agent/" },
|
|
];
|
|
},
|
|
});
|
|
|
|
function AgentsPage() {
|
|
// Lấy danh sách version
|
|
const { data, isLoading } = useGetAgentVersion();
|
|
|
|
// Lấy danh sách phòng
|
|
const { data: roomData } = useGetRoomList();
|
|
|
|
const versionList: Version[] = Array.isArray(data)
|
|
? data
|
|
: data
|
|
? [data]
|
|
: [];
|
|
|
|
const uploadMutation = useUploadSoftware();
|
|
|
|
const updateMutation = useUpdateAgent();
|
|
|
|
const handleUpload = async (
|
|
fd: FormData,
|
|
config?: { onUploadProgress?: (e: AxiosProgressEvent) => void },
|
|
) => {
|
|
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!");
|
|
}
|
|
};
|
|
|
|
const handleUpdate = async (roomNames: string[]) => {
|
|
try {
|
|
for (const roomName of roomNames) {
|
|
await updateMutation.mutateAsync({
|
|
roomName,
|
|
data: {},
|
|
});
|
|
}
|
|
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!");
|
|
}
|
|
};
|
|
|
|
// Cột bảng
|
|
|
|
|
|
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={agentColumns}
|
|
onUpload={handleUpload}
|
|
onUpdate={handleUpdate}
|
|
updateLoading={updateMutation.isPending}
|
|
rooms={roomData}
|
|
enablePagination
|
|
defaultPageSize={10}
|
|
/>
|
|
);
|
|
}
|