Vary number of decimals of file sizes

This commit is contained in:
Fedor Indutny
2025-02-11 15:01:27 -08:00
committed by GitHub
parent 8c4a7cb2be
commit 1364ba5784
3 changed files with 13 additions and 11 deletions

View File

@@ -775,10 +775,9 @@ export function Lightbox({
{attachment.totalDownloaded && attachment.size {attachment.totalDownloaded && attachment.size
? i18n('icu:lightBoxDownloading', { ? i18n('icu:lightBoxDownloading', {
downloaded: formatFileSize( downloaded: formatFileSize(
attachment.totalDownloaded, attachment.totalDownloaded
2
), ),
total: formatFileSize(attachment.size, 2), total: formatFileSize(attachment.size),
}) })
: undefined} : undefined}
</Toast> </Toast>

View File

@@ -126,7 +126,7 @@ export function AttachmentDetailPill({
); );
text = ( text = (
<div className="AttachmentDetailPill__text-wrapper"> <div className="AttachmentDetailPill__text-wrapper">
{formatFileSize(totalSize, 2)} {formatFileSize(totalSize)}
</div> </div>
); );
} else if (totalDownloadedSize > 0) { } else if (totalDownloadedSize > 0) {
@@ -148,9 +148,9 @@ export function AttachmentDetailPill({
text = ( text = (
<div className="AttachmentDetailPill__text-wrapper"> <div className="AttachmentDetailPill__text-wrapper">
{totalDownloadedSize > 0 && areAnyPending {totalDownloadedSize > 0 && areAnyPending
? `${formatFileSize(totalDownloadedSize, 2)} / ` ? `${formatFileSize(totalDownloadedSize)} / `
: undefined} : undefined}
{formatFileSize(totalSize, 2)} {formatFileSize(totalSize)}
</div> </div>
); );
} else { } else {
@@ -165,7 +165,7 @@ export function AttachmentDetailPill({
); );
text = ( text = (
<div className="AttachmentDetailPill__text-wrapper"> <div className="AttachmentDetailPill__text-wrapper">
{formatFileSize(totalSize, 2)} {formatFileSize(totalSize)}
</div> </div>
); );
} }
@@ -191,9 +191,9 @@ export function AttachmentDetailPill({
<div className="AttachmentDetailPill"> <div className="AttachmentDetailPill">
<div className="AttachmentDetailPill__text-wrapper"> <div className="AttachmentDetailPill__text-wrapper">
{totalDownloadedSize > 0 && areAnyPending {totalDownloadedSize > 0 && areAnyPending
? `${formatFileSize(totalDownloadedSize, 2)} / ` ? `${formatFileSize(totalDownloadedSize)} / `
: undefined} : undefined}
{formatFileSize(totalSize, 2)} {formatFileSize(totalSize)}
{isGif ? ' · GIF' : undefined} {isGif ? ' · GIF' : undefined}
</div> </div>
</div> </div>

View File

@@ -2,6 +2,9 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import filesize from 'filesize'; import filesize from 'filesize';
export function formatFileSize(size: number, decimals = 0): string { // Intentional, `filesize` uses `jedec` standard by default
return filesize(size, { round: decimals }); const MB = 1000 * 1000;
export function formatFileSize(size: number): string {
return filesize(size, { round: size < MB ? 0 : 1 });
} }