mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 19:58:21 +00:00
* feat(webui): add space key quick preview for files and folders Allow users in file list views to press the Space key to quick preview the most recently selected item. - Add FilePreviewDialog supporting both folder previews (icon, name, count) and real file previews (reusing FileViewer with viewer area and control bar). - Constrain dialog to max 1920x1080 or 70vw/70vh. - Support dismissing via close button, clicking outside, or pressing Space again (intercepted in capture phase). - Enable across project file lists, share links, and public share views. * feat(webui): auto play video in quick preview dialog - Pass autoPlay prop from FilePreviewDialog through FileViewer to VideoViewer - Configure videojs with autoplay: 'any' and handle policy rejection with muted autoplay fallback - Add test coverage for autoPlay prop forwarding in FilePreviewDialog * fix(webui): resolve video quick preview initialization and autoplay pause bugs * fix(webui): support folder symlinks and password-protected shares in quick preview
173 lines
5.9 KiB
TypeScript
173 lines
5.9 KiB
TypeScript
import React, { useEffect } from 'react'
|
|
import { Folder, Loader2, X } from 'lucide-react'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import type { AssetInfo } from '@shumai/dtos'
|
|
import { client } from '@/ui/api/client'
|
|
import { m } from '@/ui/paraglide/messages.js'
|
|
import { Dialog, DialogContent, DialogTitle } from '@/ui/components/ui/dialog'
|
|
import { FileViewer } from './file-viewer'
|
|
import { cn } from '../lib/utils'
|
|
|
|
export interface FilePreviewDialogProps {
|
|
item: AssetInfo | null
|
|
isOpen: boolean
|
|
onClose: () => void
|
|
allowDownload?: boolean
|
|
shareId?: string
|
|
sharePassword?: string
|
|
isPublic?: boolean
|
|
}
|
|
|
|
export function FilePreviewDialog({
|
|
item,
|
|
isOpen,
|
|
onClose,
|
|
allowDownload = true,
|
|
shareId,
|
|
sharePassword,
|
|
}: FilePreviewDialogProps) {
|
|
// Capture-phase keydown listener for Space key to unconditionally close the preview
|
|
useEffect(() => {
|
|
if (!isOpen) return
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === ' ' || e.code === 'Space') {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
onClose()
|
|
}
|
|
}
|
|
|
|
window.addEventListener('keydown', handleKeyDown, true)
|
|
return () => {
|
|
window.removeEventListener('keydown', handleKeyDown, true)
|
|
}
|
|
}, [isOpen, onClose])
|
|
|
|
const isFolder = item?.type === 'folder' || item?.targetType === 'folder'
|
|
const targetFileId = item?.versionStack ? item.versionStack.id : item?.id
|
|
|
|
const resolvedPassword =
|
|
sharePassword ??
|
|
(shareId && typeof window !== 'undefined'
|
|
? localStorage.getItem(`share_pwd_${shareId}`) || ''
|
|
: '')
|
|
|
|
// Fetch full asset data (with presigned high-res/transcoded media URLs) when previewing a file
|
|
const { data: detailedFile } = useQuery({
|
|
queryKey: shareId
|
|
? ['shares', shareId, 'files', targetFileId, resolvedPassword]
|
|
: ['files', targetFileId],
|
|
queryFn: async () => {
|
|
if (!targetFileId) throw new Error('No target file ID')
|
|
const res = shareId
|
|
? await client.api.shares[':shareId'].files[':fileId'].$get(
|
|
{
|
|
param: { shareId, fileId: targetFileId },
|
|
},
|
|
resolvedPassword
|
|
? {
|
|
headers: {
|
|
'x-share-password': resolvedPassword,
|
|
},
|
|
}
|
|
: undefined,
|
|
)
|
|
: await client.api.files[':fileId'].$get({
|
|
param: { fileId: targetFileId },
|
|
})
|
|
if (!res.ok) throw new Error('Failed to fetch file detail')
|
|
return (await res.json()) as unknown as AssetInfo
|
|
},
|
|
enabled: isOpen && !isFolder && !!targetFileId,
|
|
})
|
|
|
|
if (!item) return null
|
|
|
|
const hasPlayableUrl = Boolean(
|
|
item.media?.videoTranscodes?.some((t) => !!t.url) ||
|
|
item.media?.imageTranscodes?.some((t) => !!t.url) ||
|
|
item.media?.pdfTranscode?.url,
|
|
)
|
|
const fileToDisplay = detailedFile || (hasPlayableUrl ? item : null)
|
|
|
|
return (
|
|
<Dialog
|
|
open={isOpen}
|
|
onOpenChange={(open) => {
|
|
if (!open) onClose()
|
|
}}
|
|
>
|
|
<DialogContent
|
|
data-testid="file-preview-dialog-content"
|
|
showCloseButton={false}
|
|
className={cn(
|
|
'p-0 overflow-hidden flex flex-col gap-0 border bg-background rounded-lg shadow-2xl outline-none focus:outline-none focus-visible:outline-none',
|
|
isFolder
|
|
? 'w-full max-w-md max-h-[min(1080px,70vh)]'
|
|
: 'w-[min(1920px,70vw)] h-[min(1080px,70vh)] max-w-[min(1920px,70vw)] max-h-[min(1080px,70vh)]',
|
|
)}
|
|
style={{
|
|
maxWidth: 'min(1920px, 70vw)',
|
|
maxHeight: 'min(1080px, 70vh)',
|
|
}}
|
|
>
|
|
{/* Subtle top header bar with title and close button */}
|
|
<div className="flex items-center justify-between px-4 py-2.5 border-b border-border bg-card select-none shrink-0">
|
|
<div className="flex items-center gap-2 min-w-0 mr-4">
|
|
<DialogTitle className="text-sm font-medium text-foreground truncate" title={item.name}>
|
|
{item.name}
|
|
</DialogTitle>
|
|
</div>
|
|
<button
|
|
data-testid="quick-preview-close-button"
|
|
onClick={onClose}
|
|
className="p-1 rounded-sm text-muted-foreground hover:text-foreground hover:bg-muted transition-colors cursor-pointer"
|
|
aria-label="Close"
|
|
>
|
|
<X size={16} />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Content Area */}
|
|
{isFolder ? (
|
|
<div className="flex flex-col items-center justify-center p-8 text-center select-none gap-4">
|
|
<div className="p-4 rounded-2xl bg-primary/10 text-primary">
|
|
<Folder className="w-16 h-16 stroke-[1.5]" data-testid="folder-preview-icon" />
|
|
</div>
|
|
<div className="flex flex-col items-center gap-1 max-w-full px-2">
|
|
<h3
|
|
className="text-lg font-semibold text-foreground truncate max-w-full"
|
|
title={item.name}
|
|
>
|
|
{item.name}
|
|
</h3>
|
|
<p className="text-sm text-muted-foreground" data-testid="folder-preview-count">
|
|
{(item.fileCount || 0) === 1
|
|
? m.n_items_singular({ count: item.fileCount || 0 })
|
|
: m.n_items_plural({ count: item.fileCount || 0 })}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
) : !fileToDisplay ? (
|
|
<div
|
|
data-testid="preview-loading-spinner"
|
|
className="flex-1 flex items-center justify-center bg-background min-h-[300px]"
|
|
>
|
|
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
|
</div>
|
|
) : (
|
|
<div className="flex-1 relative min-h-0 overflow-hidden bg-background">
|
|
<FileViewer
|
|
file={fileToDisplay}
|
|
allowDownload={allowDownload}
|
|
shareId={shareId}
|
|
autoPlay={true}
|
|
/>
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|