import * as React from 'react' import { Link, useNavigate, useParams } from 'react-router-dom' import { Check, CircleAlert, LoaderCircle } from 'lucide-react' import type { AnalysisJobStatusDto } from '@persona-lenz/shared' import { Button } from '../components/ui/button' import { getJobStatus, loadLastRequest, requestAnalysis, saveLastRequest } from '../lib/api' import { cn } from '../components/ui/utils' import { SiteFooter } from './site-footer' const POLL_INTERVAL_MS = 2000 const STAGES = [ { id: 'resolve-account', label: '계정 확인' }, { id: 'fetch-match-ids', label: '랭크 게임 목록 조회' }, { id: 'fetch-match-details', label: '매치 데이터 수집' }, { id: 'extract-features', label: '플레이스타일 지표 추출' }, { id: 'build-profile', label: '프로필 생성' }, ] as const function stageIndex(stage?: string): number { const index = STAGES.findIndex((entry) => entry.id === stage) return index < 0 ? 0 : index } export function AnalysisProgressPage() { const { jobId } = useParams<{ jobId: string }>() const navigate = useNavigate() const [job, setJob] = React.useState(null) const [pollError, setPollError] = React.useState(null) const [retrying, setRetrying] = React.useState(false) React.useEffect(() => { if (!jobId) return let cancelled = false let timer: ReturnType | undefined async function poll() { try { const status = await getJobStatus(jobId!) if (cancelled) return setJob(status) setPollError(null) if (status.status === 'completed' && status.puuid) { timer = setTimeout(() => navigate(`/players/${status.puuid}`, { replace: true }), 600) return } if (status.status === 'failed') return } catch (cause) { if (cancelled) return setPollError(cause instanceof Error ? cause.message : '상태를 불러오지 못했어요.') } timer = setTimeout(poll, POLL_INTERVAL_MS) } void poll() return () => { cancelled = true if (timer) clearTimeout(timer) } }, [jobId, navigate]) async function handleRetry() { const lastRequest = loadLastRequest() if (!lastRequest) { navigate('/') return } setRetrying(true) try { const accepted = await requestAnalysis(lastRequest) saveLastRequest(lastRequest) navigate(`/analysis/${accepted.jobId}`, { replace: true }) setJob(null) setRetrying(false) } catch (cause) { setPollError(cause instanceof Error ? cause.message : '재시도 요청에 실패했어요.') setRetrying(false) } } const failed = job?.status === 'failed' const completed = job?.status === 'completed' const currentIndex = completed ? STAGES.length : stageIndex(job?.stage) const { fetchedMatches, totalMatches } = job?.progress ?? { fetchedMatches: 0, totalMatches: 0 } return (
PersonaLenz
{failed ? (

분석을 끝내지 못했어요

{job?.error ?? '알 수 없는 문제가 발생했어요. 잠시 후 다시 시도해 주세요.'}

) : (

{completed ? '분석이 끝났어요!' : '플레이스타일을 분석하고 있어요'}

{completed ? '대시보드로 이동합니다…' : '최근 랭크 게임을 수집해 지표를 계산하는 중이에요. 잠시만 기다려 주세요.'}

    {STAGES.map((stage, index) => { const isDone = index < currentIndex const isActive = index === currentIndex && !completed return (
  1. {isDone ? ( ) : isActive ? ( ) : ( )}

    {stage.label}

    {stage.id === 'fetch-match-details' && isActive && totalMatches > 0 && (

    {fetchedMatches} / {totalMatches} 게임

    )}
  2. ) })}
{pollError && (

{pollError}

)}
)}
) }