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:
parent
7ebaac8e1a
commit
4fee19d057
|
|
@ -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<number | null>(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,18 +85,47 @@ export function FileGallery({
|
|||
))}
|
||||
</ul>
|
||||
|
||||
{opened && isImageFile(opened) && (
|
||||
<FileViewer files={files} openIndex={openIndex} onOpenIndexChange={setOpenIndex} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** Открыть файл: изображение и 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) => setOpenIndex(files.indexOf(images[i]))}
|
||||
onClose={() => setOpenIndex(null)}
|
||||
onIndexChange={(i) => onOpenIndexChange(files.indexOf(images[i]))}
|
||||
onClose={() => onOpenIndexChange(null)}
|
||||
/>
|
||||
)}
|
||||
{opened && isPdfFile(opened) && <PdfPreview file={opened} onClose={() => setOpenIndex(null)} />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
if (isPdfFile(opened)) return <PdfPreview file={opened} onClose={() => onOpenIndexChange(null)} />
|
||||
return null
|
||||
}
|
||||
|
||||
function FileTile({
|
||||
file,
|
||||
|
|
|
|||
Loading…
Reference in New Issue