diff --git a/src/kit/file-gallery.tsx b/src/kit/file-gallery.tsx index ae0b94e..b65c231 100644 --- a/src/kit/file-gallery.tsx +++ b/src/kit/file-gallery.tsx @@ -34,7 +34,7 @@ export function isPdfFile(file: FileItem) { return Boolean(file.type?.includes("pdf")) || /\.pdf$/i.test(file.name) } -function fileIcon(file: FileItem) { +export function fileIcon(file: FileItem) { if (isImageFile(file)) return ImageIcon if (isPdfFile(file)) return FileTextIcon if (/\.(zip|rar|7z)$/i.test(file.name)) return FileArchiveIcon @@ -68,14 +68,7 @@ export function FileGallery({ className?: string }) { const [openIndex, setOpenIndex] = React.useState(null) - const images = files.filter((f) => isImageFile(f) && f.src) - const opened = openIndex === null ? null : files[openIndex] - - const open = (file: FileItem, index: number) => { - if (!file.src) return - if (isImageFile(file) || isPdfFile(file)) setOpenIndex(index) - else window.open(file.src, "_blank", "noopener") - } + const open = (file: FileItem, index: number) => openFile(file, () => setOpenIndex(index)) return ( <> @@ -92,19 +85,48 @@ export function FileGallery({ ))} - {opened && isImageFile(opened) && ( - setOpenIndex(files.indexOf(images[i]))} - onClose={() => setOpenIndex(null)} - /> - )} - {opened && isPdfFile(opened) && setOpenIndex(null)} />} + ) } +/** Открыть файл: изображение и PDF — во встроенном просмотрщике (preview), остальное — в новой вкладке. */ +export function openFile(file: FileItem, preview: () => void) { + if (!file.src) return + if (isImageFile(file) || isPdfFile(file)) preview() + else window.open(file.src, "_blank", "noopener") +} + +/** + * Просмотрщик для набора файлов: лайтбокс со стрелками для изображений, окно для PDF. + * Для своих раскладок файлов (вложения в чате и т. п.), когда плитки FileGallery не подходят. + */ +export function FileViewer({ + files, + openIndex, + onOpenIndexChange, +}: { + files: readonly FileItem[] + openIndex: number | null + onOpenIndexChange: (index: number | null) => void +}) { + const images = files.filter((f) => isImageFile(f) && f.src) + const opened = openIndex === null ? null : files[openIndex] + if (!opened) return null + if (isImageFile(opened)) { + return ( + onOpenIndexChange(files.indexOf(images[i]))} + onClose={() => onOpenIndexChange(null)} + /> + ) + } + if (isPdfFile(opened)) return onOpenIndexChange(null)} /> + return null +} + function FileTile({ file, size,