The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
perf(web): sidebar working timers share one clock (#21195)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alyx committed
Oct 18, 2026 at 20:28 UTC
6ac3bc553e9f8a7fcd687c9f69b4c7c94bdb18ff
2 files changed
+57
-9
apps/web/src/components/Sidebar.tsx
+11
-9
@@ -151,6 +151,7 @@ import { useClientSettings } from "../hooks/useSettings";
151
import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
152
import { useLocalStorage } from "../hooks/useLocalStorage";
153
import { useNowMinute } from "../hooks/useNowMinute";
154
+import { useNowSecond } from "../hooks/useNowSecond";
155
import {
156
useEnvironmentIdentities,
157
useConnectedEnvironmentIds,
@@ -848,18 +849,19 @@ function JumpHintBadge(props: { label: string }) {
849
);
850
}
851
851
-// Self-ticking so only this span re-renders each second, not the whole row.
852
function WorkingDuration(props: { startedAt: string | null }) {
853
const startedMs = props.startedAt !== null ? Date.parse(props.startedAt) : Number.NaN;
854
- // Mount time is close enough for the first frame; the interval keeps it current.
855
- const [nowMs, setNowMs] = useState(() => Date.now());
856
- useEffect(() => {
857
- if (Number.isNaN(startedMs)) return;
858
- const id = window.setInterval(() => setNowMs(Date.now()), 1_000);
859
- return () => window.clearInterval(id);
860
- }, [startedMs]);
854
if (Number.isNaN(startedMs)) return null;
862
- return <span className="tabular-nums">{formatWorkingDurationLabel(nowMs - startedMs)}</span>;
855
+ return <WorkingDurationLabel startedMs={startedMs} />;
856
+}
857
+
858
+// Self-ticking so only this span re-renders each second, not the whole row;
859
+// the shared tick lands every working row's update in the same frame.
860
+function WorkingDurationLabel(props: { startedMs: number }) {
861
+ const nowMs = useNowSecond();
862
+ return (
863
+ <span className="tabular-nums">{formatWorkingDurationLabel(nowMs - props.startedMs)}</span>
864
+ );
865
}
866
867
function terminalProcessLabel(count: number): string {
apps/web/src/hooks/useNowSecond.ts
new
+46
@@ -0,0 +1,46 @@
1
+import { useSyncExternalStore } from "react";
2
+
3
+// One shared tick on wall-clock second boundaries: every live duration label
4
+// updates in the same frame instead of each repainting on its own phase.
5
+
6
+function currentSecond(): number {
7
+ return Math.floor(Date.now() / 1000) * 1000;
8
+}
9
+
10
+let nowSecond = currentSecond();
11
+let timerId: number | null = null;
12
+const listeners = new Set<() => void>();
13
+
14
+function scheduleTick(): void {
15
+ timerId = window.setTimeout(
16
+ () => {
17
+ nowSecond = currentSecond();
18
+ for (const listener of listeners) listener();
19
+ scheduleTick();
20
+ },
21
+ 1000 - (Date.now() % 1000),
22
+ );
23
+}
24
+
25
+function subscribe(listener: () => void): () => void {
26
+ if (listeners.size === 0) scheduleTick();
27
+ listeners.add(listener);
28
+ return () => {
29
+ listeners.delete(listener);
30
+ if (listeners.size === 0 && timerId !== null) {
31
+ window.clearTimeout(timerId);
32
+ timerId = null;
33
+ }
34
+ };
35
+}
36
+
37
+function getSnapshot(): number {
38
+ // While the timer runs the cached value is returned untouched, as
39
+ // useSyncExternalStore requires between change notifications.
40
+ if (timerId === null) nowSecond = currentSecond();
41
+ return nowSecond;
42
+}
43
+
44
+export function useNowSecond(): number {
45
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
46
+}