feat(ui): open attachments in another tab (#6496)

Co-authored-by: bluedbird <dev@usememos.com>
This commit is contained in:
Brandon Richardson
2026-10-29 13:35:40 +01:00
committed by GitHub
parent 08acc22f3f
commit f92bc93262
2 changed files with 120 additions and 8 deletions
@@ -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 (
<a href={getAttachmentUrl(attachment)} download title={`Download ${attachment.filename}`} className={METADATA_ROW_CLASSES}>
const url = getAttachmentUrl(attachment);
const body = (
<>
<MetadataRowIconSlot icon={FileIcon} />
<span className={METADATA_ROW_TEXT_CLASSES}>{attachment.filename}</span>
<MetadataRowDetail parts={[fileTypeLabel, fileSizeLabel]} />
<DownloadIcon aria-hidden="true" className={METADATA_ROW_HINT_CLASSES} strokeWidth={1.8} />
</a>
</>
);
if (!isServedHere(url)) {
return (
<a href={url} target="_blank" rel="noreferrer" title={`Open ${attachment.filename}`} className={METADATA_ROW_CLASSES}>
{body}
<ExternalLinkIcon aria-hidden="true" className={METADATA_ROW_HINT_CLASSES} strokeWidth={1.8} />
</a>
);
}
if (!opensInline(attachment.type)) {
return (
<a href={url} download title={`Download ${attachment.filename}`} className={METADATA_ROW_CLASSES}>
{body}
<DownloadIcon aria-hidden="true" className={METADATA_ROW_HINT_CLASSES} strokeWidth={1.8} />
</a>
);
}
return (
<div className={METADATA_ROW_BOX_CLASSES}>
<a href={url} target="_blank" rel="noreferrer" title={`Open ${attachment.filename}`} className={METADATA_ROW_LABEL_CLASSES}>
{body}
</a>
<div className={METADATA_ROW_CONTROLS_CLASSES}>
<a
href={url}
download
title="Download"
aria-label={`Download ${attachment.filename}`}
className={buttonVariants({ variant: "quiet", size: "icon-sm" })}
>
<DownloadIcon className="size-3.5" strokeWidth={1.8} />
</a>
</div>
</div>
);
};
@@ -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(<AttachmentRows audio={[]} docs={[create(AttachmentSchema, { name: "attachments/doc", ...fields })]} />);
describe("<AttachmentRows> 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");
});
});