Skip to content

Commit

Permalink
fix: correct file size for zero-byte files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mersho committed Sep 25, 2024
1 parent 704b6d4 commit 337f33c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions FileBox/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface ItemProps {
name: string
onCheckChange: (checked: boolean) => void
modifiedDate: Date
icon: React.ReactNode
icon: JSX.Element
size?: number
onDoubleClick?: () => void
}
Expand Down Expand Up @@ -64,7 +64,9 @@ function Item({ name, onCheckChange, modifiedDate, icon, size, onDoubleClick }:
<span className="absolute left-1/2 transform -translate-x-1/2 -top-8 bg-gray-800 text-white text-xs rounded py-1 px-2 opacity-0 group-hover:opacity-100 transition-opacity delay-1000 duration-300 whitespace-nowrap z-10 overflow-hidden w-0 group-hover:w-auto">
{name}
</span>
<span className="mt-1 text-xs text-gray-500 dark:text-gray-400">{moment(modifiedDate).fromNow()} {size && `- ${bytesToSize(size)}`}</span>
<span className="mt-1 text-xs text-gray-500 dark:text-gray-400">
{moment(modifiedDate).fromNow()} {icon.props.name !== "folder" ? `- ${bytesToSize(size)}` : ''}
</span>
</div>
</div>
)
Expand Down
6 changes: 4 additions & 2 deletions FileBox/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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];
}
Expand Down

0 comments on commit 337f33c

Please sign in to comment.