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,5 +1,14 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { GenerateTranscriptsDialog } from '@/ui/components/file-browser/generate-transcripts-dialog'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
Empty,
|
||||
EmptyContent,
|
||||
EmptyDescription,
|
||||
EmptyHeader,
|
||||
EmptyMedia,
|
||||
EmptyTitle,
|
||||
} from '@/ui/components/ui/empty'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { NativeSelect, NativeSelectOption } from '@/ui/components/ui/native-select'
|
||||
import { ScrollArea } from '@/ui/components/ui/scroll-area'
|
||||
@@ -10,8 +19,8 @@ import { cn } from '@/ui/lib/utils'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { getLocale } from '@/ui/paraglide/runtime.js'
|
||||
import type { TranscriptSegment } from '@shumai/dtos'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { ChevronDown, ChevronUp, Search, X } from 'lucide-react'
|
||||
import { skipToken, useQuery } from '@tanstack/react-query'
|
||||
import { Captions, ChevronDown, ChevronUp, Loader2, Search, X } from 'lucide-react'
|
||||
import {
|
||||
memo,
|
||||
useCallback,
|
||||
@@ -29,8 +38,13 @@ import { useTranscriptScroll } from './use-transcript-scroll'
|
||||
interface TranscriptPanelProps {
|
||||
id: string
|
||||
assetId: string
|
||||
assetName?: string
|
||||
languages: string[]
|
||||
canEdit: boolean
|
||||
canGenerate?: boolean
|
||||
canStartGeneration?: boolean
|
||||
isTranscribing?: boolean
|
||||
onJobAccepted?: (assetId: string) => Promise<void>
|
||||
mediaControllerRef: RefObject<MediaController | null>
|
||||
onClose: () => void
|
||||
className?: string
|
||||
@@ -60,8 +74,13 @@ function findMatches(segments: TranscriptSegment[], search: string) {
|
||||
export const TranscriptPanel = memo(function TranscriptPanel({
|
||||
id,
|
||||
assetId,
|
||||
assetName = '',
|
||||
languages,
|
||||
canEdit,
|
||||
canGenerate = false,
|
||||
canStartGeneration = false,
|
||||
isTranscribing = false,
|
||||
onJobAccepted,
|
||||
mediaControllerRef,
|
||||
onClose,
|
||||
className,
|
||||
@@ -76,29 +95,33 @@ export const TranscriptPanel = memo(function TranscriptPanel({
|
||||
}))
|
||||
.sort((a, b) => a.name.localeCompare(b.name, locale))
|
||||
}, [languages, locale])
|
||||
const hasTranscript = options.length > 0
|
||||
const [preferredLanguage, setPreferredLanguage] = useState<string>(locale)
|
||||
const language = options.find(({ code }) => code === preferredLanguage)?.code ?? options[0].code
|
||||
const language = options.find(({ code }) => code === preferredLanguage)?.code ?? options[0]?.code
|
||||
const [searchOpen, setSearchOpen] = useState(false)
|
||||
const [search, setSearch] = useState('')
|
||||
const [matchIndex, setMatchIndex] = useState(0)
|
||||
const [following, setFollowing] = useState(true)
|
||||
const [activeId, setActiveId] = useState<string>()
|
||||
const [editingId, setEditingId] = useState<string>()
|
||||
const [generateDialogOpen, setGenerateDialogOpen] = useState(false)
|
||||
const toggleSearchRef = useRef<HTMLButtonElement>(null)
|
||||
const closeRef = useRef<HTMLButtonElement>(null)
|
||||
const { viewportRef, setViewport, scrollTo, cancelScroll, onScroll, onWheel } =
|
||||
useTranscriptScroll(() => setFollowing(false))
|
||||
|
||||
const transcript = useQuery({
|
||||
queryKey: savedTranscriptKey(assetId, language),
|
||||
queryFn: async ({ signal }) => {
|
||||
const response = await client.api.files[':fileId'].transcripts[':language'].$get(
|
||||
{ param: { fileId: assetId, language } },
|
||||
{ init: { signal } },
|
||||
)
|
||||
if (!response.ok) throw new Error(m.transcript_load_failed())
|
||||
return await response.json()
|
||||
},
|
||||
queryKey: savedTranscriptKey(assetId, language ?? ''),
|
||||
queryFn: language
|
||||
? async ({ signal }) => {
|
||||
const response = await client.api.files[':fileId'].transcripts[':language'].$get(
|
||||
{ param: { fileId: assetId, language } },
|
||||
{ init: { signal } },
|
||||
)
|
||||
if (!response.ok) throw new Error(m.transcript_load_failed())
|
||||
return await response.json()
|
||||
}
|
||||
: skipToken,
|
||||
retry: false,
|
||||
})
|
||||
const segments = transcript.data?.segments ?? noSegments
|
||||
@@ -180,39 +203,45 @@ export const TranscriptPanel = memo(function TranscriptPanel({
|
||||
>
|
||||
<div className="shrink-0 space-y-2 border-b border-border p-2">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<div className="min-w-0 [&>[data-slot=native-select-wrapper]]:max-w-full">
|
||||
<NativeSelect
|
||||
size="sm"
|
||||
className="max-w-40 truncate"
|
||||
aria-label={m.language()}
|
||||
value={language}
|
||||
disabled={!!editingId}
|
||||
onChange={(event) => {
|
||||
cancelScroll()
|
||||
setPreferredLanguage(event.target.value)
|
||||
setMatchIndex(0)
|
||||
setFollowing(true)
|
||||
}}
|
||||
>
|
||||
{options.map(({ code, name }) => (
|
||||
<NativeSelectOption key={code} value={code}>
|
||||
{name}
|
||||
</NativeSelectOption>
|
||||
))}
|
||||
</NativeSelect>
|
||||
</div>
|
||||
{hasTranscript && language ? (
|
||||
<div className="min-w-0 [&>[data-slot=native-select-wrapper]]:max-w-full">
|
||||
<NativeSelect
|
||||
size="sm"
|
||||
className="max-w-40 truncate"
|
||||
aria-label={m.language()}
|
||||
value={language}
|
||||
disabled={!!editingId}
|
||||
onChange={(event) => {
|
||||
cancelScroll()
|
||||
setPreferredLanguage(event.target.value)
|
||||
setMatchIndex(0)
|
||||
setFollowing(true)
|
||||
}}
|
||||
>
|
||||
{options.map(({ code, name }) => (
|
||||
<NativeSelectOption key={code} value={code}>
|
||||
{name}
|
||||
</NativeSelectOption>
|
||||
))}
|
||||
</NativeSelect>
|
||||
</div>
|
||||
) : (
|
||||
<h2 className="truncate px-1 text-sm font-medium">{m.transcript_title()}</h2>
|
||||
)}
|
||||
<div className="flex shrink-0 items-center gap-1">
|
||||
<Button
|
||||
ref={toggleSearchRef}
|
||||
size="icon-sm"
|
||||
variant="ghost"
|
||||
aria-expanded={searchOpen}
|
||||
aria-label={m.transcript_search()}
|
||||
title={m.transcript_search()}
|
||||
onClick={toggleSearch}
|
||||
>
|
||||
<Search />
|
||||
</Button>
|
||||
{hasTranscript && (
|
||||
<Button
|
||||
ref={toggleSearchRef}
|
||||
size="icon-sm"
|
||||
variant="ghost"
|
||||
aria-expanded={searchOpen}
|
||||
aria-label={m.transcript_search()}
|
||||
title={m.transcript_search()}
|
||||
onClick={toggleSearch}
|
||||
>
|
||||
<Search />
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
ref={closeRef}
|
||||
size="icon-sm"
|
||||
@@ -225,7 +254,7 @@ export const TranscriptPanel = memo(function TranscriptPanel({
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{searchOpen && (
|
||||
{hasTranscript && searchOpen && (
|
||||
<div className="flex items-center gap-1">
|
||||
<Input
|
||||
autoFocus
|
||||
@@ -280,76 +309,120 @@ export const TranscriptPanel = memo(function TranscriptPanel({
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{transcript.isPending && (
|
||||
<p role="status" className="p-3 text-sm text-muted-foreground">
|
||||
{m.loading()}
|
||||
</p>
|
||||
)}
|
||||
{transcript.isError && (
|
||||
<div className="space-y-2 p-3">
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{m.transcript_load_failed()}
|
||||
</p>
|
||||
<Button size="sm" variant="outline" onClick={() => void transcript.refetch()}>
|
||||
{m.retry()}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{transcript.data && (
|
||||
<div className="relative min-h-0 flex-1">
|
||||
<ScrollArea
|
||||
className="h-full [&>div>div]:block!"
|
||||
viewportProps={{
|
||||
ref: setViewport,
|
||||
tabIndex: 0,
|
||||
className: '[overflow-anchor:none]',
|
||||
onScroll,
|
||||
onWheel,
|
||||
}}
|
||||
{!hasTranscript ? (
|
||||
isTranscribing ? (
|
||||
<div
|
||||
role="status"
|
||||
className="flex min-h-0 flex-1 flex-col items-center justify-center gap-3 p-6 text-center"
|
||||
>
|
||||
<ol className="space-y-0.5 px-2 pt-2 pb-14">
|
||||
{segments.map((segment) => {
|
||||
const segmentMatches = matches.bySegment.get(segment.id)
|
||||
return (
|
||||
<TranscriptSegmentRow
|
||||
key={segment.id}
|
||||
segment={segment}
|
||||
active={segment.id === activeId}
|
||||
matches={segmentMatches}
|
||||
selectedMatch={
|
||||
segmentMatches?.some(({ index }) => index === selectedMatch)
|
||||
? selectedMatch
|
||||
: undefined
|
||||
}
|
||||
mediaControllerRef={mediaControllerRef}
|
||||
canEdit={canEdit}
|
||||
onEdit={canEdit && !editingId ? startEditing : undefined}
|
||||
editTarget={
|
||||
segment.id === editingId
|
||||
? { assetId, language, onFinish: finishEditing }
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</ol>
|
||||
</ScrollArea>
|
||||
{!following && !editingId && (
|
||||
<div className="pointer-events-none absolute inset-x-0 bottom-3 flex justify-center px-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="pointer-events-auto rounded-full shadow-md dark:bg-background dark:hover:bg-accent"
|
||||
onClick={() => {
|
||||
viewportRef.current?.focus({ preventScroll: true })
|
||||
setFollowing(true)
|
||||
}}
|
||||
>
|
||||
{m.transcript_sync_to_playback()}
|
||||
<Loader2 className="size-6 animate-spin text-muted-foreground" aria-hidden="true" />
|
||||
<p className="text-sm text-muted-foreground">{m.transcript_generating()}</p>
|
||||
</div>
|
||||
) : (
|
||||
<Empty className="gap-4 p-6 md:p-6">
|
||||
<EmptyHeader>
|
||||
<EmptyMedia variant="icon">
|
||||
<Captions />
|
||||
</EmptyMedia>
|
||||
<EmptyTitle className="text-base">{m.transcript_empty_title()}</EmptyTitle>
|
||||
<EmptyDescription>{m.transcript_empty_description()}</EmptyDescription>
|
||||
</EmptyHeader>
|
||||
{canGenerate && onJobAccepted && (
|
||||
<EmptyContent>
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={!canStartGeneration}
|
||||
onClick={() => setGenerateDialogOpen(true)}
|
||||
>
|
||||
{m.generate_transcripts()}
|
||||
</Button>
|
||||
</EmptyContent>
|
||||
)}
|
||||
</Empty>
|
||||
)
|
||||
) : (
|
||||
<>
|
||||
{transcript.isPending && (
|
||||
<p role="status" className="p-3 text-sm text-muted-foreground">
|
||||
{m.loading()}
|
||||
</p>
|
||||
)}
|
||||
{transcript.isError && (
|
||||
<div className="space-y-2 p-3">
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{m.transcript_load_failed()}
|
||||
</p>
|
||||
<Button size="sm" variant="outline" onClick={() => void transcript.refetch()}>
|
||||
{m.retry()}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{transcript.data && language && (
|
||||
<div className="relative min-h-0 flex-1">
|
||||
<ScrollArea
|
||||
className="h-full [&>div>div]:block!"
|
||||
viewportProps={{
|
||||
ref: setViewport,
|
||||
tabIndex: 0,
|
||||
className: '[overflow-anchor:none]',
|
||||
onScroll,
|
||||
onWheel,
|
||||
}}
|
||||
>
|
||||
<ol className="space-y-0.5 px-2 pt-2 pb-14">
|
||||
{segments.map((segment) => {
|
||||
const segmentMatches = matches.bySegment.get(segment.id)
|
||||
return (
|
||||
<TranscriptSegmentRow
|
||||
key={segment.id}
|
||||
segment={segment}
|
||||
active={segment.id === activeId}
|
||||
matches={segmentMatches}
|
||||
selectedMatch={
|
||||
segmentMatches?.some(({ index }) => index === selectedMatch)
|
||||
? selectedMatch
|
||||
: undefined
|
||||
}
|
||||
mediaControllerRef={mediaControllerRef}
|
||||
canEdit={canEdit}
|
||||
onEdit={canEdit && !editingId ? startEditing : undefined}
|
||||
editTarget={
|
||||
segment.id === editingId
|
||||
? { assetId, language, onFinish: finishEditing }
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</ol>
|
||||
</ScrollArea>
|
||||
{!following && !editingId && (
|
||||
<div className="pointer-events-none absolute inset-x-0 bottom-3 flex justify-center px-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="pointer-events-auto rounded-full shadow-md dark:bg-background dark:hover:bg-accent"
|
||||
onClick={() => {
|
||||
viewportRef.current?.focus({ preventScroll: true })
|
||||
setFollowing(true)
|
||||
}}
|
||||
>
|
||||
{m.transcript_sync_to_playback()}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{!hasTranscript && generateDialogOpen && canGenerate && onJobAccepted && (
|
||||
<GenerateTranscriptsDialog
|
||||
assetId={assetId}
|
||||
name={assetName}
|
||||
isTranscribing={isTranscribing}
|
||||
onJobAccepted={onJobAccepted}
|
||||
onClose={() => setGenerateDialogOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</aside>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user