feat(audiobook): enhance segment polling during project status changes
This commit is contained in:
@@ -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
|
||||||
|
// Always poll segments regardless of expanded state so cards update in real time
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
onRefresh()
|
onRefresh()
|
||||||
if (expanded) { fetchDetail(); fetchSegments() }
|
fetchSegments()
|
||||||
|
if (expanded) fetchDetail()
|
||||||
}, 3000)
|
}, 3000)
|
||||||
return () => clearInterval(interval)
|
return () => clearInterval(interval)
|
||||||
}
|
// expanded intentionally excluded: interval must not reset on expand/collapse
|
||||||
}, [project.status, expanded, onRefresh, fetchDetail, fetchSegments])
|
// 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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user