import { createFileRoute } from "@tanstack/react-router"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useQueryData } from "@/hooks/useQueryData"; import { useMutationData } from "@/hooks/useMutationData"; import { formOptions, useForm } from "@tanstack/react-form"; import { toast } from "sonner"; import { Label } from "@/components/ui/label"; import { useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { FileText, Plus } from "lucide-react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Progress } from "@/components/ui/progress"; import { BASE_URL, API_ENDPOINTS } from "@/config/api"; interface UploadAppFormProps { files: FileList; newVersion: string; } const defaultInput: UploadAppFormProps = { files: new DataTransfer().files, newVersion: "", }; const formOpts = formOptions({ defaultValues: defaultInput, }); export const Route = createFileRoute("/_authenticated/apps/")({ head: () => ({ meta: [{ title: "Quản lý Agent" }], }), component: AppsComponent, }); function AppsComponent() { const { data: versionData, isLoading } = useQueryData({ queryKey: ["app-version"], url: BASE_URL + API_ENDPOINTS.APP_VERSION.GET_VERSION, }); const versionList = Array.isArray(versionData) ? versionData : versionData ? [versionData] : []; const [isUploadOpen, setIsUploadOpen] = useState(false); const [uploadPercent, setUploadPercent] = useState(0); const uploadMutation = useMutationData({ url: BASE_URL + API_ENDPOINTS.APP_VERSION.UPLOAD, method: "POST", config: { onUploadProgress: (e: ProgressEvent) => { if (e.total) { const percent = Math.round((e.loaded * 100) / e.total); setUploadPercent(percent); } }, }, onSuccess: () => { toast.success("Cập nhật thành công!"); setUploadPercent(0); form.reset(); setIsUploadOpen(false); }, onError: () => toast.error("Lỗi khi cập nhật phiên bản!"), }); const updateAgentMutation = useMutationData({ url: BASE_URL + API_ENDPOINTS.DEVICE_COMM.UPDATE_AGENT, method: "POST", onSuccess: () => toast.success("Đã gửi yêu cầu cập nhật đến thiết bị!"), onError: () => toast.error("Gửi yêu cầu thất bại!"), }); const form = useForm({ ...formOpts, onSubmit: async ({ value }) => { const typedValue = value as UploadAppFormProps; if (!typedValue.newVersion || typedValue.files.length === 0) { toast.error("Vui lòng điền đầy đủ thông tin"); return; } const formData = new FormData(); Array.from(typedValue.files).forEach((file) => formData.append("files", file) ); formData.append("newVersion", typedValue.newVersion); await uploadMutation.mutateAsync(formData); }, }); return (

Quản lý Agent

Quản lý và theo dõi các phiên bản Agent

Cập nhật phiên bản mới Chọn tệp và nhập số phiên bản
{(field) => (
field.handleChange(e.target.value)} placeholder="e.g., 1.0.0" />
)}
{(field) => (
{ if (e.target.files) { field.handleChange(e.target.files); } }} />
)}
{uploadPercent > 0 && (
{uploadPercent}%
)}
Lịch sử phiên bản Tất cả các phiên bản đã tải lên của Agent Phiên bản Tên tệp Đường dẫn thư mục Thời gian cập nhật {isLoading ? ( Đang tải dữ liệu... ) : versionList.length === 0 ? ( Không có dữ liệu phiên bản. ) : ( versionList.map((v: any) => ( {v.version} {v.fileName} {v.folderPath} {v.updatedAt ? new Date(v.updatedAt).toLocaleString("vi-VN") : "N/A"} )) )}
); }