Files
shumai/packages/webui/components/file-browser/file-preview.tsx
T
Yiling-J 0f318b4050 feat(search): render results in responsive card grid with sprite preview and new-tab navigation (#479)
* feat(search): render results in responsive card grid with sprite preview and new-tab navigation

* fix(search): align semantic matched duration badge style with standard duration badge

* fix(webui): use pointer cursor instead of crosshair cursor for sprite scrubber on hover
2026-10-16 10:42:32 +08:00

120 lines
3.6 KiB
TypeScript

import { File, Folder, AudioLines } from 'lucide-react'
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
matchDuration?: {
startTime?: number | null
endTime?: number | null
} | null
}
export const FilePreview = ({ item, showDuration = false, 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 isMediaWithSprite =
(isVideo || isPdf || is3D) &&
item.preview?.spriteUrl &&
item.preview.thumbnailUrl &&
item.preview.originalWidth &&
item.preview.originalHeight
if (isMediaWithSprite && item.preview) {
return (
<>
<SpriteScrubber
spriteUrl={item.preview.spriteUrl!}
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="w-full h-full flex items-center justify-center">
<File className="w-8 h-8 fill-primary/60 stroke-0" />
</div>
)
}