195 lines
8.0 KiB
TypeScript
195 lines
8.0 KiB
TypeScript
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<AnalysisJobStatusDto | null>(null)
|
|
const [pollError, setPollError] = React.useState<string | null>(null)
|
|
const [retrying, setRetrying] = React.useState(false)
|
|
|
|
React.useEffect(() => {
|
|
if (!jobId) return
|
|
let cancelled = false
|
|
let timer: ReturnType<typeof setTimeout> | 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 (
|
|
<div className="min-h-dvh flex flex-col bg-surface-page">
|
|
<header className="px-6 py-5">
|
|
<Link to="/" className="mx-auto flex w-full max-w-2xl items-center gap-2">
|
|
<span aria-hidden className="size-2 rounded-full bg-brand" />
|
|
<span className="text-[17px] font-bold text-text-primary">PersonaLenz</span>
|
|
</Link>
|
|
</header>
|
|
|
|
<main className="flex flex-1 items-center px-6 pb-16">
|
|
<div className="mx-auto w-full max-w-xl">
|
|
{failed ? (
|
|
<div className="rounded-2xl border border-border bg-card p-8 shadow-[var(--shadow-card)]">
|
|
<CircleAlert aria-hidden className="size-8 text-destructive" />
|
|
<h1 className="mt-4 text-[22px] font-bold text-text-primary">분석을 끝내지 못했어요</h1>
|
|
<p className="mt-2 text-[15px] leading-relaxed text-text-secondary">
|
|
{job?.error ?? '알 수 없는 문제가 발생했어요. 잠시 후 다시 시도해 주세요.'}
|
|
</p>
|
|
<div className="mt-6 flex gap-3">
|
|
<Button onClick={handleRetry} disabled={retrying} className="bg-brand text-white hover:bg-brand/90">
|
|
{retrying ? '다시 요청하는 중…' : '다시 분석하기'}
|
|
</Button>
|
|
<Button variant="outline" asChild>
|
|
<Link to="/">홈으로</Link>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div className="rounded-2xl border border-border bg-card p-8 shadow-[var(--shadow-card)]">
|
|
<h1 className="text-[22px] font-bold text-text-primary">
|
|
{completed ? '분석이 끝났어요!' : '플레이스타일을 분석하고 있어요'}
|
|
</h1>
|
|
<p className="mt-2 text-[15px] text-text-secondary">
|
|
{completed
|
|
? '대시보드로 이동합니다…'
|
|
: '최근 랭크 게임을 수집해 지표를 계산하는 중이에요. 잠시만 기다려 주세요.'}
|
|
</p>
|
|
|
|
<ol className="mt-8 space-y-5">
|
|
{STAGES.map((stage, index) => {
|
|
const isDone = index < currentIndex
|
|
const isActive = index === currentIndex && !completed
|
|
return (
|
|
<li key={stage.id} className="flex items-center gap-4">
|
|
<span
|
|
className={cn(
|
|
'flex size-8 shrink-0 items-center justify-center rounded-full border',
|
|
isDone && 'border-brand bg-brand text-white',
|
|
isActive && 'border-brand text-brand',
|
|
!isDone && !isActive && 'border-border text-text-disabled',
|
|
)}
|
|
>
|
|
{isDone ? (
|
|
<Check aria-hidden className="size-4" />
|
|
) : isActive ? (
|
|
<LoaderCircle aria-hidden className="size-4 animate-spin" />
|
|
) : (
|
|
<span aria-hidden className="size-1.5 rounded-full bg-current" />
|
|
)}
|
|
</span>
|
|
<div className="min-w-0 flex-1">
|
|
<p
|
|
className={cn(
|
|
'text-[15px] font-medium',
|
|
isDone || isActive ? 'text-text-primary' : 'text-text-tertiary',
|
|
)}
|
|
>
|
|
{stage.label}
|
|
</p>
|
|
{stage.id === 'fetch-match-details' && isActive && totalMatches > 0 && (
|
|
<div className="mt-2">
|
|
<div
|
|
role="progressbar"
|
|
aria-valuemin={0}
|
|
aria-valuemax={totalMatches}
|
|
aria-valuenow={fetchedMatches}
|
|
aria-label="매치 수집 진행률"
|
|
className="h-1.5 w-full overflow-hidden rounded-full bg-surface-muted"
|
|
>
|
|
<div
|
|
className="h-full rounded-full bg-brand transition-[width] duration-500"
|
|
style={{ width: `${Math.round((fetchedMatches / totalMatches) * 100)}%` }}
|
|
/>
|
|
</div>
|
|
<p className="mt-1.5 text-[13px] tabular-nums text-text-tertiary">
|
|
{fetchedMatches} / {totalMatches} 게임
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</li>
|
|
)
|
|
})}
|
|
</ol>
|
|
|
|
{pollError && (
|
|
<p role="alert" className="mt-6 text-[14px] text-destructive">
|
|
{pollError}
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</main>
|
|
|
|
<SiteFooter />
|
|
</div>
|
|
)
|
|
}
|