Repository navigation
feat: 위험도 분석 비동기 응답 형태에 맞춘 타입·결과 상세·이어받기 보완 #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5aec3de
c398f0a
b1dab42
e39fe2d
4afaaeb
b3696ce
2b9754f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,13 @@ | |
|
|
||
| import { useQuery } from "@tanstack/react-query"; | ||
| import Link from "next/link"; | ||
| import { getAssessment } from "@/entities/analysis/api"; | ||
| import { getAssessment, isCompletedAssessment } from "@/entities/analysis/api"; | ||
| import { mapAssessment } from "@/entities/analysis/model"; | ||
| import { shareAnalysisResult } from "@/features/analysis-share/shareAnalysisResult"; | ||
| import Arrow from "@/shared/asset/svg/Arrow"; | ||
| import Share from "@/shared/asset/svg/Share"; | ||
| import BackButton from "@/shared/ui/BackButton"; | ||
| import LinkButton from "@/shared/ui/LinkButton"; | ||
| import { useIsLoggedIn } from "@/shared/lib/useIsLoggedIn"; | ||
| import Footer from "@/widgets/Footer"; | ||
| import SiteHeader from "@/widgets/SiteHeader"; | ||
|
|
@@ -41,9 +42,11 @@ export default function AnalysisResultDetailPage({ id }: AnalysisResultDetailPag | |
| queryKey: ["riskAssessments", assessmentId], | ||
| queryFn: () => getAssessment(assessmentId), | ||
| enabled: Number.isFinite(assessmentId), | ||
| refetchInterval: (query) => (query.state.data?.status === "PROCESSING" ? 3000 : false), | ||
| }); | ||
|
|
||
| const result = assessment ? mapAssessment(assessment, locale) : null; | ||
| const result = | ||
| assessment && isCompletedAssessment(assessment) ? mapAssessment(assessment, locale) : null; | ||
|
|
||
| return ( | ||
| <> | ||
|
|
@@ -102,6 +105,28 @@ export default function AnalysisResultDetailPage({ id }: AnalysisResultDetailPag | |
| </p> | ||
| )} | ||
|
|
||
| {assessment && !result && ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 분기는
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| <div className="flex flex-col items-start gap-3 rounded-2xl border border-gray-300 bg-white p-5 md:p-8"> | ||
| {assessment.status === "PROCESSING" ? ( | ||
| <> | ||
| <p className="text-body-1 font-semibold text-black">{t.processingTitle}</p> | ||
| <p className="text-body-2 text-gray-700">{t.processingDescription}</p> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <p className="text-body-1 font-semibold text-black">{t.failedTitle}</p> | ||
| <p className="text-body-2 text-gray-700"> | ||
| {(assessment.failureReason && t.failure[assessment.failureReason]) || | ||
| t.failedFallback} | ||
| </p> | ||
| <LinkButton href="/analyze" variant="primary" className="mt-2"> | ||
| {t.analyzeAgain} | ||
| </LinkButton> | ||
| </> | ||
| )} | ||
| </div> | ||
| )} | ||
|
|
||
| {assessment && result && ( | ||
| <> | ||
| <div className="grid grid-cols-1 items-start gap-6 md:grid-cols-2 lg:gap-8"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { toast } from "sonner"; | |
| import { | ||
| createRiskAssessment, | ||
| getAssessment, | ||
| isCompletedAssessment, | ||
| type RiskAssessmentResponse, | ||
| } from "@/entities/analysis/api"; | ||
| import { mapAssessment, type AnalysisResult } from "@/entities/analysis/model"; | ||
|
|
@@ -29,7 +30,7 @@ function needsLogin(error: unknown) { | |
| ); | ||
| } | ||
|
|
||
| const ASSESSMENT_POLL = { intervalMs: 1500, timeoutMs: 120_000 }; | ||
| const ASSESSMENT_POLL = { intervalMs: 2000, timeoutMs: 330_000 }; | ||
|
|
||
| export function useAnalyzeFlow() { | ||
| const t = useT(analyzeMessages); | ||
|
|
@@ -44,7 +45,8 @@ export function useAnalyzeFlow() { | |
| const [isProtected, setIsProtected] = useState(initial.isProtected); | ||
| const [isSimulatedProtection, setIsSimulatedProtection] = useState(initial.isSimulatedProtection); | ||
| const [assessment, setAssessment] = useState<RiskAssessmentResponse | null>(initial.assessment); | ||
| const result: AnalysisResult | null = assessment ? mapAssessment(assessment, locale) : null; | ||
| const result: AnalysisResult | null = | ||
| assessment && isCompletedAssessment(assessment) ? mapAssessment(assessment, locale) : null; | ||
| const [assessmentId, setAssessmentId] = useState<number | null>(initial.assessmentId); | ||
| const [protectionId, setProtectionId] = useState<number | null>(initial.protectionId); | ||
| const analyzeAbortRef = useRef<AbortController | null>(null); | ||
|
|
@@ -123,16 +125,22 @@ export function useAnalyzeFlow() { | |
| } | ||
|
|
||
| const analyzeMutation = useMutation({ | ||
| mutationFn: async () => { | ||
| if (!selectedFile) throw new Error(t.flow.selectImage); | ||
| mutationFn: async (resumeId: number | null) => { | ||
| cancelAnalysis(); | ||
| const controller = new AbortController(); | ||
| analyzeAbortRef.current = controller; | ||
| const created = await createRiskAssessment(selectedFile); | ||
| if (controller.signal.aborted) throw new DOMException("Analysis aborted", "AbortError"); | ||
| if (created.status !== "PROCESSING") return created; | ||
| let pendingId = resumeId; | ||
| if (pendingId === null) { | ||
| if (!selectedFile) throw new Error(t.flow.selectImage); | ||
| const created = await createRiskAssessment(selectedFile); | ||
| if (controller.signal.aborted) throw new DOMException("Analysis aborted", "AbortError"); | ||
| if (created.status !== "PROCESSING") return created; | ||
| pendingId = created.assessmentId; | ||
| setAssessmentId(pendingId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 저장한
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| const assessmentToPoll = pendingId; | ||
| return pollUntil( | ||
| () => getAssessment(created.assessmentId), | ||
| () => getAssessment(assessmentToPoll), | ||
| (polled) => polled.status !== "PROCESSING", | ||
| { ...ASSESSMENT_POLL, signal: controller.signal }, | ||
| ); | ||
|
|
@@ -161,6 +169,14 @@ export function useAnalyzeFlow() { | |
| }, | ||
| }); | ||
|
|
||
| const { mutate: analyze } = analyzeMutation; | ||
|
|
||
| useEffect(() => { | ||
| if (initial.status === "analyzing" && initial.assessmentId !== null) { | ||
| analyze(initial.assessmentId); | ||
| } | ||
| }, [analyze, initial.status, initial.assessmentId]); | ||
|
|
||
| const protectMutation = useMutation({ | ||
| mutationFn: async () => { | ||
| if (!assessmentId) throw new Error(t.flow.analyzeFirst); | ||
|
|
@@ -210,7 +226,7 @@ export function useAnalyzeFlow() { | |
|
|
||
| function handleAnalyze() { | ||
| setStatus("analyzing"); | ||
| analyzeMutation.mutate(); | ||
| analyzeMutation.mutate(null); | ||
| } | ||
|
|
||
| function handleProtect() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PROCESSING인 동안 3초마다 계속 조회하고 멈추는 조건이 없습니다. 서버가 5분 안에 반드시FAILED로 바꿔 준다면 괜찮지만, 서버가 멈추거나 상태가 영영 바뀌지 않으면 이 페이지를 열어 둔 동안 무한히 조회합니다. 분석 흐름의 조회 한도(5분 30초)와 비슷하게query.state.dataUpdatedCount나 시작 시각으로 상한을 두는 것을 고려해 주세요.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b3696ce커밋에서 반영했습니다. 3초 간격으로 최대 110번(약 5분 30초, 분석 흐름의 조회 한도와 같음)까지만 다시 조회하고 그 뒤에는 멈춥니다(query.state.dataUpdateCount기준).