2026-12-03 11:19:22 +08:00
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2026-12-02 22:12:33 +08:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
|
|
|
import type { TranscriptJobStatus } from '@shumai/dtos'
|
|
|
|
|
import { toast } from 'sonner'
|
|
|
|
|
import { client } from '@/ui/api/client'
|
|
|
|
|
import { m } from '@/ui/paraglide/messages.js'
|
2026-12-03 11:19:22 +08:00
|
|
|
import { savedTranscriptLanguagesKey } from '@/ui/hooks/use-video-transcript'
|
2026-12-02 22:12:33 +08:00
|
|
|
import { Button } from '@/ui/components/ui/button'
|
|
|
|
|
import {
|
|
|
|
|
Combobox,
|
|
|
|
|
ComboboxContent,
|
|
|
|
|
ComboboxEmpty,
|
|
|
|
|
ComboboxInput,
|
|
|
|
|
ComboboxItem,
|
|
|
|
|
ComboboxList,
|
|
|
|
|
} from '@/ui/components/ui/combobox'
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from '@/ui/components/ui/dialog'
|
|
|
|
|
|
|
|
|
|
function isActive(status?: TranscriptJobStatus | null) {
|
|
|
|
|
return status?.status === 'pending' || status?.status === 'processing'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function statusMessage(job: TranscriptJobStatus) {
|
|
|
|
|
switch (job.status) {
|
|
|
|
|
case 'pending':
|
|
|
|
|
return m.transcript_queued()
|
|
|
|
|
case 'processing':
|
|
|
|
|
return m.transcript_generating()
|
|
|
|
|
case 'completed':
|
|
|
|
|
return m.transcript_completed()
|
|
|
|
|
case 'no_speech':
|
|
|
|
|
return m.transcript_no_speech()
|
|
|
|
|
case 'failed':
|
|
|
|
|
if (job.reason === 'tooLarge') return m.transcript_audio_too_large()
|
|
|
|
|
if (job.reason === 'noAudio') return m.transcript_no_audio()
|
|
|
|
|
return m.transcript_failed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function GenerateTranscriptsDialog({
|
|
|
|
|
assetId,
|
|
|
|
|
name,
|
|
|
|
|
onClose,
|
|
|
|
|
}: {
|
|
|
|
|
assetId: string
|
|
|
|
|
name: string
|
|
|
|
|
onClose: () => void
|
|
|
|
|
}) {
|
|
|
|
|
const dialogRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
|
const [language, setLanguage] = useState('')
|
|
|
|
|
const param = { fileId: assetId }
|
|
|
|
|
const statusKey = ['transcription-status', assetId]
|
|
|
|
|
|
|
|
|
|
const languages = useQuery({
|
|
|
|
|
queryKey: ['transcription-languages'],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const response = await client.api.transcription.languages.$get()
|
|
|
|
|
if (!response.ok) throw new Error(m.transcript_languages_unavailable())
|
|
|
|
|
return await response.json()
|
|
|
|
|
},
|
|
|
|
|
staleTime: 5 * 60 * 1000,
|
|
|
|
|
retry: false,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const job = useQuery({
|
|
|
|
|
queryKey: statusKey,
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const response = await client.api.files[':fileId']['transcription-status'].$get({ param })
|
|
|
|
|
if (!response.ok) throw new Error(m.transcript_status_unavailable())
|
|
|
|
|
return await response.json()
|
|
|
|
|
},
|
|
|
|
|
refetchInterval: (query) => (isActive(query.state.data) ? 2_000 : false),
|
|
|
|
|
})
|
|
|
|
|
|
2026-12-03 11:19:22 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (job.data?.status === 'completed') {
|
|
|
|
|
void queryClient.invalidateQueries({ queryKey: savedTranscriptLanguagesKey(assetId) })
|
|
|
|
|
}
|
|
|
|
|
}, [assetId, job.data, queryClient])
|
|
|
|
|
|
2026-12-02 22:12:33 +08:00
|
|
|
const generate = useMutation({
|
|
|
|
|
mutationFn: async () => {
|
|
|
|
|
const response = await client.api.files[':fileId'].transcripts.$post({
|
|
|
|
|
param,
|
|
|
|
|
json: { language: language || undefined },
|
|
|
|
|
})
|
|
|
|
|
if (!response.ok) throw new Error(m.transcript_submit_failed())
|
|
|
|
|
return await response.json()
|
|
|
|
|
},
|
|
|
|
|
onSuccess: (result) => {
|
|
|
|
|
if (result.kind === 'existing' || result.kind === 'no_speech') {
|
2026-12-03 11:19:22 +08:00
|
|
|
if (result.kind === 'existing') {
|
|
|
|
|
void queryClient.invalidateQueries({ queryKey: savedTranscriptLanguagesKey(assetId) })
|
|
|
|
|
}
|
2026-12-02 22:12:33 +08:00
|
|
|
toast.info(
|
|
|
|
|
result.kind === 'existing' ? m.transcript_already_exists() : m.transcript_no_speech(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
queryClient.setQueryData<TranscriptJobStatus>(statusKey, {
|
|
|
|
|
status: 'pending',
|
|
|
|
|
taskId: result.taskId,
|
|
|
|
|
})
|
|
|
|
|
void queryClient.invalidateQueries({ queryKey: statusKey })
|
|
|
|
|
if (result.kind === 'queued') toast.success(m.transcript_queued())
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const choices = [
|
|
|
|
|
{ code: '', name: m.transcript_language_auto() },
|
|
|
|
|
...(languages.data?.languages ?? []),
|
|
|
|
|
]
|
|
|
|
|
const busy = generate.isPending || isActive(job.data)
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
open
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
if (!open) onClose()
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<DialogContent ref={dialogRef} showCloseButton={false}>
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>{m.generate_transcripts()}</DialogTitle>
|
|
|
|
|
<DialogDescription>{name}</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
<Combobox
|
|
|
|
|
items={choices}
|
|
|
|
|
value={choices.find((choice) => choice.code === language)}
|
|
|
|
|
onValueChange={(choice) => setLanguage(choice?.code ?? '')}
|
|
|
|
|
itemToStringLabel={(choice) => choice.name}
|
|
|
|
|
isItemEqualToValue={(choice, value) => choice.code === value.code}
|
|
|
|
|
disabled={busy || languages.isPending || languages.isError}
|
|
|
|
|
>
|
|
|
|
|
<label htmlFor="transcript-language" className="text-sm font-medium">
|
|
|
|
|
{m.language()}
|
|
|
|
|
</label>
|
|
|
|
|
<ComboboxInput id="transcript-language" />
|
|
|
|
|
<ComboboxContent container={dialogRef}>
|
|
|
|
|
<ComboboxEmpty>{m.transcript_no_languages_found()}</ComboboxEmpty>
|
|
|
|
|
<ComboboxList>
|
|
|
|
|
{(choice) => (
|
|
|
|
|
<ComboboxItem key={choice.code} value={choice}>
|
|
|
|
|
{choice.name}
|
|
|
|
|
</ComboboxItem>
|
|
|
|
|
)}
|
|
|
|
|
</ComboboxList>
|
|
|
|
|
</ComboboxContent>
|
|
|
|
|
</Combobox>
|
|
|
|
|
|
|
|
|
|
{languages.isPending && (
|
|
|
|
|
<p role="status" className="text-sm text-muted-foreground">
|
|
|
|
|
{m.loading()}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{languages.isError && (
|
|
|
|
|
<div className="flex items-center justify-between gap-2">
|
|
|
|
|
<p role="alert" className="text-sm text-destructive">
|
|
|
|
|
{m.transcript_languages_unavailable()}
|
|
|
|
|
</p>
|
|
|
|
|
<Button variant="outline" size="sm" onClick={() => languages.refetch()}>
|
|
|
|
|
{m.retry()}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{generate.isError && (
|
|
|
|
|
<p role="alert" className="text-sm text-destructive">
|
|
|
|
|
{generate.error.message}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{job.isError && (
|
|
|
|
|
<p role="alert" className="text-sm text-destructive">
|
|
|
|
|
{m.transcript_status_unavailable()}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{job.data && (
|
|
|
|
|
<p
|
|
|
|
|
role="status"
|
|
|
|
|
className={
|
|
|
|
|
job.data.status === 'failed'
|
|
|
|
|
? 'text-sm text-destructive'
|
|
|
|
|
: 'text-sm text-muted-foreground'
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{statusMessage(job.data)}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" onClick={onClose}>
|
|
|
|
|
{m.close()}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={busy || !languages.data || job.isPending}
|
|
|
|
|
onClick={() => generate.mutate()}
|
|
|
|
|
>
|
|
|
|
|
{m.generate()}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|