From 4fee19d0573902942c86d50714396858ae5234c9 Mon Sep 17 00:00:00 2001 From: Ilya Mukhortov Date: Sat, 24 Oct 2026 19:43:09 +1000 Subject: [PATCH] Messages: file attachments Attach files with the paperclip, drag and drop onto the thread or paste from the clipboard (up to 10 files, 20 MB each). Pending files show above the input and survive switching conversations; a message can be files only. In the thread images form a grid that opens in the lightbox, other files render as cards. The lightbox and PDF preview are extracted from FileGallery as FileViewer for reuse. Co-Authored-By: Claude Opus 5.5 --- src/kit/file-gallery.tsx | 58 +++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 18 deletions(-) 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,