메인페이지 원복시키기
This commit is contained in:
1 parent
c07ef255d4
commit
5f7e0051cd
9 files changed
+178
-123
No files matched your search
@@ -2,16 +2,25 @@ import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { useNavigate, useLocation, useParams } from 'react-router';
|
||||
import { useCreateClinic } from '@/shared/api/generated/clinics/clinics';
|
||||
import { useStartAnalysis, getAnalysisStatus } from '@/shared/api/generated/analyses/analyses';
|
||||
import { getReport } from '@/shared/api/generated/reports/reports';
|
||||
import { AnalysisStatus } from '@/shared/api/model/analysisStatus';
|
||||
import { sleep } from '@/shared/lib/utils';
|
||||
|
||||
// AnalyzePayload(MultiChannelInput) 와 호환되도록 string | string[] 둘 다 허용.
|
||||
// 백엔드 SDK 는 단일 string 만 받으므로 내부에서 첫 요소만 추출.
|
||||
export type ManualChannels = {
|
||||
instagram?: string;
|
||||
youtube?: string;
|
||||
facebook?: string;
|
||||
naverBlog?: string;
|
||||
gangnamUnni?: string;
|
||||
instagram?: string | string[];
|
||||
youtube?: string | string[];
|
||||
facebook?: string | string[];
|
||||
naverBlog?: string | string[];
|
||||
gangnamUnni?: string | string[];
|
||||
};
|
||||
|
||||
function pickFirst(v: string | string[] | undefined): string | null {
|
||||
if (!v) return null;
|
||||
if (Array.isArray(v)) return v[0] ?? null;
|
||||
return v;
|
||||
}
|
||||
|
||||
export type Phase =
|
||||
| 'resuming'
|
||||
| 'discovering'
|
||||
@@ -47,22 +56,9 @@ function clearSession() {
|
||||
Object.values(SESSION_KEYS).forEach((k) => sessionStorage.removeItem(k));
|
||||
}
|
||||
|
||||
// 백엔드 AnalysisStatus enum: discovering | collecting | analyzing | completed | failed
|
||||
// UI Phase 스텝과 매핑
|
||||
function phaseFromStatus(s: string): Phase {
|
||||
if (s === 'discovering') return 'collecting';
|
||||
if (s === 'collecting') return 'generating';
|
||||
if (s === 'analyzing') return 'planning';
|
||||
if (s === 'completed') return 'complete';
|
||||
return 'discovering';
|
||||
}
|
||||
|
||||
function resumePhaseFromStatus(s: string): Phase {
|
||||
if (s === 'discovering') return 'collecting';
|
||||
if (s === 'collecting') return 'generating';
|
||||
if (s === 'analyzing') return 'planning';
|
||||
return 'discovering';
|
||||
}
|
||||
// 백엔드 AnalysisStatus 4단계를 UI 5단계로 매핑.
|
||||
// analyzing 한 단계는 길어서 generating → planning 으로 시간 기반 분할.
|
||||
const ANALYZING_SPLIT_MS = 30_000;
|
||||
|
||||
interface UseAnalysisPipelineResult {
|
||||
phase: Phase;
|
||||
@@ -98,13 +94,26 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
const runPipeline = useCallback(
|
||||
async (
|
||||
startUrl?: string,
|
||||
resumeFrom?: { reportId: string; clinicId?: string; runId?: string; phase: Phase },
|
||||
resumeFrom?: { reportId: string; clinicId?: string; runId?: string },
|
||||
) => {
|
||||
try {
|
||||
let reportId = resumeFrom?.reportId || '';
|
||||
let clinicId = resumeFrom?.clinicId;
|
||||
let runId = resumeFrom?.runId;
|
||||
|
||||
// analyzing 진입 시각을 기록해서 generating → planning 시점을 시간으로 판단
|
||||
let analyzingStartedAt: number | null = null;
|
||||
const mapPhase = (s: AnalysisStatus): Phase => {
|
||||
if (s === AnalysisStatus.discovering) return 'discovering';
|
||||
if (s === AnalysisStatus.collecting) return 'collecting';
|
||||
if (s === AnalysisStatus.analyzing) {
|
||||
if (analyzingStartedAt === null) analyzingStartedAt = Date.now();
|
||||
return Date.now() - analyzingStartedAt > ANALYZING_SPLIT_MS ? 'planning' : 'generating';
|
||||
}
|
||||
if (s === AnalysisStatus.completed) return 'complete';
|
||||
return 'discovering';
|
||||
};
|
||||
|
||||
// Phase 1: 신규 분석이면 createClinic → startAnalysis
|
||||
if (!resumeFrom?.runId) {
|
||||
if (!startUrl) throw new Error('No URL provided');
|
||||
@@ -120,11 +129,11 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
data: {
|
||||
clinic_id: clinicId,
|
||||
channels: {
|
||||
youtube: manualChannels?.youtube ?? null,
|
||||
instagram: manualChannels?.instagram ?? null,
|
||||
facebook: manualChannels?.facebook ?? null,
|
||||
naver_blog: manualChannels?.naverBlog ?? null,
|
||||
gangnam_unni: manualChannels?.gangnamUnni ?? null,
|
||||
youtube: pickFirst(manualChannels?.youtube),
|
||||
instagram: pickFirst(manualChannels?.instagram),
|
||||
facebook: pickFirst(manualChannels?.facebook),
|
||||
naver_blog: pickFirst(manualChannels?.naverBlog),
|
||||
gangnam_unni: pickFirst(manualChannels?.gangnamUnni),
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -146,40 +155,20 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
const statusRes = await getAnalysisStatus(runId);
|
||||
if (statusRes.status !== 200) throw new Error('분석 상태 조회에 실패했습니다.');
|
||||
const statusData = statusRes.data;
|
||||
setPhase(phaseFromStatus(statusData.status));
|
||||
setPhase(mapPhase(statusData.status));
|
||||
if (statusData.channel_errors && Object.keys(statusData.channel_errors).length > 0) {
|
||||
console.warn('[pipeline] Partial failures:', statusData.channel_errors);
|
||||
}
|
||||
if (statusData.status === 'completed') break;
|
||||
if (statusData.status === 'failed') {
|
||||
if (statusData.status === AnalysisStatus.completed) break;
|
||||
if (statusData.status === AnalysisStatus.failed) {
|
||||
throw new Error('파이프라인 실패: failed');
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
await sleep(1500);
|
||||
}
|
||||
|
||||
setPhase('complete');
|
||||
clearSession();
|
||||
|
||||
// 최종 리포트 조회 (best effort)
|
||||
const reportRes = await getReport(reportId).catch(() => null);
|
||||
const reportPayload = reportRes && reportRes.status === 200 ? reportRes.data : null;
|
||||
setTimeout(() => {
|
||||
navigate(`/report/${reportId}`, {
|
||||
replace: true,
|
||||
state: reportPayload
|
||||
? {
|
||||
report: reportPayload,
|
||||
metadata: {
|
||||
url: startUrl,
|
||||
clinicName: '',
|
||||
generatedAt: new Date().toISOString(),
|
||||
},
|
||||
reportId,
|
||||
clinicId,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
}, 800);
|
||||
navigate(`/report/${reportId}`, { replace: true });
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : 'An error occurred';
|
||||
setError(msg);
|
||||
@@ -203,13 +192,12 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
reportId: session.reportId,
|
||||
clinicId: session.clinicId || undefined,
|
||||
runId: session.runId || undefined,
|
||||
phase,
|
||||
});
|
||||
} else if (url || session.url) {
|
||||
hasStarted.current = false;
|
||||
runPipeline(url || session.url || undefined);
|
||||
}
|
||||
}, [phase, url, runPipeline]);
|
||||
}, [url, runPipeline]);
|
||||
|
||||
const handleAbort = useCallback(() => {
|
||||
clearSession();
|
||||
@@ -229,11 +217,11 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
.then((res) => {
|
||||
if (res.status !== 200) throw new Error('status fetch failed');
|
||||
const status = res.data;
|
||||
if (status.status === 'completed') {
|
||||
if (status.status === AnalysisStatus.completed) {
|
||||
navigate(`/report/${urlReportId}`, { replace: true });
|
||||
return;
|
||||
}
|
||||
if (status.status === 'failed') {
|
||||
if (status.status === AnalysisStatus.failed) {
|
||||
setError('Data collection failed. Please retry.');
|
||||
setPhase('collecting');
|
||||
return;
|
||||
@@ -248,7 +236,6 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
reportId: urlReportId,
|
||||
clinicId: session.clinicId || undefined,
|
||||
runId,
|
||||
phase: resumePhaseFromStatus(status.status),
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -266,7 +253,7 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
.then((res) => {
|
||||
if (res.status !== 200) throw new Error('status fetch failed');
|
||||
const status = res.data;
|
||||
if (status.status === 'completed') {
|
||||
if (status.status === AnalysisStatus.completed) {
|
||||
clearSession();
|
||||
navigate(`/report/${session.reportId}`, { replace: true });
|
||||
return;
|
||||
@@ -275,7 +262,6 @@ export function useAnalysisPipeline(): UseAnalysisPipelineResult {
|
||||
reportId: session.reportId!,
|
||||
clinicId: session.clinicId || undefined,
|
||||
runId,
|
||||
phase: resumePhaseFromStatus(status.status),
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
|
||||
Reference in new issue
Block a user