feat(audiobook): enhance segment polling during project status changes

This commit is contained in:
2026-03-09 12:03:26 +08:00
parent f20b250430
commit 109ec25246

View File

@@ -268,24 +268,34 @@ function ProjectCard({ project, onRefresh }: { project: AudiobookProject; onRefr
setExpanded(true) setExpanded(true)
autoExpandedRef.current = true autoExpandedRef.current = true
} }
}, [project.status]) // When backend enters an active state, immediately sync segments
if (['analyzing', 'generating'].includes(project.status)) {
fetchSegments()
}
}, [project.status, fetchSegments])
useEffect(() => { useEffect(() => {
if (['analyzing', 'generating'].includes(project.status)) { if (!['analyzing', 'generating'].includes(project.status)) return
const interval = setInterval(() => { // Always poll segments regardless of expanded state so cards update in real time
onRefresh() const interval = setInterval(() => {
if (expanded) { fetchDetail(); fetchSegments() } onRefresh()
}, 3000) fetchSegments()
return () => clearInterval(interval) if (expanded) fetchDetail()
} }, 3000)
}, [project.status, expanded, onRefresh, fetchDetail, fetchSegments]) return () => clearInterval(interval)
// expanded intentionally excluded: interval must not reset on expand/collapse
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [project.status, onRefresh, fetchDetail, fetchSegments])
const handleAnalyze = async () => { const handleAnalyze = async () => {
setLoadingAction(true) setLoadingAction(true)
try { try {
await audiobookApi.analyze(project.id) await audiobookApi.analyze(project.id)
toast.success('分析已开始') toast.success('分析已开始')
// Backend sets status in a background task; poll a few times to catch the transition
onRefresh() onRefresh()
setTimeout(onRefresh, 800)
setTimeout(onRefresh, 2000)
} catch (e: any) { } catch (e: any) {
toast.error(formatApiError(e)) toast.error(formatApiError(e))
} finally { } finally {
@@ -298,7 +308,12 @@ function ProjectCard({ project, onRefresh }: { project: AudiobookProject; onRefr
try { try {
await audiobookApi.generate(project.id) await audiobookApi.generate(project.id)
toast.success('生成已开始') toast.success('生成已开始')
// Backend sets status in a background task; poll quickly to catch the transition
// and start fetching segments as soon as the first ones finish
onRefresh() onRefresh()
setTimeout(() => { onRefresh(); fetchSegments() }, 800)
setTimeout(() => { onRefresh(); fetchSegments() }, 2000)
setTimeout(() => { onRefresh(); fetchSegments() }, 4000)
} catch (e: any) { } catch (e: any) {
toast.error(formatApiError(e)) toast.error(formatApiError(e))
} finally { } finally {