Skip to content

Commit c309024

Browse files
fix(mobile): usage clock reads the minute tick as UTC
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b3add55 commit c309024

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎apps/mobile/src/features/settings/settings-environment-filter.tsx‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { EnvironmentId } from "@t3tools/contracts";
22
import { buildProjectGroups } from "@t3tools/client-runtime/state/project-grouping";
33
import {
44
createContext,
5+
useCallback,
56
useContext,
67
useMemo,
78
useRef,
@@ -79,24 +80,26 @@ export function SettingsEnvironmentFilterProvider(props: {
7980
}) {
8081
const [selectedIds, setSelectedIds] = useState<ReadonlySet<EnvironmentId> | null>(null);
8182
const [selectedProjectKey, setSelectedProjectKey] = useState<string | null>(null);
82-
// Written only by selectProject; a closed Settings has no project page left to read it.
8383
const currentProjectKey = useRef<string | null>(null);
8484
if (!props.open && (selectedIds !== null || selectedProjectKey !== null)) {
85+
currentProjectKey.current = null;
8586
setSelectedIds(null);
8687
setSelectedProjectKey(null);
8788
}
89+
const selectProject = useCallback((projectKey: string | null) => {
90+
currentProjectKey.current = projectKey;
91+
setSelectedProjectKey(projectKey);
92+
}, []);
93+
const getSelectedProjectKey = useCallback(() => currentProjectKey.current, []);
8894
const value = useMemo(
8995
() => ({
9096
selectedIds,
9197
selectedProjectKey,
9298
setSelectedIds,
93-
selectProject: (projectKey: string | null) => {
94-
currentProjectKey.current = projectKey;
95-
setSelectedProjectKey(projectKey);
96-
},
97-
getSelectedProjectKey: () => currentProjectKey.current,
99+
selectProject,
100+
getSelectedProjectKey,
98101
}),
99-
[selectedIds, selectedProjectKey],
102+
[selectedIds, selectedProjectKey, selectProject, getSelectedProjectKey],
100103
);
101104
return (
102105
<SettingsEnvironmentSelectionContext value={value}>

‎apps/mobile/src/features/usage/UsageLimitsSection.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { environmentPresentations } from "../../state/presentation";
2727
import { serverEnvironment } from "../../state/server";
2828
import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
2929
import { useAtomCommand } from "../../state/use-atom-command";
30-
import { useNowMinute } from "../../lib/clock";
30+
import { useNowMinuteMs } from "../../lib/clock";
3131
import {
3232
environmentIdsKey,
3333
limitsAutoRefreshSetAtom,
@@ -323,7 +323,7 @@ export function useRefreshLimits(
323323
) {
324324
const registry = useContext(RegistryContext);
325325
const presentations = useAtomValue(environmentPresentations.presentationsAtom);
326-
const now = Date.parse(useNowMinute());
326+
const now = useNowMinuteMs();
327327
const [refreshing, setRefreshing] = useState(false);
328328
const refreshingRef = useRef(false);
329329
const selected = [...presentations].filter(

‎apps/mobile/src/lib/clock.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { minuteTickToEpochMs } from "./clock";
4+
5+
describe("minuteTickToEpochMs", () => {
6+
it("reads the minute tick as UTC regardless of the device timezone", () => {
7+
expect(minuteTickToEpochMs("2026-10-26T02:14")).toBe(Date.UTC(2026, 9, 26, 2, 14));
8+
});
9+
});

‎apps/mobile/src/lib/clock.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ export function useDeadlinePassed(deadlineMs: number | null): boolean {
77
return useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
88
}
99

10-
/** ISO timestamp truncated to the minute, advancing each wall-clock minute. */
11-
export function useNowMinute(): string {
12-
return useSyncExternalStore(minuteTick.subscribe, minuteTick.getSnapshot, minuteTick.getSnapshot);
10+
/**
11+
* `minuteTick`'s UTC `YYYY-MM-DDTHH:mm` as epoch ms. The snapshot carries no
12+
* offset, and `Date.parse` reads offset-less date-times as local time.
13+
*/
14+
export function minuteTickToEpochMs(minute: string): number {
15+
return Date.parse(`${minute}:00Z`);
16+
}
17+
18+
/** Epoch ms floored to the minute, advancing each wall-clock minute. */
19+
export function useNowMinuteMs(): number {
20+
return minuteTickToEpochMs(
21+
useSyncExternalStore(minuteTick.subscribe, minuteTick.getSnapshot, minuteTick.getSnapshot),
22+
);
1323
}

0 commit comments

Comments
 (0)