Files
shumai/packages/webui/components/file-browser/file-preview.tsx
T
Yiling-J eeb41fee03 fix(search): preview matched video chunks on hover (#540)
* fix(search): preview matched video chunks on hover

* fix(search): show chunk covers before hover

* fix(search): reuse sprites for whole-video matches
2026-11-23 11:42:00 +08:00

134 lines
4.0 KiB
TypeScript

import { File, Folder, AudioLines } from 'lucide-react'
import type { MatchedSprite } from '@shumai/dtos'
import { SpriteScrubber } from '../sprite-scrubber'
import { formatTime } from '../viewers/video/utils'
type FilePreviewItem = {
type?: string | null
proxyType?: 'image' | 'video' | 'audio' | 'pdf' | '3d' | null
preview?: {
mediaType?: string | null
proxyType?: 'image' | 'video' | 'audio' | 'pdf' | '3d' | null
spriteUrl?: string
thumbnailUrl?: string
originalWidth?: number
originalHeight?: number
duration?: number
pageCount?: number
} | null
}
interface FilePreviewProps {
item: FilePreviewItem
showDuration?: boolean
matchedSprite?: MatchedSprite
matchDuration?: {
startTime?: number | null
endTime?: number | null
} | null
}
export const FilePreview = ({
item,
showDuration = false,
matchedSprite,
matchDuration,
}: FilePreviewProps) => {
const proxyType = item.preview?.proxyType || item.proxyType
const isVideo = proxyType === 'video'
const isAudio = proxyType === 'audio'
const isPdf = proxyType === 'pdf'
const is3D = proxyType === '3d'
const hasDuration = isVideo || isAudio
const duration = item.preview?.duration
const pageCount = item.preview?.pageCount
const hasMatchedChunk =
matchDuration != null && matchDuration.startTime != null && matchDuration.endTime != null
const overlay = hasMatchedChunk ? (
<span
data-testid="file-preview-matched-duration"
className="pointer-events-none absolute bottom-1 right-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
>
{formatTime(matchDuration.startTime!)} - {formatTime(matchDuration.endTime!)}
</span>
) : showDuration && hasDuration && typeof duration === 'number' && duration > 0 ? (
<span className="pointer-events-none absolute bottom-1 right-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white">
{formatTime(duration)}
</span>
) : showDuration && isPdf && typeof pageCount === 'number' && pageCount > 0 ? (
<span className="pointer-events-none absolute bottom-1 right-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white">
{pageCount} P
</span>
) : null
const chunkSprite = hasMatchedChunk ? matchedSprite : undefined
const canShowFullSprite =
hasMatchedChunk ||
((isVideo || isPdf || is3D) &&
item.preview?.thumbnailUrl &&
item.preview.originalWidth &&
item.preview.originalHeight)
const fullSpriteUrl = canShowFullSprite ? item.preview?.spriteUrl : undefined
const spriteUrl = chunkSprite?.url || fullSpriteUrl
if (spriteUrl) {
return (
<>
<SpriteScrubber
spriteUrl={spriteUrl}
spriteLayout={chunkSprite}
showFirstFrame={Boolean(chunkSprite)}
fallbackSpriteUrl={chunkSprite && fullSpriteUrl ? fullSpriteUrl : undefined}
thumbnailUrl={item.preview?.thumbnailUrl}
videoWidth={item.preview?.originalWidth}
videoHeight={item.preview?.originalHeight}
reverse={is3D}
/>
{overlay}
</>
)
}
if (item.preview?.thumbnailUrl) {
return (
<>
<img
src={item.preview.thumbnailUrl}
alt="Preview"
className="w-full h-full object-contain bg-black"
/>
{overlay}
</>
)
}
if (isAudio) {
return (
<>
<div className="w-full h-full flex items-center justify-center bg-zinc-950">
<AudioLines className="w-8 h-8 text-zinc-500" />
</div>
{overlay}
</>
)
}
// Assumption: folders in latestChildren have no media type.
if (!item.type) {
return (
<div className="w-full h-full flex items-center justify-center bg-linear-to-t from-black/5 to-black/0">
<Folder className="w-8 h-8 fill-primary/60 stroke-0" />
</div>
)
}
return (
<div className="relative w-full h-full flex items-center justify-center">
<File className="w-8 h-8 fill-primary/60 stroke-0" />
{overlay}
</div>
)
}