Skip to content

Commit bd0b8cb

Browse files
t3dotggclaude
andcommitted
fix(web): back useNowMinute with one shared module-level timer
Per-instance state and timers meant two consumers could still hold different minute values (remount timing, throttled background timers) despite boundary alignment. A single module-level clock behind useSyncExternalStore gives every consumer the same value by construction; ticks re-read the clock so late timers self-correct, and the timer stops when the last subscriber leaves. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 18b3e5e commit bd0b8cb

1 file changed

Lines changed: 59 additions & 23 deletions

File tree

‎apps/web/src/hooks/useNowMinute.ts‎

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,63 @@
1-
import { useEffect, useState } from "react";
1+
import { useSyncExternalStore } from "react";
22

33
/** Minute-quantized clock ("YYYY-MM-DDTHH:MM") for settled-state resolution.
4-
Ticks are aligned to UTC minute boundaries — not offset from mount time —
5-
so every consumer (sidebar partition, composer banner) crosses each minute
6-
together and effectiveSettled can never disagree between surfaces. */
7-
export function useNowMinute(): string {
8-
const [nowMinute, setNowMinute] = useState(() => new Date().toISOString().slice(0, 16));
9-
useEffect(() => {
10-
let intervalId: number | null = null;
11-
const timeoutId = window.setTimeout(
12-
() => {
13-
setNowMinute(new Date().toISOString().slice(0, 16));
14-
intervalId = window.setInterval(
15-
() => setNowMinute(new Date().toISOString().slice(0, 16)),
16-
60_000,
17-
);
18-
},
19-
60_000 - (Date.now() % 60_000),
20-
);
21-
return () => {
22-
window.clearTimeout(timeoutId);
23-
if (intervalId !== null) window.clearInterval(intervalId);
24-
};
25-
}, []);
4+
One module-level timer feeds every consumer through useSyncExternalStore,
5+
so all surfaces resolving effectiveSettled against it (sidebar partition,
6+
composer banner) share a single value by construction and tick on UTC
7+
minute boundaries together. */
8+
9+
function currentMinute(): string {
10+
return new Date().toISOString().slice(0, 16);
11+
}
12+
13+
let nowMinute = currentMinute();
14+
let timerId: number | null = null;
15+
let timerIsInterval = false;
16+
const listeners = new Set<() => void>();
17+
18+
function tick(): void {
19+
const next = currentMinute();
20+
if (next !== nowMinute) {
21+
nowMinute = next;
22+
for (const listener of listeners) listener();
23+
}
24+
}
25+
26+
function startTimer(): void {
27+
// Align to the next UTC minute boundary, then tick every 60s. Ticks re-read
28+
// the clock, so a throttled or late timer self-corrects when it fires.
29+
timerIsInterval = false;
30+
timerId = window.setTimeout(
31+
() => {
32+
tick();
33+
timerIsInterval = true;
34+
timerId = window.setInterval(tick, 60_000);
35+
},
36+
60_000 - (Date.now() % 60_000),
37+
);
38+
}
39+
40+
function subscribe(listener: () => void): () => void {
41+
if (listeners.size === 0) {
42+
// The stored minute may have gone stale while no one was subscribed.
43+
nowMinute = currentMinute();
44+
startTimer();
45+
}
46+
listeners.add(listener);
47+
return () => {
48+
listeners.delete(listener);
49+
if (listeners.size === 0 && timerId !== null) {
50+
if (timerIsInterval) window.clearInterval(timerId);
51+
else window.clearTimeout(timerId);
52+
timerId = null;
53+
}
54+
};
55+
}
56+
57+
function getSnapshot(): string {
2658
return nowMinute;
2759
}
60+
61+
export function useNowMinute(): string {
62+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
63+
}

0 commit comments

Comments
 (0)