mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 07:34:25 +00:00
feat(transcript): add a popup language search
This commit is contained in:
@@ -13,6 +13,8 @@ import {
|
||||
ComboboxInput,
|
||||
ComboboxItem,
|
||||
ComboboxList,
|
||||
ComboboxTrigger,
|
||||
ComboboxValue,
|
||||
} from '@/ui/components/ui/combobox'
|
||||
import {
|
||||
Dialog,
|
||||
@@ -36,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({
|
||||
@@ -84,6 +88,7 @@ export function GenerateTranscriptsDialog({
|
||||
})),
|
||||
]
|
||||
const busy = generate.isPending || isTranscribing
|
||||
const searchLabel = m.transcript_search_languages()
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
@@ -92,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>
|
||||
@@ -100,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 ?? '')}
|
||||
@@ -117,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 && (
|
||||
|
||||
Reference in New Issue
Block a user