Skip to content

Build your personal knowledge base with Trilium Notes

refactor(pdf_attachments): deduplicate font size

Elian Doran6513e2cfca5b
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index 9dff8fef23..8d9ce1014f 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -187,13 +187,15 @@ export function formatSize(size: number | null | undefined) {
return "";
}
- size = Math.max(Math.round(size / 1024), 1);
-
- if (size < 1024) {
- return `${size} KiB`;
+ if (size === 0) {
+ return "0 B";
}
- return `${Math.round(size / 102.4) / 10} MiB`;
+ const k = 1024;
+ const sizes = ["B", "KB", "MB", "GB"];
+ const i = Math.floor(Math.log(size) / Math.log(k));
+
+ return `${Math.round((size / Math.pow(k, i)) * 100) / 100} ${sizes[i]}`;
}
function toObject<T, R>(array: T[], fn: (arg0: T) => [key: string, value: R]) {
diff --git a/apps/client/src/widgets/sidebar/PdfAttachments.tsx b/apps/client/src/widgets/sidebar/PdfAttachments.tsx
index 1627814afc..3269db6275 100644
--- a/apps/client/src/widgets/sidebar/PdfAttachments.tsx
+++ b/apps/client/src/widgets/sidebar/PdfAttachments.tsx
@@ -1,5 +1,6 @@
import "./PdfAttachments.css";
+import { formatSize } from "../../services/utils";
import { useActiveNoteContext, useGetContextData, useNoteProperty } from "../react/hooks";
import Icon from "../react/Icon";
import RightPanelWidget from "./RightPanelWidget";
@@ -52,7 +53,7 @@ function PdfAttachmentItem({
attachment: AttachmentInfo;
onDownload: (filename: string) => void;
}) {
- const sizeText = formatFileSize(attachment.size);
+ const sizeText = formatSize(attachment.size);
return (
<div className="pdf-attachment-item" onClick={() => onDownload(attachment.filename)}>
@@ -65,11 +66,3 @@ function PdfAttachmentItem({
</div>
);
}
-
-function formatFileSize(bytes: number): string {
- if (bytes === 0) return "0 B";
- const k = 1024;
- const sizes = ["B", "KB", "MB", "GB"];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return `${Math.round((bytes / Math.pow(k, i)) * 100) / 100 } ${ sizes[i]}`;
-}