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 (
<>
⚠️ 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.
> )}