diff --git a/web/src/components/MemoMetadata/Attachment/AttachmentListView.tsx b/web/src/components/MemoMetadata/Attachment/AttachmentListView.tsx index 0741c5c05..be8276512 100644 --- a/web/src/components/MemoMetadata/Attachment/AttachmentListView.tsx +++ b/web/src/components/MemoMetadata/Attachment/AttachmentListView.tsx @@ -1,16 +1,20 @@ -import { DownloadIcon, FileIcon, PlayIcon } from "lucide-react"; +import { DownloadIcon, ExternalLinkIcon, FileIcon, PlayIcon } from "lucide-react"; import type { PropsWithChildren } from "react"; import { useMemo } from "react"; import MotionPhotoPreview from "@/components/MotionPhotoPreview"; +import { buttonVariants } from "@/components/ui/button"; import VideoPoster from "@/components/VideoPoster"; import { cn } from "@/lib/utils"; import type { Attachment } from "@/types/proto/api/attachment_service_pb"; -import { getAttachmentUrl } from "@/utils/attachment"; +import { getAttachmentUrl, isImage } from "@/utils/attachment"; import type { AttachmentVisualItem, PreviewMediaItem } from "@/utils/media-item"; import { buildAttachmentVisualItems } from "@/utils/media-item"; import { + METADATA_ROW_BOX_CLASSES, METADATA_ROW_CLASSES, + METADATA_ROW_CONTROLS_CLASSES, METADATA_ROW_HINT_CLASSES, + METADATA_ROW_LABEL_CLASSES, METADATA_ROW_TEXT_CLASSES, MetadataRowDetail, MetadataRowIconSlot, @@ -189,20 +193,71 @@ export const AttachmentGallery = ({ visual, onImagePreview }: AttachmentGalleryP ); }; +/** The file server renders only images and PDFs inline; it serves every other file as a download. */ +const opensInline = (mimeType: string) => mimeType === "application/pdf" || isImage(mimeType); + +/** Share links carry an external link too, but it still points at this instance's file server. A link that does not parse is not ours. */ +const isServedHere = (url: string) => { + try { + return new URL(url, window.location.origin).origin === window.location.origin; + } catch { + return false; + } +}; + /** * A document is a row: file glyph in the slot, name, type and size on the detail rail. - * The whole row downloads; its arrow only appears over the detail rail once the row is - * engaged, so the rail stays on the same edge as every other row's at rest. + * A file the browser can show, or a link to another site, opens in a new tab; any other + * file downloads. A shown file offers its download in the trailing overlay. Hint and + * controls only appear over the detail rail once the row is engaged, so the rail stays on + * the same edge as every other row's at rest. */ const DocumentRow = ({ attachment }: { attachment: Attachment }) => { const { fileTypeLabel, fileSizeLabel } = getAttachmentMetadata(attachment); - return ( - + const url = getAttachmentUrl(attachment); + const body = ( + <> {attachment.filename} - + + ); + + if (!isServedHere(url)) { + return ( + + {body} + + ); + } + + if (!opensInline(attachment.type)) { + return ( + + {body} + + ); + } + + return ( +
+ + {body} + +
+ + + +
+
); }; diff --git a/web/tests/attachment-document-rows.test.tsx b/web/tests/attachment-document-rows.test.tsx new file mode 100644 index 000000000..34fc794bc --- /dev/null +++ b/web/tests/attachment-document-rows.test.tsx @@ -0,0 +1,57 @@ +import { create } from "@bufbuild/protobuf"; +import { render, screen } from "@testing-library/react"; +import { describe, expect, it } from "vitest"; +import { AttachmentRows } from "@/components/MemoMetadata/Attachment/AttachmentListView"; +import { AttachmentSchema } from "@/types/proto/api/attachment_service_pb"; + +const renderDocument = (fields: { filename: string; type: string; externalLink?: string }) => + render(); + +describe(" document rows", () => { + it("opens a PDF in a new tab and offers its download separately", () => { + renderDocument({ filename: "report.pdf", type: "application/pdf" }); + + const open = screen.getByTitle("Open report.pdf"); + expect(open.getAttribute("target")).toBe("_blank"); + expect(open.hasAttribute("download")).toBe(false); + const download = screen.getByLabelText("Download report.pdf"); + expect(download.tagName).toBe("A"); + expect(download.hasAttribute("download")).toBe(true); + expect(download.getAttribute("href")).toBe(open.getAttribute("href")); + }); + + it("downloads a file the browser would not show inline", () => { + renderDocument({ filename: "notes.txt", type: "text/plain" }); + + const row = screen.getByTitle("Download notes.txt"); + expect(row.hasAttribute("download")).toBe(true); + expect(row.hasAttribute("target")).toBe(false); + expect(screen.queryByTitle(/^Open /)).toBeNull(); + }); + + it("treats a share link as a file on this instance", () => { + renderDocument({ + filename: "notes.txt", + type: "text/plain", + externalLink: `${window.location.origin}/file/attachments/doc/notes.txt?share_token=abc`, + }); + + expect(screen.getByTitle("Download notes.txt").hasAttribute("download")).toBe(true); + }); + + it("opens a link to another site in a new tab", () => { + renderDocument({ filename: "spec.docx", type: "application/msword", externalLink: "https://example.com/spec.docx" }); + + const row = screen.getByTitle("Open spec.docx"); + expect(row.getAttribute("href")).toBe("https://example.com/spec.docx"); + expect(row.getAttribute("target")).toBe("_blank"); + expect(row.hasAttribute("download")).toBe(false); + }); + + it("opens a link that does not parse instead of failing the render", () => { + renderDocument({ filename: "broken.docx", type: "application/msword", externalLink: "http://[" }); + + const row = screen.getByTitle("Open broken.docx"); + expect(row.getAttribute("target")).toBe("_blank"); + }); +});