refactor(pdf_attachments): deduplicate font size

This commit is contained in:
Elian Doran 2025-12-29 23:01:19 +02:00
parent 43a749b6a7
commit 6513e2cfca
No known key found for this signature in database
2 changed files with 9 additions and 14 deletions

View File

@ -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]) {

View File

@ -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]}`;
}