Skip to content

Commit 1eee983

Browse files
only21milclaude
andcommitted
fix(mobile): client storage sizes no longer read "1024 KB"
The client storage formatter picked the unit before rounding and stopped at MB, so 1048575 bytes read "1024 KB" and 5 GiB read "5120 MB". Pick the unit after rounding and continue through GB and TB, keeping the existing precision rule. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8760d06 commit 1eee983

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎apps/mobile/src/features/settings/SettingsClientStorageRouteScreen.tsx‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useSavedRemoteConnections } from "../../state/use-remote-environment-re
2121
import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
2222
import { SettingsSection } from "./components/SettingsSection";
2323
import { SettingsScreen } from "./components/SettingsScreen";
24+
import { formatBytes } from "./lib/format-bytes";
2425

2526
export function SettingsClientStorageRouteScreen() {
2627
const insets = useSafeAreaInsets();
@@ -179,9 +180,3 @@ function CacheEnvironmentRow(props: {
179180
</View>
180181
);
181182
}
182-
183-
function formatBytes(bytes: number): string {
184-
if (bytes < 1024) return `${bytes} B`;
185-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10 * 1024 ? 1 : 0)} KB`;
186-
return `${(bytes / (1024 * 1024)).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0)} MB`;
187-
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { formatBytes } from "./format-bytes";
4+
5+
describe("formatBytes", () => {
6+
it.each([
7+
[1_048_575, "1.0 MB"],
8+
[1_073_741_823, "1.0 GB"],
9+
])("formats %d bytes as %s once rounding reaches the next unit", (bytes, label) => {
10+
expect(formatBytes(bytes)).toBe(label);
11+
});
12+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const UNITS = ["KB", "MB", "GB", "TB"] as const;
2+
3+
function formatScaled(value: number): string {
4+
return value.toFixed(value < 10 ? 1 : 0);
5+
}
6+
7+
export function formatBytes(bytes: number): string {
8+
if (bytes < 1024) return `${bytes} B`;
9+
let value = bytes / 1024;
10+
let unitIndex = 0;
11+
// Pick the unit after rounding, so 1023.9 KB reads "1.0 MB", not "1024 KB".
12+
while (Number(formatScaled(value)) >= 1024 && unitIndex < UNITS.length - 1) {
13+
value /= 1024;
14+
unitIndex += 1;
15+
}
16+
return `${formatScaled(value)} ${UNITS[unitIndex]}`;
17+
}

0 commit comments

Comments
 (0)