aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPorter Averett <porter@averett.net>2023-09-25 20:16:04 -0700
committerPorter Averett <porter@averett.net>2023-09-25 20:16:04 -0700
commitd98fd93dbb3f36b67b76d5227328f8626ac55013 (patch)
tree35310961ba6332c90d68e5ddd38ce2c81ba7227a /src
parent77ec3da43c11449d2d4ae64c1413505f7a7741a2 (diff)
downloadslskd-d98fd93dbb3f36b67b76d5227328f8626ac55013.tar.gz
fix: transfer size column is too narrow
Diffstat (limited to 'src')
-rw-r--r--src/web/src/components/Transfers/TransferList.js21
-rw-r--r--src/web/src/lib/util.js14
-rw-r--r--src/web/src/lib/util.test.js5
3 files changed, 21 insertions, 19 deletions
diff --git a/src/web/src/components/Transfers/TransferList.js b/src/web/src/components/Transfers/TransferList.js
index c35f512f..1efe5900 100644
--- a/src/web/src/components/Transfers/TransferList.js
+++ b/src/web/src/components/Transfers/TransferList.js
@@ -4,7 +4,7 @@ import {
Checkbox,
} from 'semantic-ui-react';
-import { formatBytes, getFileName } from '../../lib/util';
+import { formatBytes, formatBytesAsUnit, getFileName } from '../../lib/util';
import {
Header,
@@ -37,23 +37,10 @@ const isRetryableState = (state) => getColor(state).color === 'red';
const isQueuedState = (state) => state.includes('Queued');
const formatBytesTransferred = ({ transferred, size }) => {
- const [t, tExt] = formatBytes(transferred).split(' ');
- const [s, sExt] = formatBytes(size).split(' ');
+ const [s, sExt] = formatBytes(size, 1).split(' ');
+ const t = formatBytesAsUnit(transferred, 1, sExt);
- const fmt = (n) => parseFloat(n).toFixed(2);
-
- // if less than 1 MB has been transferred, don't include decimals
- if (tExt === 'KB') {
- return `${t} KB/${fmt(s)} ${sExt}`;
- }
-
- // if the suffix for size and transferred doesn't match, include
- // the suffix for each
- if (tExt !== sExt) {
- return `${fmt(t)} ${tExt}/${fmt(s)} ${sExt}`;
- }
-
- return `${fmt(t)}/${fmt(s)} ${sExt}`;
+ return `${t}/${s} ${sExt}`;
};
class TransferList extends Component {
diff --git a/src/web/src/lib/util.js b/src/web/src/lib/util.js
index 974915ed..f5fd53fa 100644
--- a/src/web/src/lib/util.js
+++ b/src/web/src/lib/util.js
@@ -9,12 +9,22 @@ export const formatSeconds = (seconds) =>
return date.toTimeString().replace(/.*(\d{2}:\d{2}).*/, '$1');
};
+export const formatBytesAsUnit = (bytes, decimals = 2, unit) => {
+ if (unit === 'B') return bytes + ' ' + unit;
+
+ const k = 1024;
+ const dm = decimals < 0 ? 0 : decimals;
+ const sizes = {'KB': 1, 'MB': 2, 'GB': 3, 'TB': 4, 'PB': 5, 'EB': 6, 'ZB': 7, 'YB': 8};
+
+ return parseFloat((bytes / Math.pow(k, sizes[unit])).toFixed(dm));
+}
+
export const formatBytes = (bytes, decimals = 2) => {
- if (bytes === 0) return '0 Bytes';
+ if (bytes === 0) return '0 B';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
- const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
diff --git a/src/web/src/lib/util.test.js b/src/web/src/lib/util.test.js
new file mode 100644
index 00000000..8b912530
--- /dev/null
+++ b/src/web/src/lib/util.test.js
@@ -0,0 +1,5 @@
+import * as utils from './util';
+
+it('converts bytes to specified unit', () => {
+ expect(utils.formatBytesAsUnit(1234567, 2, 'MB')).toBe(1.18);
+}); \ No newline at end of file