Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(attachments): drop the kind from a card that already shows it
Elian Doran9f66a9220846
diff --git a/apps/client/src/services/attachment_role_names.spec.ts b/apps/client/src/services/attachment_role_names.spec.tsindex 957baf2df3..6f36d3e2fd 100644--- a/apps/client/src/services/attachment_role_names.spec.ts+++ b/apps/client/src/services/attachment_role_names.spec.ts@@ -5,30 +5,43 @@ import { describe, expect, it, vi } from "vitest";// what the mapping here is actually about.vi.mock("./i18n.js", () => ({ t: (key: string) => key }));-const { attachmentRoleName } = await import("./attachment_role_names.js");+const { attachmentRoleLabel } = await import("./attachment_role_names.js");-describe("attachmentRoleName", () => {- it("names every role the app creates", () => {- // The types already refuse a role that arrives without a name; this says the same for a role- // added to neither table, which would otherwise reach a reader as its own identifier.- for (const role of Object.keys(ATTACHMENT_ROLES)) {- expect(attachmentRoleName(role), role).toBe(`attachment_roles.${role}`);+describe("attachmentRoleLabel", () => {+ it("names the app's own doing, which nothing else in the card does", () => {+ // "Site icon" is the whole reason that row is not a picture the reader forgot placing.+ for (const role of [ "favicon", "coverImage", "viewConfig", "canvasLibraryItem", "importSource" ]) {+ expect(attachmentRoleLabel(role), role).toBe(`attachment_roles.${role}`);}});- it("shows a role it has never heard of as it was stored", () => {- // Not a name anyone chose, but it is all that is known about the attachment, and a blank- // would leave a gap where every other row says something.- expect(attachmentRoleName("somethingAScriptInvented")).toBe("somethingAScriptInvented");- // The role is whatever was stored, so these reach the lookup too; read off the table rather- // than checked against it, they would answer with something from its prototype.- expect(attachmentRoleName("constructor")).toBe("constructor");- expect(attachmentRoleName("toString")).toBe("toString");+ it("says nothing over what a reader placed themselves", () => {+ // "Image" over a picture, "File" over a PDF's own mark: the icon beside the title has said it.+ expect(attachmentRoleLabel("image")).toBeNull();+ expect(attachmentRoleLabel("file")).toBeNull();});- it("has nothing to say for an attachment without a role", () => {- expect(attachmentRoleName(undefined)).toBe("");- expect(attachmentRoleName(null)).toBe("");- expect(attachmentRoleName("")).toBe("");+ it("follows the icon column rather than answering the question twice", () => {+ // The types already refuse a role given a mark of its own and no name; this says the same at+ // runtime, and the other way round too — so the two tables cannot drift apart.+ for (const [ role, traits ] of Object.entries(ATTACHMENT_ROLES)) {+ expect(attachmentRoleLabel(role) === null, role).toBe(traits.icon === null);+ }+ });++ it("keeps the name of a role it has never heard of", () => {+ // A script's own, or one from a newer version reached over sync. Not a name anyone chose, but+ // the only identification the attachment has.+ expect(attachmentRoleLabel("somethingAScriptInvented")).toBe("somethingAScriptInvented");+ // The role is whatever was stored, so these reach the lookups too; read off a table rather+ // than checked against it, they would answer with one of its prototype's functions.+ expect(attachmentRoleLabel("constructor")).toBe("constructor");+ expect(attachmentRoleLabel("toString")).toBe("toString");+ });++ it("says nothing for an attachment without a role", () => {+ expect(attachmentRoleLabel(undefined)).toBeNull();+ expect(attachmentRoleLabel(null)).toBeNull();+ expect(attachmentRoleLabel("")).toBeNull();});});diff --git a/apps/client/src/services/attachment_role_names.ts b/apps/client/src/services/attachment_role_names.tsindex 177cc62ee6..41d0fada9d 100644--- a/apps/client/src/services/attachment_role_names.ts+++ b/apps/client/src/services/attachment_role_names.ts@@ -1,24 +1,25 @@-import type { AttachmentRole } from "@triliumnext/commons";+import { attachmentRoleTraits, type NamedAttachmentRole } from "@triliumnext/commons";import { t } from "./i18n.js";/**- * What each role is called where a reader sees it.+ * What each role worth naming is called where a reader sees it.*- * A `Record` over the roles rather than a lookup with a default, so a role added to- * `ATTACHMENT_ROLES` does not compile until it has been given a name — the same arrangement the- * user/system split uses. Naming is not derivable from the traits that live with the roles- * themselves, and it is a question only the client has to answer, which is why the names live here- * and not beside the roles.+ * A `Record` over {@link NamedAttachmentRole} rather than a lookup with a default, so a role added to+ * `ATTACHMENT_ROLES` with a mark of its own does not compile until it has been given a name — the same+ * arrangement the user/system split uses. Which roles those are is not decided here: it is read off the+ * icon column, so a role the app created can never end up with a mark nobody can read, and one the+ * reader placed can never end up with a word restating its own picture.+ *+ * Naming is not derivable from the traits that live with the roles themselves, and it is a question only+ * the client has to answer, which is why the names live here and not beside the roles.** Each entry calls `t` on a literal rather than holding the key for a caller to resolve: the tooling* that finds translatable strings reads call sites, and a key handed through a variable is invisible* to it. Thunks rather than strings so the name is read at render, and a language changed under the* app is not left showing the one loaded at startup.*/-const ATTACHMENT_ROLE_NAMES: Record<AttachmentRole, () => string> = {- image: () => t("attachment_roles.image"),- file: () => t("attachment_roles.file"),+const ATTACHMENT_ROLE_NAMES: Record<NamedAttachmentRole, () => string> = {favicon: () => t("attachment_roles.favicon"),coverImage: () => t("attachment_roles.coverImage"),viewConfig: () => t("attachment_roles.viewConfig"),@@ -27,20 +28,26 @@ const ATTACHMENT_ROLE_NAMES: Record<AttachmentRole, () => string> = {};/**- * What to call an attachment of this role.+ * What to call an attachment of this role beside an attachment that is already showing its icon — or+ * nothing, where the role has nothing left to tell.*- * A role nobody here has heard of — a script's own, or one from a newer version reached over sync —- * is shown as it was stored. It is not a name anyone chose, but it is the only thing known about the- * attachment, and saying nothing would leave a row with a gap where every other row says something.+ * Three answers, and each is the interesting one for its case. A role the app created is named: "Site+ * icon" is the whole reason that row is not a picture the reader forgot placing. A role the app knows a+ * reader placed themselves says nothing, because the icon beside the title has already said it and+ * doing so twice on every card is what makes a listing look like a form. A role nobody here has heard+ * of — a script's own, or one from a newer version reached over sync — is shown as it was stored: not a+ * name anyone chose, but the only identification the attachment has, and no icon can stand in for it.*/-export function attachmentRoleName(role: string | undefined | null): string {+export function attachmentRoleLabel(role: string | undefined | null): string | null {if (!role) {- return "";+ return null;}// `hasOwn` rather than a plain lookup: the role is whatever was stored, so `"constructor"`// reaches this, and reading it off the object would answer with something from its prototype.- return Object.hasOwn(ATTACHMENT_ROLE_NAMES, role)- ? ATTACHMENT_ROLE_NAMES[role as AttachmentRole]()- : role;+ if (Object.hasOwn(ATTACHMENT_ROLE_NAMES, role)) {+ return ATTACHMENT_ROLE_NAMES[role as NamedAttachmentRole]();+ }++ return attachmentRoleTraits(role) ? null : role;}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 0ad6b1543a..284ddda18a 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2239,8 +2239,6 @@"unrecognized_role": "Unrecognized attachment role '{{role}}'."},"attachment_roles": {- "image": "Image",- "file": "File","favicon": "Site icon","coverImage": "Cover image","viewConfig": "View settings",diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex 226524db04..406687294e 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -10,7 +10,7 @@ import FAttachment from "../../entities/fattachment";import FNote from "../../entities/fnote";import imageContextMenu from "../../menus/image_context_menu";import { type AttachmentGroup, partitionAttachmentsByGroup } from "../../services/attachment_groups";-import { attachmentRoleName } from "../../services/attachment_role_names";+import { attachmentRoleLabel } from "../../services/attachment_role_names";import content_renderer from "../../services/content_renderer";import dialog from "../../services/dialog";import froca from "../../services/froca";@@ -310,12 +310,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view{/* The media type used to sit here too, spelled out. On a card whose title ends inthe extension and whose content is drawn right below, `image/svg+xml` told areader nothing they could not already see. */}- <div className="attachment-details">- {t("attachment_detail_2.kind_and_size", {- role: attachmentRoleName(attachment.role),- size: utils.formatSize(attachment.contentLength)- })}- </div>+ <AttachmentDetails attachment={attachment} /></div>{scheduledForErasureSince && <DeletionBadge utcDateScheduledForErasureSince={scheduledForErasureSince} />}@@ -352,6 +347,21 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view);}+/**+ * The line under the title. Its size always, and what kind of thing it is only where the icon beside the+ * title has not already said so — see {@link attachmentRoleLabel}.+ */+function AttachmentDetails({ attachment }: { attachment: FAttachment }) {+ const size = utils.formatSize(attachment.contentLength);+ const role = attachmentRoleLabel(attachment.role);++ return (+ <div className="attachment-details">+ {role ? t("attachment_detail_2.kind_and_size", { role, size }) : size}+ </div>+ );+}+/*** What the attachment is, ahead of its name.*diff --git a/packages/commons/src/lib/attachment_roles.ts b/packages/commons/src/lib/attachment_roles.tsindex 49a8dbfb80..85c36a614d 100644--- a/packages/commons/src/lib/attachment_roles.ts+++ b/packages/commons/src/lib/attachment_roles.ts@@ -102,6 +102,20 @@ export const ATTACHMENT_ROLES = {/** A role the app itself creates. Arbitrary strings reach the same fields — see {@link ATTACHMENT_ROLES}. */export type AttachmentRole = keyof typeof ATTACHMENT_ROLES;+/**+ * The roles worth spelling out to a reader: the ones the app created, which is the same set as the ones+ * with a mark of their own.+ *+ * Read off the icon column rather than listed again, so the two cannot drift. A role that cannot say what+ * the thing is has nothing to add once the thing is shown — "Image" over a picture is not worth a word,+ * let alone a word in every language.+ *+ * @see AttachmentRoleTraits.icon+ */+export type NamedAttachmentRole = {+ [Role in AttachmentRole]: typeof ATTACHMENT_ROLES[Role]["icon"] extends null ? never : Role+}[AttachmentRole];+/** The roles whose content is a picture, read off {@link ATTACHMENT_ROLES} rather than listed again. */export type ImageAttachmentRole = {[Role in AttachmentRole]: typeof ATTACHMENT_ROLES[Role]["picture"] extends true ? Role : never