fix(video): transcribe archived videos and show progress (#560)

This commit is contained in:
Yiling-J
2026-12-04 15:37:58 +08:00
committed by GitHub
parent d3899d1257
commit 72a7f68c92
21 changed files with 408 additions and 171 deletions
@@ -0,0 +1,48 @@
import { useMemo } from 'react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import { client } from '@/ui/api/client'
import { m } from '@/ui/paraglide/messages.js'
import { savedTranscriptLanguagesKey } from './use-video-transcript'
export function useTranscriptionJobs(projectId: string, enabled: boolean) {
const queryClient = useQueryClient()
const queryKey = ['project-transcription-status', projectId] as const
const { data } = useQuery({
queryKey,
enabled,
// Folder routes can remount the browser. Reuse the project's cached jobs on navigation.
refetchOnMount: false,
queryFn: async ({ signal }) => {
const response = await client.api.projects[':projectId']['transcription-status'].$get(
{ param: { projectId } },
{ init: { signal } },
)
if (!response.ok) throw new Error(m.transcript_status_unavailable())
const { assetIds } = await response.json()
signal.throwIfAborted()
const activeIds = new Set(assetIds)
const previousIds = queryClient.getQueryData<string[]>(queryKey) ?? []
for (const id of previousIds) {
if (!activeIds.has(id)) {
void queryClient.invalidateQueries({ queryKey: savedTranscriptLanguagesKey(id) })
}
}
return assetIds
},
refetchInterval: ({ state }) => (state.data?.length ? 2000 : false),
})
const transcribingAssetIds = useMemo(() => new Set(enabled ? data : undefined), [data, enabled])
async function trackJob(assetId: string) {
// Discard any request that started before the job was accepted.
await queryClient.cancelQueries({ queryKey, exact: true })
queryClient.setQueryData<string[]>(queryKey, (current) =>
current?.includes(assetId) ? current : [...(current ?? []), assetId],
)
void queryClient.invalidateQueries({ queryKey, exact: true })
}
return { transcribingAssetIds, trackJob }
}