Skip to content

Commit 7e98a85

Browse files
authored
fix: file size formatting (#2357)
Quick fix for some stuff that got broken after removing `pretty-bytes` in #2301: 1. Sizes are now 1024-based (as opposed for 1000-based) to align with backend. 2. Add support for zero-precision,
1 parent a8aa524 commit 7e98a85

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/components/Attachment/__tests__/VoiceRecording.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('VoiceRecordingPlayer', () => {
5959

6060
it('should fallback to file size, if duration is not available', () => {
6161
const { getByTestId } = renderComponent({
62-
attachment: { ...attachment, duration: undefined, file_size: 60000 },
62+
attachment: { ...attachment, duration: undefined, file_size: 60 * 1024 },
6363
});
6464
expect(getByTestId('file-size-indicator')).toHaveTextContent('60 kB');
6565
});
@@ -149,7 +149,7 @@ describe('QuotedVoiceRecording', () => {
149149
});
150150
it('should fallback to file size, if duration is not available', () => {
151151
const { queryByTestId } = renderComponent({
152-
attachment: { ...attachment, duration: undefined, file_size: 60000 },
152+
attachment: { ...attachment, duration: undefined, file_size: 60 * 1024 },
153153
isQuoted: true,
154154
});
155155
expect(queryByTestId('file-size-indicator')).toHaveTextContent('60 kB');

src/components/Attachment/__tests__/__snapshots__/File.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports[`File should render File component in V1 1`] = `
3232
className="str-chat__message-attachment-file--item-size"
3333
data-testid="file-size-indicator"
3434
>
35-
1.34 kB
35+
1.31 kB
3636
</span>
3737
</div>
3838
</div>
@@ -113,7 +113,7 @@ exports[`File should render File component in V2 1`] = `
113113
className="str-chat__message-attachment-file--item-size"
114114
data-testid="file-size-indicator"
115115
>
116-
1.34 kB
116+
1.31 kB
117117
</span>
118118
</div>
119119
</div>

src/components/MessageInput/hooks/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,7 @@ export function prettifyFileSize(bytes: number, precision = 3) {
212212
const units = ['B', 'kB', 'MB', 'GB'];
213213
const exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);
214214
const mantissa = bytes / 1024 ** exponent;
215-
return `${mantissa.toPrecision(precision)} ${units[exponent]}`;
215+
const formattedMantissa =
216+
precision === 0 ? Math.round(mantissa).toString() : mantissa.toPrecision(precision);
217+
return `${formattedMantissa} ${units[exponent]}`;
216218
}

0 commit comments

Comments
 (0)