import React, { useEffect, useState, useRef, useCallback } from 'react'; import { createPortal } from 'react-dom'; import { useLanguage } from '../i18n/LanguageContext'; import { MessageKey } from '../i18n/translations'; interface GenerationProgressProps { progress: number; message: string; step?: string; } const STEP_ICONS: Record = { analyzing: '🔍', connecting: '🔗', generating: '🤖', parsing: '📋', saving: '💾', building: '🏗️', }; const STEP_KEYS: Record = { analyzing: 'stepAnalyzing', connecting: 'stepConnecting', generating: 'stepGenerating', parsing: 'stepParsing', saving: 'stepSaving', building: 'stepBuilding', }; const TICK_MS = 200; const SIM_CEILING = 99; const GENERATE_DURATION_MS = 120000; const GenerationProgress: React.FC = ({ progress, message, step }) => { const { t } = useLanguage(); const [displayProgress, setDisplayProgress] = useState(0); const [dots, setDots] = useState(''); const realProgressRef = useRef(progress); const stepRef = useRef(step); const simStartRef = useRef(Date.now()); const simBaseRef = useRef(0); useEffect(() => { stepRef.current = step; if (progress > realProgressRef.current) { realProgressRef.current = progress; } }, [progress, step]); useEffect(() => { simStartRef.current = Date.now(); simBaseRef.current = 0; }, []); useEffect(() => { const timer = setInterval(() => { setDisplayProgress(prev => { const real = realProgressRef.current; if (real >= 100) return 100; if (prev < real) { const diff = real - prev; const catchUp = Math.max(diff * 0.3, 2); return Math.min(prev + catchUp, real); } if (prev < SIM_CEILING) { const elapsed = Date.now() - simStartRef.current; const ratio = Math.min(elapsed / GENERATE_DURATION_MS, 1); const eased = 1 - Math.pow(1 - ratio, 3); const simulated = simBaseRef.current + (SIM_CEILING - simBaseRef.current) * eased; return Math.min(Math.max(simulated, prev + 0.05), SIM_CEILING); } return prev; }); }, TICK_MS); return () => clearInterval(timer); }, []); useEffect(() => { const timer = setInterval(() => { setDots(prev => prev.length >= 3 ? '' : prev + '.'); }, 500); return () => clearInterval(timer); }, []); useEffect(() => { if (displayProgress >= realProgressRef.current && displayProgress < SIM_CEILING) { simStartRef.current = Date.now(); simBaseRef.current = displayProgress; } }, [displayProgress]); const steps = ['analyzing', 'connecting', 'generating', 'parsing', 'saving', 'building']; const currentStepIndex = step ? steps.indexOf(step) : -1; const roundedProgress = Math.round(displayProgress); return createPortal(
{step && STEP_ICONS[step] ? STEP_ICONS[step] : '🚀'}

{t('generatingTitle')}

{message}{dots}

{roundedProgress}%
{steps.map((s, i) => { const isCompleted = i < currentStepIndex; const isCurrent = i === currentStepIndex; return (
{isCompleted ? '✓' : STEP_ICONS[s]}
{t(STEP_KEYS[s])}
); })}
, document.body ); }; export default GenerationProgress;