import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { AlertCircle, CheckCircle, Clock, RefreshCw } from "lucide-react"; import api from "../../utils/api"; const AgentManagementTab = () => { const _queryClient = useQueryClient(); // Agent version queries const { data: versionInfo, isLoading: versionLoading, error: versionError, refetch: refetchVersion, } = useQuery({ queryKey: ["agentVersion"], queryFn: async () => { try { const response = await api.get("/agent/version"); console.log("🔍 Frontend received version info:", response.data); return response.data; } catch (error) { console.error("Failed to fetch version info:", error); throw error; } }, refetchInterval: 5 * 60 * 1000, // Refetch every 5 minutes enabled: true, // Always enabled retry: 3, // Retry failed requests }); const { data: _availableVersions, isLoading: _versionsLoading, error: _versionsError, } = useQuery({ queryKey: ["agentVersions"], queryFn: async () => { try { const response = await api.get("/agent/versions"); console.log("🔍 Frontend received available versions:", response.data); return response.data; } catch (error) { console.error("Failed to fetch available versions:", error); throw error; } }, enabled: true, retry: 3, }); const checkUpdatesMutation = useMutation({ mutationFn: async () => { // First check GitHub for updates await api.post("/agent/version/check"); // Then refresh current agent version detection await api.post("/agent/version/refresh"); }, onSuccess: () => { refetchVersion(); }, onError: (error) => { console.error("Check updates error:", error); }, }); const downloadUpdateMutation = useMutation({ mutationFn: async () => { // Download the latest binaries const downloadResult = await api.post("/agent/version/download"); // Refresh current agent version detection after download await api.post("/agent/version/refresh"); // Return the download result for success handling return downloadResult; }, onSuccess: (data) => { console.log("Download completed:", data); console.log("Download response data:", data.data); refetchVersion(); // Show success message const message = data.data?.message || "Agent binaries downloaded successfully"; alert(`✅ ${message}`); }, onError: (error) => { console.error("Download update error:", error); alert(`❌ Download failed: ${error.message}`); }, }); const getVersionStatus = () => { console.log("🔍 getVersionStatus called with:", { versionError, versionInfo, versionLoading, }); if (versionError) { console.log("❌ Version error detected:", versionError); return { status: "error", message: "Failed to load version info", Icon: AlertCircle, color: "text-red-600", }; } if (!versionInfo || versionLoading) { console.log("⏳ Loading state:", { versionInfo, versionLoading }); return { status: "loading", message: "Loading version info...", Icon: RefreshCw, color: "text-gray-600", }; } // Use the backend's updateStatus for proper semver comparison switch (versionInfo.updateStatus) { case "update-available": return { status: "update-available", message: `Update available: ${versionInfo.latestVersion}`, Icon: Clock, color: "text-yellow-600", }; case "newer-version": return { status: "newer-version", message: `Newer version running: ${versionInfo.currentVersion}`, Icon: CheckCircle, color: "text-blue-600", }; case "up-to-date": return { status: "up-to-date", message: `Up to date: ${versionInfo.latestVersion}`, Icon: CheckCircle, color: "text-green-600", }; case "no-agent": return { status: "no-agent", message: "No agent binary found", Icon: AlertCircle, color: "text-orange-600", }; case "github-unavailable": return { status: "github-unavailable", message: `Agent running: ${versionInfo.currentVersion} (GitHub API unavailable)`, Icon: CheckCircle, color: "text-purple-600", }; case "no-data": return { status: "no-data", message: "No version data available", Icon: AlertCircle, color: "text-gray-600", }; default: return { status: "unknown", message: "Version status unknown", Icon: AlertCircle, color: "text-gray-600", }; } }; const versionStatus = getVersionStatus(); const StatusIcon = versionStatus.Icon; return (
{/* Header */}

Agent Version Management

Monitor agent versions and download updates

{/* Download Updates Button */}

{versionInfo?.currentVersion ? "Download Agent Updates" : "Download Agent Binaries"}

{versionInfo?.currentVersion ? "Download the latest agent binaries from GitHub" : "No agent binaries found. Download from GitHub to get started."}

{/* Version Status Card */}

Agent Version Status

{StatusIcon && ( )} {versionStatus.message}
{versionInfo && (
Current Version: {versionInfo.currentVersion || "Unknown"}
Latest Version: {versionInfo.latestVersion || "Unknown"}
Last Checked: {versionInfo.lastChecked ? new Date(versionInfo.lastChecked).toLocaleString() : "Never"}
)}
); }; export default AgentManagementTab;