Files
shumai/packages/webui/components/file-browser/file-preview.tsx
T

134 lines
4.0 KiB
TypeScript
Raw Normal View History

import { File, Folder, AudioLines } from 'lucide-react'
import type { MatchedSprite } from '@shumai/dtos'
2026-05-19 16:05:09 +08:00
import { SpriteScrubber } from '../sprite-scrubber'
import { formatTime } from '../viewers/video/utils'
2026-05-19 16:05:09 +08:00
type FilePreviewItem = {
type?: string | null
proxyType?: 'image' | 'video' | 'audio' | 'pdf' | '3d' | null
2026-05-19 16:05:09 +08:00
preview?: {
mediaType?: string | null
proxyType?: 'image' | 'video' | 'audio' | 'pdf' | '3d' | null
2026-05-19 16:05:09 +08:00
spriteUrl?: string
thumbnailUrl?: string
originalWidth?: number
originalHeight?: number
duration?: number
pageCount?: number
2026-05-19 16:05:09 +08:00
} | null
}
interface FilePreviewProps {
item: FilePreviewItem
showDuration?: boolean
matchedSprite?: MatchedSprite
matchDuration?: {
startTime?: number | null
endTime?: number | null
} | null
2026-05-19 16:05:09 +08:00
}
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
2026-05-19 16:05:09 +08:00
if (spriteUrl) {
2026-05-19 16:05:09 +08:00
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}
</>
2026-05-19 16:05:09 +08:00
)
}
if (item.preview?.thumbnailUrl) {
return (
<>
<img
src={item.preview.thumbnailUrl}
alt="Preview"
className="w-full h-full object-contain bg-black"
/>
{overlay}
</>
2026-05-19 16:05:09 +08:00
)
}
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}
</>
)
}
2026-05-19 16:05:09 +08:00
// 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">
2026-05-19 16:05:09 +08:00
<File className="w-8 h-8 fill-primary/60 stroke-0" />
{overlay}
2026-05-19 16:05:09 +08:00
</div>
)
}