import React, { useState } from 'react'; import { Upload, CheckCircle, XCircle, RotateCw, Download } from 'lucide-react'; import { useAgentUpdate } from '@/hooks/useAgentUpdate'; import { Agent } from '@/types'; import { cn } from '@/lib/utils'; import toast from 'react-hot-toast'; interface AgentUpdateProps { agent: Agent; onUpdateComplete?: () => void; className?: string; } export function AgentUpdate({ agent, onUpdateComplete, className }: AgentUpdateProps) { const { checkForUpdate, triggerAgentUpdate, updateStatus, checkingUpdate, updatingAgent, hasUpdate, availableVersion, currentVersion } = useAgentUpdate(); const [isChecking, setIsChecking] = useState(false); const [showConfirmDialog, setShowConfirmDialog] = useState(false); const [hasChecked, setHasChecked] = useState(false); const handleCheckUpdate = async (e: React.MouseEvent) => { e.stopPropagation(); setIsChecking(true); try { await checkForUpdate(agent.id); setHasChecked(true); if (hasUpdate && availableVersion) { setShowConfirmDialog(true); } else if (!hasUpdate && hasChecked) { toast.info('Agent is already at latest version'); } } catch (error) { console.error('[UI] Failed to check for updates:', error); toast.error('Failed to check for available updates'); } finally { setIsChecking(false); } }; const handleConfirmUpdate = async () => { if (!hasUpdate || !availableVersion) { toast.error('No update available'); return; } setShowConfirmDialog(false); try { await triggerAgentUpdate(agent, availableVersion); if (onUpdateComplete) { onUpdateComplete(); } } catch (error) { console.error('[UI] Update failed:', error); } }; const buttonContent = () => { if (updatingAgent) { return ( <> {updateStatus.status === 'downloading' && 'Downloading...'} {updateStatus.status === 'installing' && 'Installing...'} {updateStatus.status === 'pending' && 'Starting update...'} ); } if (agent.is_updating) { return ( <> Updating... ); } if (isChecking) { return ( <> Checking... ); } if (hasChecked && hasUpdate) { return ( <> Update to {availableVersion} ); } return ( <> Check for Update ); }; return (
{/* Progress indicator */} {updatingAgent && updateStatus.progress && (
)} {/* Status icon */} {hasChecked && !updatingAgent && (
{hasUpdate ? ( ) : ( )}
)} {/* Version info popup */} {hasChecked && (
{currentVersion} → {hasUpdate ? availableVersion : 'Latest'}
)} {/* Confirmation Dialog */} {showConfirmDialog && (

Update Agent: {agent.hostname}

{/* Warning for same-version updates */} {currentVersion === availableVersion ? ( <>

⚠️ Version appears identical

Current: {currentVersion} → Target: {availableVersion}

This will reinstall the current version. Useful if the binary was rebuilt or corrupted.

The agent will be temporarily offline during reinstallation.

) : ( <>

Update agent from {currentVersion} to {availableVersion}?

This will temporarily take the agent offline during the update process.

)}
)}
); }