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(resources): open a resource's folder from a button on its row
Elian Doran4fe8abf2351f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 573e1858fe..e1be197d14 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4646,6 +4646,7 @@"space_used": "Space used","no_resources": "Nothing has been downloaded yet.","delete": "Delete",- "delete_confirmation": "Delete {{title}}? It is downloaded again the next time a feature needs it."+ "delete_confirmation": "Delete {{title}}? It is downloaded again the next time a feature needs it.",+ "open_folder": "Open folder"}}diff --git a/apps/client/src/widgets/type_widgets/options/resources.css b/apps/client/src/widgets/type_widgets/options/resources.cssindex 9cd4a91ce1..be42853711 100644--- a/apps/client/src/widgets/type_widgets/options/resources.css+++ b/apps/client/src/widgets/type_widgets/options/resources.css@@ -1,5 +1,6 @@.resources-list {- .resources-item {+ .resources-item,+ .resources-total {gap: 12px;}@@ -9,10 +10,6 @@color: var(--muted-text-color);}- .resources-directory {- word-break: break-all;- }-.resources-status {display: flex;flex-direction: column;@@ -21,6 +18,16 @@text-align: end;}+ .resources-actions {+ --resources-actions-gap: 12px;++ display: flex;+ flex-shrink: 0;+ justify-content: flex-end;+ gap: var(--resources-actions-gap);+ width: calc(2 * var(--icon-button-size, 37px) + var(--resources-actions-gap));+ }+.resources-size {font-variant-numeric: tabular-nums;white-space: nowrap;diff --git a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx b/apps/client/src/widgets/type_widgets/options/resources.spec.tsxindex d05b8cd158..2d319fd9e2 100644--- a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/resources.spec.tsx@@ -8,6 +8,8 @@ const mocks = vi.hoisted(() => ({requests: 0,confirmed: true,removed: [] as string[],+ electron: true,+ opened: [] as string[],handlers: [] as ((message: WebSocketMessage) => void)[]}));@@ -46,8 +48,17 @@ vi.mock("../../../services/i18n", () => ({t: (key: string, args?: Record<string, string>) => args ? `${key} ${JSON.stringify(args)}` : key}));-vi.mock("../../react/DirectoryLink", () => ({- default: ({ directory }: { directory: string }) => <a className="directory-stub">{directory}</a>+vi.mock("../../../services/utils", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../../../services/utils")>()),+ isElectron: () => mocks.electron+}));++vi.mock("../../../services/open", () => ({+ default: {+ openDirectory: async (directory: string) => {+ mocks.opened.push(directory);+ }+ }}));vi.mock("./components/OptionsPageHeader", () => ({ default: () => <div className="header-stub" /> }));@@ -69,6 +80,8 @@ beforeEach(() => {mocks.requests = 0;mocks.confirmed = true;mocks.removed = [];+ mocks.electron = true;+ mocks.opened = [];mocks.handlers = [];host = document.body.appendChild(document.createElement("div"));});@@ -85,7 +98,7 @@ async function open() {}describe("ResourcesSettings", () => {- it("lists the resources installed or partly downloaded, with their status, folder and size", async () => {+ it("lists the resources installed or partly downloaded, with their status and size", async () => {await open();const rows = [ ...host.querySelectorAll(".resources-item") ];@@ -95,12 +108,26 @@ describe("ResourcesSettings", () => {"resources_options.installed",`resources_options.partially_downloaded {"downloaded":"4.00 MiB","size":"10 MiB"}`]);- expect(rows.map((row) => row.querySelector(".directory-stub")?.textContent ?? null))- .toEqual([ "/data/resources/test.installed-abc", null ]);expect([ ...host.querySelectorAll(".resources-size") ].map((size) => size.textContent)).toEqual([ "2.00 GiB", "0.01 GiB", "2.00 GiB" ]);});+ it("opens the folder of an installed resource, on desktop only", async () => {+ await open();+ const buttons = () => [ ...host.querySelectorAll(".resources-item") ]+ .map((row) => row.querySelector<HTMLElement>(".bx-folder-open"));+ const [ installed, partial ] = buttons();+ expect(installed).not.toBeNull();+ expect(partial).toBeNull();+ await act(async () => installed?.click());+ expect(mocks.opened).toEqual([ "/data/resources/test.installed-abc" ]);++ act(() => render(null, host));+ mocks.electron = false;+ await open();+ expect(buttons()).toEqual([ null, null ]);+ });+it("deletes a resource once confirmed, then reads the list again", async () => {await open();const click = async () => {diff --git a/apps/client/src/widgets/type_widgets/options/resources.tsx b/apps/client/src/widgets/type_widgets/options/resources.tsxindex 34ed760668..b1eec65b7c 100644--- a/apps/client/src/widgets/type_widgets/options/resources.tsx+++ b/apps/client/src/widgets/type_widgets/options/resources.tsx@@ -5,12 +5,12 @@ import { useCallback, useEffect, useState } from "preact/hooks";import dialogService from "../../../services/dialog";import { t } from "../../../services/i18n";+import openService from "../../../services/open";import server from "../../../services/server";-import { formatSize } from "../../../services/utils";+import { formatSize, isElectron } from "../../../services/utils";import { subscribeToMessages, unsubscribeToMessage } from "../../../services/ws";import ActionButton from "../../react/ActionButton";import { Card, CardSection } from "../../react/Card";-import DirectoryLink from "../../react/DirectoryLink";import Icon from "../../react/Icon";import NoItems from "../../react/NoItems";import OptionsPageHeader from "./components/OptionsPageHeader";@@ -78,6 +78,7 @@ function ResourceRows({ resources, onDeleted }: { resources: ResourceInfo[]; onD<span className="tn-card-option-title">{t("resources_options.space_used")}</span></div><span className="tn-card-option-value resources-size">{formatSize(used, 2, unitOf)}</span>+ <div className="resources-actions" /></CardSection></>);@@ -90,6 +91,7 @@ interface ResourceRowProps {}function ResourceRow({ resource, unitOf, onDeleted }: ResourceRowProps) {+ const { directory } = resource;async function remove() {if (!await dialogService.confirm(t("resources_options.delete_confirmation", { title: resource.title }))) {return;@@ -109,22 +111,26 @@ function ResourceRow({ resource, unitOf, onDeleted }: ResourceRowProps) {{" · "}{t("resource_download.license", { license: resource.license })}</small>- {resource.directory && (- <small className="tn-card-option-description resources-directory">- <DirectoryLink directory={resource.directory} />- </small>- )}</div><div className="tn-card-option-value resources-status"><span className="resources-size">{formatSize(resource.size, 2, unitOf)}</span><small className="tn-card-option-description">{getStatusText(resource)}</small></div>- <ActionButton- className="destructive-action-icon"- icon="bx bx-trash"- text={t("resources_options.delete")}- onClick={() => void remove()}- />+ <div className="resources-actions">+ {directory && isElectron() && (+ <ActionButton+ icon="bx bx-folder-open"+ text={t("resources_options.open_folder")}+ onClick={() => void openService.openDirectory(directory)}+ />+ )}+ <ActionButton+ className="destructive-action-icon"+ icon="bx bx-trash"+ text={t("resources_options.delete")}+ onClick={() => void remove()}+ />+ </div></CardSection>);}