From 337f33cd2d40ccc7c257515671fb5e204b82990d Mon Sep 17 00:00:00 2001 From: Mehrshad Date: Wed, 25 Sep 2024 22:02:17 +0330 Subject: [PATCH] fix: correct file size for zero-byte files --- FileBox/src/App.tsx | 6 ++++-- FileBox/src/lib/utils.ts | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/FileBox/src/App.tsx b/FileBox/src/App.tsx index 6c3fea7..d6fc6d8 100644 --- a/FileBox/src/App.tsx +++ b/FileBox/src/App.tsx @@ -25,7 +25,7 @@ interface ItemProps { name: string onCheckChange: (checked: boolean) => void modifiedDate: Date - icon: React.ReactNode + icon: JSX.Element size?: number onDoubleClick?: () => void } @@ -64,7 +64,9 @@ function Item({ name, onCheckChange, modifiedDate, icon, size, onDoubleClick }: {name} - {moment(modifiedDate).fromNow()} {size && `- ${bytesToSize(size)}`} + + {moment(modifiedDate).fromNow()} {icon.props.name !== "folder" ? `- ${bytesToSize(size)}` : ''} + ) diff --git a/FileBox/src/lib/utils.ts b/FileBox/src/lib/utils.ts index 2dd555d..6c483c0 100644 --- a/FileBox/src/lib/utils.ts +++ b/FileBox/src/lib/utils.ts @@ -1,6 +1,8 @@ -function bytesToSize(bytes: number) { +function bytesToSize(bytes?: number) { + if (bytes === 0 || bytes === undefined) { + return "0 Byte"; + } const sizes = ["B", "KB", "MB", "GB", "TB"]; - if (bytes === 0) return "0 Byte"; const i = Math.floor(Math.log(bytes) / Math.log(1024)); return Math.round(bytes / Math.pow(1024, i)) + sizes[i]; }