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): state the free disk space under the space used
Elian Doranae5522b708ac
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 59e04c97a6..a3934f5c75 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4614,7 +4614,7 @@"partially_downloaded": "Already downloaded","sources": "From {{sources}}","total": "Download size",- "free_space": "Free space: {{size}}",+ "free_space": "Free space on disk: {{size}}","not_enough_space": "The download needs {{size}}, but only {{free}} is free on this device. Free up some space, then close this and try again.","progress": "{{received}} of {{total}}","current_file": "Downloading {{file}}…",diff --git a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx b/apps/client/src/widgets/type_widgets/options/resources.spec.tsxindex 2d319fd9e2..91ea656b70 100644--- a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/resources.spec.tsx@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const mocks = vi.hoisted(() => ({resources: [] as ResourceInfo[],+ freeBytes: null as number | null,requests: 0,confirmed: true,removed: [] as string[],@@ -20,7 +21,7 @@ vi.mock("../../../services/server", () => ({return [];}mocks.requests++;- return { resources: mocks.resources, freeBytes: null, download: null };+ return { resources: mocks.resources, freeBytes: mocks.freeBytes, download: null };},remove: async (url: string) => {mocks.removed.push(url);@@ -78,6 +79,7 @@ beforeEach(() => {resource({ id: "test.missing" })];mocks.requests = 0;+ mocks.freeBytes = null;mocks.confirmed = true;mocks.removed = [];mocks.electron = true;@@ -147,6 +149,18 @@ describe("ResourcesSettings", () => {expect(mocks.requests).toBe(2);});+ it("states the free space below the space used, when it can be read", async () => {+ const description = () => host.querySelector(".resources-total .tn-card-option-description");+ mocks.freeBytes = 38 * 1024 * MIB;+ await open();+ expect(description()?.textContent).toBe(`resource_download.free_space {"size":"38 GiB"}`);++ act(() => render(null, host));+ mocks.freeBytes = null;+ await open();+ expect(description()).toBeNull();+ });+it("shows an empty state when nothing is downloaded", async () => {mocks.resources = [ resource({ id: "test.missing" }) ];await open();diff --git a/apps/client/src/widgets/type_widgets/options/resources.tsx b/apps/client/src/widgets/type_widgets/options/resources.tsxindex b1eec65b7c..66f59a4727 100644--- a/apps/client/src/widgets/type_widgets/options/resources.tsx+++ b/apps/client/src/widgets/type_widgets/options/resources.tsx@@ -17,19 +17,23 @@ import OptionsPageHeader from "./components/OptionsPageHeader";/** Lists the resources installed on this device, and the ones an interrupted download left partly fetched. */export default function ResourcesSettings() {- const [ resources, refresh ] = useResources();+ const [ status, refresh ] = useResources();return (<><OptionsPageHeader />- {resources && (+ {status && (<CardclassName="resources-list"heading={t("resources_options.list_title")}description={t("resources_options.list_description")}>- {resources.length > 0 ? (- <ResourceRows resources={resources} onDeleted={refresh} />+ {status.resources.length > 0 ? (+ <ResourceRows+ resources={status.resources}+ freeBytes={status.freeBytes}+ onDeleted={refresh}+ />) : (<CardSection><NoItems icon="bx bx-download" text={t("resources_options.no_resources")} size="small" />@@ -41,13 +45,17 @@ export default function ResourcesSettings() {);}-/** The resources installed or partly downloaded, fetched again whenever a download ends. */+/**+ * The resources installed or partly downloaded and the free space where they are stored, fetched+ * again whenever a download ends.+ */function useResources() {- const [ resources, setResources ] = useState<ResourceInfo[]>();+ const [ status, setStatus ] = useState<Pick<ResourcesStatusResponse, "resources" | "freeBytes">>();const refresh = useCallback(() => {- void server.get<ResourcesStatusResponse>("resources")- .then((status) => setResources(status.resources.filter(- ({ downloadedSize }) => downloadedSize > 0)));+ void server.get<ResourcesStatusResponse>("resources").then(({ resources, freeBytes }) => setStatus({+ resources: resources.filter(({ downloadedSize }) => downloadedSize > 0),+ freeBytes+ }));}, []);useEffect(() => {@@ -61,11 +69,17 @@ function useResources() {return () => unsubscribeToMessage(handler);}, [ refresh ]);- return [ resources, refresh ] as const;+ return [ status, refresh ] as const;}/** The resources and the space they use, every size in one unit so that the column adds up. */-function ResourceRows({ resources, onDeleted }: { resources: ResourceInfo[]; onDeleted: () => void }) {+interface ResourceRowsProps {+ resources: ResourceInfo[];+ freeBytes: number | null;+ onDeleted: () => void;+}++function ResourceRows({ resources, freeBytes, onDeleted }: ResourceRowsProps) {const used = resources.reduce((total, { downloadedSize }) => total + downloadedSize, 0);const unitOf = Math.max(used, ...resources.map(({ size }) => size));return (@@ -76,6 +90,11 @@ function ResourceRows({ resources, onDeleted }: { resources: ResourceInfo[]; onD<CardSection className="tn-card-option resources-total"><div className="tn-card-option-label"><span className="tn-card-option-title">{t("resources_options.space_used")}</span>+ {freeBytes !== null && (+ <small className="tn-card-option-description">+ {t("resource_download.free_space", { size: formatSize(freeBytes) })}+ </small>+ )}</div><span className="tn-card-option-value resources-size">{formatSize(used, 2, unitOf)}</span><div className="resources-actions" />