import React, { useState, useEffect } from 'react'; import { X, Copy, Check, Terminal, HardHat, Search, Wand2, CheckCircle2, ArrowLeft, Play } from 'lucide-react'; import { generateCodeWithAgents, AgentStatus } from '../services/geminiService'; interface CodeViewerProps { code: string; // This represents the PROMPT text initially isOpen: boolean; onClose: () => void; isLoading: boolean; } const CodeViewer: React.FC = ({ code, isOpen, onClose, isLoading }) => { const [copied, setCopied] = useState(false); const [viewMode, setViewMode] = useState<'prompt' | 'building' | 'code'>('prompt'); const [finalCode, setFinalCode] = useState(''); const [agentStatus, setAgentStatus] = useState('idle'); const [agentMessage, setAgentMessage] = useState(''); // Reset state when opening/closing or changing input useEffect(() => { if (isOpen) { setViewMode('prompt'); setFinalCode(''); setAgentStatus('idle'); } }, [isOpen, code]); const handleCopy = (text: string) => { navigator.clipboard.writeText(text); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const handleBuildWithAgents = async () => { setViewMode('building'); setAgentStatus('architect'); setAgentMessage('Initializing agents...'); try { const result = await generateCodeWithAgents(code, (status, msg) => { setAgentStatus(status); setAgentMessage(msg); }); setFinalCode(result); // Small delay to show completion state setTimeout(() => { setViewMode('code'); }, 1000); } catch (error) { setAgentStatus('error'); setAgentMessage('Generation failed. Please try again.'); setTimeout(() => setViewMode('prompt'), 2000); } }; const getStepStatus = (step: AgentStatus, current: AgentStatus) => { const order = ['idle', 'architect', 'critic', 'refiner', 'complete']; const stepIdx = order.indexOf(step); const currentIdx = order.indexOf(current); if (current === 'error') return 'text-slate-500'; if (currentIdx > stepIdx) return 'text-emerald-400'; if (currentIdx === stepIdx) return 'text-blue-400 animate-pulse'; return 'text-slate-600'; }; const renderAgentStep = (step: AgentStatus, icon: React.ReactNode, label: string) => { const statusColor = getStepStatus(step, agentStatus); const isCurrent = agentStatus === step; const isDone = ['architect', 'critic', 'refiner', 'complete'].indexOf(agentStatus) > ['architect', 'critic', 'refiner'].indexOf(step); return (
{isDone ? : icon}
{label}
{isCurrent && (
{agentMessage}
)}
{isCurrent &&
}
); }; if (!isOpen) return null; return (
{/* Header */}

{viewMode === 'prompt' && Architecture Prompt} {viewMode === 'building' && Agents Building...} {viewMode === 'code' && Final Code} {isLoading && Building...}

{viewMode === 'code' && ( )} {viewMode !== 'building' && ( )} {viewMode === 'prompt' && !isLoading && ( )}
{/* Content */}
{/* View: Loading Prompt */} {isLoading && viewMode === 'prompt' && (

Refining architecture prompt...

)} {/* View: Prompt Text */} {!isLoading && viewMode === 'prompt' && (
{code}
)} {/* View: Agents Building */} {viewMode === 'building' && (
{renderAgentStep('architect', , "Coder Agent: Writing Implementation")} {renderAgentStep('critic', , "Reviewer Agent: Analyzing Logic")} {renderAgentStep('refiner', , "Polisher Agent: Finalizing Code")}
{agentStatus === 'complete' && (
Done! Loading code...
)}
)} {/* View: Final Code */} {viewMode === 'code' && (
{finalCode}
)}
); }; export default CodeViewer;