2026-12-04 15:37:58 +08:00
|
|
|
import { useRef, useState } from 'react'
|
2026-12-04 13:49:25 +08:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
|
|
|
import { toast } from 'sonner'
|
|
|
|
|
import { client } from '@/ui/api/client'
|
|
|
|
|
import { m } from '@/ui/paraglide/messages.js'
|
|
|
|
|
import { savedTranscriptLanguagesKey } from '@/ui/hooks/use-video-transcript'
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
|
export function GenerateTranscriptsDialog({
|
|
|
|
|
assetId,
|
|
|
|
|
name,
|
2026-12-04 15:37:58 +08:00
|
|
|
isTranscribing,
|
|
|
|
|
onJobAccepted,
|
2026-12-04 13:49:25 +08:00
|
|
|
onClose,
|
|
|
|
|
}: {
|
|
|
|
|
assetId: string
|
|
|
|
|
name: string
|
2026-12-04 15:37:58 +08:00
|
|
|
isTranscribing: boolean
|
|
|
|
|
onJobAccepted: (assetId: string) => Promise<void>
|
2026-12-04 13:49:25 +08:00
|
|
|
onClose: () => void
|
|
|
|
|
}) {
|
|
|
|
|
const dialogRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
|
const [language, setLanguage] = useState('')
|
|
|
|
|
const param = { fileId: 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 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()
|
|
|
|
|
},
|
2026-12-04 15:37:58 +08:00
|
|
|
onSuccess: async (result) => {
|
2026-12-04 13:49:25 +08:00
|
|
|
if (result.kind === 'existing' || result.kind === 'no_speech') {
|
|
|
|
|
if (result.kind === 'existing') {
|
|
|
|
|
void queryClient.invalidateQueries({ queryKey: savedTranscriptLanguagesKey(assetId) })
|
|
|
|
|
}
|
|
|
|
|
toast.info(
|
|
|
|
|
result.kind === 'existing' ? m.transcript_already_exists() : m.transcript_no_speech(),
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-12-04 15:37:58 +08:00
|
|
|
await onJobAccepted(assetId)
|
|
|
|
|
onClose()
|
2026-12-04 13:49:25 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const choices = [
|
|
|
|
|
{ code: '', name: m.transcript_language_auto() },
|
|
|
|
|
...(languages.data?.languages ?? []),
|
|
|
|
|
]
|
2026-12-04 15:37:58 +08:00
|
|
|
const busy = generate.isPending || isTranscribing
|
2026-12-04 13:49:25 +08:00
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
)}
|
2026-12-04 15:37:58 +08:00
|
|
|
{isTranscribing && (
|
|
|
|
|
<p role="status" className="text-sm text-muted-foreground">
|
|
|
|
|
{m.transcript_generating()}
|
2026-12-04 13:49:25 +08:00
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button variant="outline" onClick={onClose}>
|
|
|
|
|
{m.close()}
|
|
|
|
|
</Button>
|
2026-12-04 15:37:58 +08:00
|
|
|
<Button disabled={busy || !languages.data} onClick={() => generate.mutate()}>
|
2026-12-04 13:49:25 +08:00
|
|
|
{m.generate()}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|