mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 15:44:28 +00:00
Add Chinese transcript script choices and language search (#562)
* feat(transcript): add Chinese script choices * feat(transcript): add a popup language search
This commit is contained in:
@@ -2,6 +2,7 @@ import { useRef, useState } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'sonner'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { getChineseTranscriptLanguageName } from '@/ui/lib/transcript'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { savedTranscriptLanguagesKey } from '@/ui/hooks/use-video-transcript'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
@@ -12,6 +13,8 @@ import {
|
||||
ComboboxInput,
|
||||
ComboboxItem,
|
||||
ComboboxList,
|
||||
ComboboxTrigger,
|
||||
ComboboxValue,
|
||||
} from '@/ui/components/ui/combobox'
|
||||
import {
|
||||
Dialog,
|
||||
@@ -35,9 +38,11 @@ export function GenerateTranscriptsDialog({
|
||||
onJobAccepted: (assetId: string) => Promise<void>
|
||||
onClose: () => void
|
||||
}) {
|
||||
const dialogRef = useRef<HTMLDivElement>(null)
|
||||
const popupContainerRef = useRef<HTMLDivElement>(null)
|
||||
const languageTriggerRef = useRef<HTMLButtonElement>(null)
|
||||
const queryClient = useQueryClient()
|
||||
const [language, setLanguage] = useState('')
|
||||
const [isLanguagePickerOpen, setLanguagePickerOpen] = useState(false)
|
||||
const param = { fileId: assetId }
|
||||
|
||||
const languages = useQuery({
|
||||
@@ -77,9 +82,13 @@ export function GenerateTranscriptsDialog({
|
||||
|
||||
const choices = [
|
||||
{ code: '', name: m.transcript_language_auto() },
|
||||
...(languages.data?.languages ?? []),
|
||||
...(languages.data?.languages ?? []).map(({ code, name }) => ({
|
||||
code,
|
||||
name: getChineseTranscriptLanguageName(code) ?? name,
|
||||
})),
|
||||
]
|
||||
const busy = generate.isPending || isTranscribing
|
||||
const searchLabel = m.transcript_search_languages()
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
@@ -88,7 +97,12 @@ export function GenerateTranscriptsDialog({
|
||||
if (!open) onClose()
|
||||
}}
|
||||
>
|
||||
<DialogContent ref={dialogRef} showCloseButton={false}>
|
||||
<DialogContent
|
||||
showCloseButton={false}
|
||||
onEscapeKeyDown={(event) => {
|
||||
if (isLanguagePickerOpen) event.preventDefault()
|
||||
}}
|
||||
>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{m.generate_transcripts()}</DialogTitle>
|
||||
<DialogDescription>{name}</DialogDescription>
|
||||
@@ -96,16 +110,24 @@ export function GenerateTranscriptsDialog({
|
||||
|
||||
<Combobox
|
||||
items={choices}
|
||||
onOpenChange={(isOpen, details) => {
|
||||
const activeElement = dialogRef.current?.ownerDocument.activeElement
|
||||
// WebKit can report focus-out after focus has already returned to the input.
|
||||
open={isLanguagePickerOpen}
|
||||
onOpenChange={(open, details) => {
|
||||
const container = popupContainerRef.current
|
||||
const activeElement = container?.ownerDocument.activeElement
|
||||
// WebKit can report focus-out while focus is still within the language picker.
|
||||
if (
|
||||
!isOpen &&
|
||||
!open &&
|
||||
details.reason === 'focus-out' &&
|
||||
activeElement === details.event.target
|
||||
(activeElement === languageTriggerRef.current ||
|
||||
container?.contains(activeElement ?? null))
|
||||
) {
|
||||
details.cancel()
|
||||
return
|
||||
}
|
||||
if (!open && details.reason === 'escape-key') {
|
||||
languageTriggerRef.current?.focus({ preventScroll: true })
|
||||
}
|
||||
setLanguagePickerOpen(open)
|
||||
}}
|
||||
value={choices.find((choice) => choice.code === language)}
|
||||
onValueChange={(choice) => setLanguage(choice?.code ?? '')}
|
||||
@@ -113,20 +135,40 @@ export function GenerateTranscriptsDialog({
|
||||
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>
|
||||
<div className="grid gap-2">
|
||||
<label htmlFor="transcript-language" className="text-sm font-medium">
|
||||
{m.language()}
|
||||
</label>
|
||||
<ComboboxTrigger
|
||||
ref={languageTriggerRef}
|
||||
id="transcript-language"
|
||||
render={<Button variant="outline" className="w-full justify-between font-normal" />}
|
||||
>
|
||||
<ComboboxValue />
|
||||
</ComboboxTrigger>
|
||||
</div>
|
||||
{/* Keep the popup inside the modal focus trap and outside the dialog grid. */}
|
||||
<div ref={popupContainerRef} className="absolute top-0 left-0">
|
||||
<ComboboxContent
|
||||
container={popupContainerRef}
|
||||
className="min-w-0"
|
||||
aria-label={m.language()}
|
||||
>
|
||||
<ComboboxInput
|
||||
aria-label={searchLabel}
|
||||
placeholder={searchLabel}
|
||||
showTrigger={false}
|
||||
/>
|
||||
<ComboboxEmpty>{m.transcript_no_languages_found()}</ComboboxEmpty>
|
||||
<ComboboxList>
|
||||
{(choice) => (
|
||||
<ComboboxItem key={choice.code} value={choice}>
|
||||
{choice.name}
|
||||
</ComboboxItem>
|
||||
)}
|
||||
</ComboboxList>
|
||||
</ComboboxContent>
|
||||
</div>
|
||||
</Combobox>
|
||||
|
||||
{languages.isPending && (
|
||||
|
||||
@@ -5,6 +5,7 @@ import { NativeSelect, NativeSelectOption } from '@/ui/components/ui/native-sele
|
||||
import { ScrollArea } from '@/ui/components/ui/scroll-area'
|
||||
import type { MediaController } from '@/ui/components/viewers/types'
|
||||
import { savedTranscriptKey } from '@/ui/hooks/use-video-transcript'
|
||||
import { getChineseTranscriptLanguageName } from '@/ui/lib/transcript'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { getLocale } from '@/ui/paraglide/runtime.js'
|
||||
@@ -69,7 +70,10 @@ export const TranscriptPanel = memo(function TranscriptPanel({
|
||||
const options = useMemo(() => {
|
||||
const names = new Intl.DisplayNames(locale, { type: 'language' })
|
||||
return languages
|
||||
.map((code) => ({ code, name: names.of(code) ?? code }))
|
||||
.map((code) => ({
|
||||
code,
|
||||
name: getChineseTranscriptLanguageName(code) ?? names.of(code) ?? code,
|
||||
}))
|
||||
.sort((a, b) => a.name.localeCompare(b.name, locale))
|
||||
}, [languages, locale])
|
||||
const [preferredLanguage, setPreferredLanguage] = useState<string>(locale)
|
||||
|
||||
Reference in New Issue
Block a user