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 <noreply@anthropic.com>
This commit is contained in:
Ilya Mukhortov 2026-10-24 19:43:09 +10:00
parent 7ebaac8e1a
commit 4fee19d057
1 changed files with 40 additions and 18 deletions

View File

@ -34,7 +34,7 @@ export function isPdfFile(file: FileItem) {
return Boolean(file.type?.includes("pdf")) || /\.pdf$/i.test(file.name) 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 (isImageFile(file)) return ImageIcon
if (isPdfFile(file)) return FileTextIcon if (isPdfFile(file)) return FileTextIcon
if (/\.(zip|rar|7z)$/i.test(file.name)) return FileArchiveIcon if (/\.(zip|rar|7z)$/i.test(file.name)) return FileArchiveIcon
@ -68,14 +68,7 @@ export function FileGallery({
className?: string className?: string
}) { }) {
const [openIndex, setOpenIndex] = React.useState<number | null>(null) const [openIndex, setOpenIndex] = React.useState<number | null>(null)
const images = files.filter((f) => isImageFile(f) && f.src) const open = (file: FileItem, index: number) => openFile(file, () => setOpenIndex(index))
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")
}
return ( return (
<> <>
@ -92,19 +85,48 @@ export function FileGallery({
))} ))}
</ul> </ul>
{opened && isImageFile(opened) && ( <FileViewer files={files} openIndex={openIndex} onOpenIndexChange={setOpenIndex} />
<ImageLightbox
images={images}
index={Math.max(0, images.indexOf(opened))}
onIndexChange={(i) => setOpenIndex(files.indexOf(images[i]))}
onClose={() => setOpenIndex(null)}
/>
)}
{opened && isPdfFile(opened) && <PdfPreview file={opened} onClose={() => 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 (
<ImageLightbox
images={images}
index={Math.max(0, images.indexOf(opened))}
onIndexChange={(i) => onOpenIndexChange(files.indexOf(images[i]))}
onClose={() => onOpenIndexChange(null)}
/>
)
}
if (isPdfFile(opened)) return <PdfPreview file={opened} onClose={() => onOpenIndexChange(null)} />
return null
}
function FileTile({ function FileTile({
file, file,
size, size,