Skip to content

Build your personal knowledge base with Trilium Notes

feat(resources): one unit for the sizes on the downloaded resources page

Elian Doran93236e389292
diff --git a/apps/client/src/services/utils.spec.ts b/apps/client/src/services/utils.spec.ts
index 75bf9893aa..9289bee026 100644
--- a/apps/client/src/services/utils.spec.ts
+++ b/apps/client/src/services/utils.spec.ts
@@ -127,6 +127,13 @@ describe("formatSize", () => {
expect(formatSize(0, 2, total)).toBe("0.00 MiB");
});
+ it("shows a size too small for the places of its unit as below the smallest step", () => {
+ const total = 2.5 * 1024 ** 3;
+ expect(formatSize(643_854, 2, total)).toBe("<0.01 GiB");
+ expect(formatSize(643_854, 0, total)).toBe("<1 GiB");
+ expect(formatSize(0, 2, total)).toBe("0.00 GiB");
+ });
+
it("keeps the places it is given, trailing zeros and all", () => {
// For a counter that is still climbing: dropping the zero shortens the text on every other
// update, and the line shifts about while it is being read.
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index 43c54becca..a83eb2421a 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -278,6 +278,10 @@ export function formatSize(size: number | null | undefined, decimals?: number, u
const value = size / Math.pow(1024, unit);
// Nothing below a byte to show, however many places were asked for.
const places = decimals !== undefined && unit === 0 ? 0 : decimals;
+ const step = Math.pow(10, -(places ?? 2));
+ if (size > 0 && value < step / 2) {
+ return `<${step.toFixed(places ?? 2)} ${SIZE_UNITS[unit]}`;
+ }
return `${places === undefined ? Math.round(value * 100) / 100 : value.toFixed(places)} ${SIZE_UNITS[unit]}`;
}
diff --git a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx b/apps/client/src/widgets/type_widgets/options/resources.spec.tsx
index f0ad33ae90..09d8016310 100644
--- a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/resources.spec.tsx
@@ -50,7 +50,10 @@ let host: HTMLElement;
beforeEach(() => {
mocks.resources = [
- resource({ id: "test.installed", installed: true, downloadedSize: 10 * MIB, directory: "/data/resources/test.installed-abc" }),
+ resource({
+ id: "test.installed", installed: true, size: 2048 * MIB, downloadedSize: 2048 * MIB,
+ directory: "/data/resources/test.installed-abc"
+ }),
resource({ id: "test.partial", downloadedSize: 4 * MIB }),
resource({ id: "test.missing" })
];
@@ -71,7 +74,7 @@ async function open() {
}
describe("ResourcesSettings", () => {
- it("lists the resources installed or partly downloaded, with their status and folder", async () => {
+ it("lists the resources installed or partly downloaded, with their status, folder and size", async () => {
await open();
const rows = [ ...host.querySelectorAll(".resources-item") ];
@@ -83,7 +86,8 @@ describe("ResourcesSettings", () => {
]);
expect(rows.map((row) => row.querySelector(".directory-stub")?.textContent ?? null))
.toEqual([ "/data/resources/test.installed-abc", null ]);
- expect(host.querySelector(".resources-total .resources-size")?.textContent).toBe("14 MiB");
+ expect([ ...host.querySelectorAll(".resources-size") ].map((size) => size.textContent))
+ .toEqual([ "2.00 GiB", "0.01 GiB", "2.00 GiB" ]);
});
it("shows an empty state when nothing is downloaded", async () => {
diff --git a/apps/client/src/widgets/type_widgets/options/resources.tsx b/apps/client/src/widgets/type_widgets/options/resources.tsx
index aa6e9b7eb0..1d55d7ba49 100644
--- a/apps/client/src/widgets/type_widgets/options/resources.tsx
+++ b/apps/client/src/widgets/type_widgets/options/resources.tsx
@@ -62,24 +62,26 @@ function useResources() {
return resources;
}
+/** The resources and the space they use, every size in one unit so that the column adds up. */
function ResourceRows({ resources }: { resources: ResourceInfo[] }) {
const used = resources.reduce((total, { downloadedSize }) => total + downloadedSize, 0);
+ const unitOf = Math.max(used, ...resources.map(({ size }) => size));
return (
<>
{resources.map((resource) => (
- <ResourceRow key={resource.id} resource={resource} />
+ <ResourceRow key={resource.id} resource={resource} unitOf={unitOf} />
))}
<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>
</div>
- <span className="tn-card-option-value resources-size">{formatSize(used)}</span>
+ <span className="tn-card-option-value resources-size">{formatSize(used, 2, unitOf)}</span>
</CardSection>
</>
);
}
-function ResourceRow({ resource }: { resource: ResourceInfo }) {
+function ResourceRow({ resource, unitOf }: { resource: ResourceInfo; unitOf: number }) {
return (
<CardSection className="tn-card-option resources-item">
<Icon icon={resource.icon} className="resources-icon" />
@@ -98,7 +100,7 @@ function ResourceRow({ resource }: { resource: ResourceInfo }) {
)}
</div>
<div className="tn-card-option-value resources-status">
- <span className="resources-size">{formatSize(resource.size)}</span>
+ <span className="resources-size">{formatSize(resource.size, 2, unitOf)}</span>
<small className="tn-card-option-description">{getStatusText(resource)}</small>
</div>
</CardSection>