mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
fix(video): transcribe archived videos and show progress (#560)
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
Reference in New Issue
Block a user