Skip to content

Build your personal knowledge base with Trilium Notes

feat(resources): show the download sizes in one unit

Elian Doran9e75a71c289c
diff --git a/apps/client/src/services/utils.spec.ts b/apps/client/src/services/utils.spec.ts
index b1f8ca4b06..75bf9893aa 100644
--- a/apps/client/src/services/utils.spec.ts
+++ b/apps/client/src/services/utils.spec.ts
@@ -120,6 +120,13 @@ describe("formatSize", () => {
expect(formatSize(5 * 1024 ** 5)).toBe("5120 TiB");
});
+ it("shows a size in another size's unit, for figures read down a column", () => {
+ const total = 711_000_000;
+ expect(formatSize(643_854, 2, total)).toBe("0.61 MiB");
+ expect(formatSize(40_257_283, 2, total)).toBe("38.39 MiB");
+ expect(formatSize(0, 2, total)).toBe("0.00 MiB");
+ });
+
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 a9ce9240aa..43c54becca 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -261,17 +261,20 @@ const SIZE_UNITS = ["B", "KiB", "MiB", "GiB", "TiB"];
* what is not needed, which reads best for a size at rest. Pass a number for a counter that is
* still climbing: there, dropping a trailing zero shortens the text on every other update and the
* line shifts about while it is being read.
+ * @param unitOf a size whose unit to show `size` in instead, so that a column of sizes shares one
+ * unit and can be summed by eye: the largest of them, or their total.
*/
-export function formatSize(size: number | null | undefined, decimals?: number) {
+export function formatSize(size: number | null | undefined, decimals?: number, unitOf?: number) {
if (size === null || size === undefined) {
return "";
}
- if (size <= 0) {
+ const reference = unitOf ?? size;
+ if (reference <= 0) {
return "0 B";
}
- const unit = Math.min(Math.floor(Math.log(size) / Math.log(1024)), SIZE_UNITS.length - 1);
+ const unit = Math.min(Math.floor(Math.log(reference) / Math.log(1024)), SIZE_UNITS.length - 1);
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;
diff --git a/apps/client/src/widgets/dialogs/resource_download.tsx b/apps/client/src/widgets/dialogs/resource_download.tsx
index 7819225da8..9babdbda49 100644
--- a/apps/client/src/widgets/dialogs/resource_download.tsx
+++ b/apps/client/src/widgets/dialogs/resource_download.tsx
@@ -138,12 +138,13 @@ export default function ResourceDownloadDialog() {
function ResourceList({ status, remaining }: { status: ResourcesStatusResponse; remaining: number }) {
const missing = status.resources.filter(({ installed }) => !installed);
+ const unitOf = Math.max(remaining, ...missing.map(({ size }) => size));
return (
<>
<p>{t("resource_download.intro")}</p>
<Card className="resource-download-list">
- {missing.map((resource) => <ResourceItem key={resource.id} resource={resource} />)}
+ {missing.map((resource) => <ResourceItem key={resource.id} resource={resource} unitOf={unitOf} />)}
<CardSection className="tn-card-option resource-download-total">
<div className="tn-card-option-label">
<span className="tn-card-option-title">{t("resource_download.total")}</span>
@@ -156,7 +157,7 @@ function ResourceList({ status, remaining }: { status: ResourcesStatusResponse;
)}
</div>
<span className="tn-card-option-value resource-download-size">
- {formatSize(remaining)}
+ {formatSize(remaining, 2, unitOf)}
</span>
</CardSection>
</Card>
@@ -165,7 +166,7 @@ function ResourceList({ status, remaining }: { status: ResourcesStatusResponse;
}
/** One resource as a settings row: its icon, what it is and where it comes from, and its size. */
-function ResourceItem({ resource }: { resource: ResourceInfo }) {
+function ResourceItem({ resource, unitOf }: { resource: ResourceInfo; unitOf: number }) {
return (
<CardSection className="tn-card-option resource-download-item">
<Icon icon={resource.icon} className="resource-download-icon" />
@@ -183,7 +184,7 @@ function ResourceItem({ resource }: { resource: ResourceInfo }) {
</small>
</div>
<span className="tn-card-option-value resource-download-size">
- {formatSize(resource.size)}
+ {formatSize(resource.size, 2, unitOf)}
</span>
</CardSection>
);