mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 08:04:37 +00:00
feat(webui): allow generating transcripts from video detail panel (#564)
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { skipToken, useQuery } from '@tanstack/react-query'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import {
|
||||
isTranscriptVideoReady,
|
||||
selectTranscriptVideoProxyKey,
|
||||
type AssetInfo,
|
||||
type TranscriptJobStatus,
|
||||
} from '@shumai/dtos'
|
||||
import { skipToken, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useCallback, useId, useRef, useState } from 'react'
|
||||
import { useServerFeatures } from './use-server-features'
|
||||
|
||||
export const savedTranscriptLanguagesKey = (assetId?: string) =>
|
||||
['files', assetId, 'transcripts'] as const
|
||||
@@ -9,11 +16,39 @@ export const savedTranscriptLanguagesKey = (assetId?: string) =>
|
||||
export const savedTranscriptKey = (assetId: string, language: string) =>
|
||||
[...savedTranscriptLanguagesKey(assetId), language] as const
|
||||
|
||||
export function useVideoTranscript(file?: Pick<AssetInfo, 'id' | 'proxyType' | 'versionStack'>) {
|
||||
const assetId =
|
||||
file?.proxyType === 'video'
|
||||
? (file.versionStack?.versions.find(({ current }) => current)?.id ?? file.id)
|
||||
: undefined
|
||||
export const fileTranscriptionStatusKey = (assetId?: string) =>
|
||||
['files', assetId, 'transcription-status'] as const
|
||||
|
||||
type VideoTranscriptFile = Pick<
|
||||
AssetInfo,
|
||||
'id' | 'name' | 'status' | 'proxyType' | 'media' | 'versionStack'
|
||||
>
|
||||
|
||||
interface UseVideoTranscriptOptions {
|
||||
canEdit?: boolean
|
||||
projectId?: string
|
||||
}
|
||||
|
||||
function isActiveTranscriptJob(status?: TranscriptJobStatus | null): boolean {
|
||||
return status?.status === 'pending' || status?.status === 'processing'
|
||||
}
|
||||
|
||||
export function useVideoTranscript(
|
||||
file?: VideoTranscriptFile,
|
||||
options?: UseVideoTranscriptOptions,
|
||||
) {
|
||||
const queryClient = useQueryClient()
|
||||
const canEdit = Boolean(options?.canEdit)
|
||||
const projectId = options?.projectId
|
||||
const currentVersion = file?.versionStack?.versions.find(({ current }) => current)
|
||||
const assetId = file?.proxyType === 'video' ? (currentVersion?.id ?? file.id) : undefined
|
||||
const assetName = currentVersion?.name || file?.name || ''
|
||||
const assetStatus = currentVersion?.status ?? file?.status ?? ''
|
||||
const canStartGeneration =
|
||||
Boolean(assetId) &&
|
||||
isTranscriptVideoReady(assetStatus) &&
|
||||
Boolean(selectTranscriptVideoProxyKey(file?.media))
|
||||
|
||||
const panelId = useId()
|
||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||
const [selection, setSelection] = useState({ assetId, open: false })
|
||||
@@ -21,6 +56,9 @@ export function useVideoTranscript(file?: Pick<AssetInfo, 'id' | 'proxyType' | '
|
||||
setSelection({ assetId, open: false })
|
||||
}
|
||||
|
||||
const serverFeatures = useServerFeatures(Boolean(assetId && canEdit))
|
||||
const canGenerate = Boolean(assetId && canEdit && serverFeatures.data?.transcriptionEnabled)
|
||||
|
||||
const { data } = useQuery({
|
||||
queryKey: savedTranscriptLanguagesKey(assetId),
|
||||
queryFn: assetId
|
||||
@@ -35,16 +73,109 @@ export function useVideoTranscript(file?: Pick<AssetInfo, 'id' | 'proxyType' | '
|
||||
: skipToken,
|
||||
})
|
||||
|
||||
const languages = data?.languages
|
||||
const hasTranscript = Boolean(languages?.length)
|
||||
const shouldTrackStatus = Boolean(
|
||||
assetId && canGenerate && languages !== undefined && !hasTranscript,
|
||||
)
|
||||
|
||||
const statusQuery = useQuery({
|
||||
queryKey: fileTranscriptionStatusKey(assetId),
|
||||
enabled: shouldTrackStatus,
|
||||
queryFn: assetId
|
||||
? async ({ signal }) => {
|
||||
const statusKey = fileTranscriptionStatusKey(assetId)
|
||||
const response = await client.api.files[':fileId']['transcription-status'].$get(
|
||||
{ param: { fileId: assetId } },
|
||||
{ init: { signal } },
|
||||
)
|
||||
if (!response.ok) throw new Error(m.transcript_status_unavailable())
|
||||
const next = (await response.json()) as TranscriptJobStatus | null
|
||||
signal.throwIfAborted()
|
||||
|
||||
const previous = queryClient.getQueryData<TranscriptJobStatus | null>(statusKey)
|
||||
const wasActive = isActiveTranscriptJob(previous)
|
||||
const isActive = isActiveTranscriptJob(next)
|
||||
|
||||
if ((wasActive || previous === undefined) && next?.status === 'completed') {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: savedTranscriptLanguagesKey(assetId),
|
||||
exact: true,
|
||||
})
|
||||
signal.throwIfAborted()
|
||||
}
|
||||
|
||||
if (wasActive && !isActive) {
|
||||
if (next?.status === 'failed') {
|
||||
void queryClient.invalidateQueries({ queryKey: ['files', assetId, 'comments'] })
|
||||
if (file?.id && file.id !== assetId) {
|
||||
void queryClient.invalidateQueries({ queryKey: ['files', file.id, 'comments'] })
|
||||
}
|
||||
}
|
||||
if (projectId) {
|
||||
const projectStatusKey = ['project-transcription-status', projectId] as const
|
||||
queryClient.setQueryData<string[]>(projectStatusKey, (current) =>
|
||||
current?.filter((id) => id !== assetId),
|
||||
)
|
||||
void queryClient.invalidateQueries({
|
||||
queryKey: projectStatusKey,
|
||||
exact: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return next
|
||||
}
|
||||
: skipToken,
|
||||
refetchInterval: ({ state }) => (isActiveTranscriptJob(state.data) ? 2000 : false),
|
||||
})
|
||||
|
||||
const isTranscribing = shouldTrackStatus && isActiveTranscriptJob(statusQuery.data)
|
||||
|
||||
const trackJob = useCallback(
|
||||
async (targetAssetId: string) => {
|
||||
const statusKey = fileTranscriptionStatusKey(targetAssetId)
|
||||
// Discard any status request that started before the job was accepted.
|
||||
await queryClient.cancelQueries({ queryKey: statusKey, exact: true })
|
||||
queryClient.setQueryData<TranscriptJobStatus | null>(statusKey, (current) =>
|
||||
isActiveTranscriptJob(current) ? current : { status: 'pending', taskId: '' },
|
||||
)
|
||||
void queryClient.invalidateQueries({ queryKey: statusKey, exact: true })
|
||||
|
||||
if (projectId) {
|
||||
const projectStatusKey = ['project-transcription-status', projectId] as const
|
||||
await queryClient.cancelQueries({ queryKey: projectStatusKey, exact: true })
|
||||
queryClient.setQueryData<string[]>(projectStatusKey, (current) =>
|
||||
current?.includes(targetAssetId) ? current : [...(current ?? []), targetAssetId],
|
||||
)
|
||||
void queryClient.invalidateQueries({ queryKey: projectStatusKey, exact: true })
|
||||
}
|
||||
},
|
||||
[projectId, queryClient],
|
||||
)
|
||||
|
||||
const close = useCallback(() => {
|
||||
setSelection({ assetId, open: false })
|
||||
buttonRef.current?.focus({ preventScroll: true })
|
||||
}, [assetId])
|
||||
|
||||
if (!assetId || !data?.languages.length) return undefined
|
||||
if (!assetId || languages === undefined || (!hasTranscript && !canGenerate)) return undefined
|
||||
|
||||
const open = selection.assetId === assetId && selection.open
|
||||
return {
|
||||
panel: open ? { id: panelId, assetId, languages: data.languages, onClose: close } : undefined,
|
||||
panel: open
|
||||
? {
|
||||
id: panelId,
|
||||
assetId,
|
||||
assetName,
|
||||
languages,
|
||||
canGenerate,
|
||||
canStartGeneration,
|
||||
isTranscribing,
|
||||
onJobAccepted: trackJob,
|
||||
onClose: close,
|
||||
}
|
||||
: undefined,
|
||||
toggle: {
|
||||
panelId,
|
||||
open,
|
||||
|
||||
Reference in New Issue
Block a user