@devries-cameron20 / t3code-1 / commits / 2171c50726
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): relative "checked" labels re-render only when their text changes (#20819)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 17, 2026 at 02:00 UTC 2171c50726d82bab6c12f1398fa5b46b82668f45
6 files changed +112 -47
apps/web/src/components/settings/DiagnosticsSettings.tsx
+4 -4
@@ -23,7 +23,8 @@ import * as Option from "effect/Option";
23 import { cn } from "../../lib/utils";
24 import { ensureLocalApi } from "../../localApi";
25 import { useOpenInPreferredEditor } from "../../editorPreferences";
26 -import { formatRelativeTimeLabel, getRelativeTimeState } from "../../timestampFormat";
26 +import { formatRelativeTimeLabel } from "../../timestampFormat";
27 +import { useRelativeTimeState } from "../../hooks/useRelativeTimeState";
28 import { useEnvironmentQuery } from "../../state/query";
29 import { serverEnvironment } from "../../state/server";
30 import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
@@ -35,7 +36,7 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
36 import { toastManager } from "../ui/toast";
37 import { ExpandableText } from "./ExpandableText";
38 import { ResourceTelemetryDiagnostics } from "./ResourceTelemetryDiagnostics";
38 -import { SettingsPageContainer, SettingsSection, useRelativeTimeTick } from "./settingsLayout";
39 +import { SettingsPageContainer, SettingsSection } from "./settingsLayout";
40 import { useAtomCommand } from "../../state/use-atom-command";
41 import { useSettingsScope } from "./SettingsScopeContext";
42 import { TelemetryExportSettings } from "./TelemetryExportSettings";
@@ -664,8 +665,7 @@ function ProcessResourceHistoryTable({
665 }
666
667 function DiagnosticsLastChecked({ checkedAt }: { checkedAt: DateTime.Utc | null }) {
667 - useRelativeTimeTick();
668 - const relative = getRelativeTimeState(checkedAt ? DateTime.formatIso(checkedAt) : null);
668 + const relative = useRelativeTimeState(checkedAt ? DateTime.formatIso(checkedAt) : null);
669
670 if (relative.status === "missing") {
671 return <span className="text-2xs text-muted-foreground/50">Checking</span>;
apps/web/src/components/settings/ProviderSettingsPanel.tsx
+2 -4
@@ -57,7 +57,7 @@ import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
57 import { useEnvironmentSessionState } from "../../state/session";
58 import { useProjects, useServerConfigs } from "../../state/entities";
59 import { useAtomCommand } from "../../state/use-atom-command";
60 -import { getRelativeTimeState } from "../../timestampFormat";
60 +import { useRelativeTimeState } from "../../hooks/useRelativeTimeState";
61 import {
62 ConnectionStatusDot,
63 connectionPhaseDotClassName,
@@ -112,7 +112,6 @@ import {
112 SettingsPageContainer,
113 SettingsRow,
114 SettingsSection,
115 - useRelativeTimeTick,
115 useSettingsSearchTargetId,
116 } from "./settingsLayout";
117 import {
@@ -151,8 +150,7 @@ function configuredBinaryPath(config: unknown): string {
150 }
151
152 function ProviderLastChecked({ lastCheckedAt }: { lastCheckedAt: string | null }) {
154 - useRelativeTimeTick();
155 - const lastCheckedRelative = getRelativeTimeState(lastCheckedAt);
153 + const lastCheckedRelative = useRelativeTimeState(lastCheckedAt);
154
155 if (lastCheckedRelative.status === "missing") {
156 return null;
apps/web/src/components/settings/ResourceTelemetryDiagnostics.tsx
+4 -8
@@ -44,7 +44,7 @@ import { ensureLocalApi } from "../../localApi";
44 import { serverEnvironment } from "../../state/server";
45 import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
46 import { useAtomCommand } from "../../state/use-atom-command";
47 -import { formatRelativeTime } from "../../timestampFormat";
47 +import { useRelativeTimeState } from "../../hooks/useRelativeTimeState";
48 import { Button } from "../ui/button";
49 import { MorphIcon } from "~/components/MorphIcon";
50 import { ScrollArea } from "../ui/scroll-area";
@@ -57,7 +57,7 @@ import {
57 shouldShowResourceMonitorRetry,
58 visibleResourceTelemetryProcesses,
59 } from "./ResourceTelemetryDiagnostics.logic";
60 -import { SettingsSection, useRelativeTimeTick } from "./settingsLayout";
60 +import { SettingsSection } from "./settingsLayout";
61
62 const HISTORY_WINDOWS = [
63 { label: "5m", windowMs: 5 * 60_000, bucketMs: 15_000 },
@@ -207,12 +207,8 @@ function SourceStatusBadge({
207 }
208
209 function LastSampleLabel({ sampledAt }: { sampledAt: DateTime.Utc | null }) {
210 - useRelativeTimeTick();
211 - if (!sampledAt) {
212 - return <span className="text-2xs text-muted-foreground/55">Waiting for sample</span>;
213 - }
214 - const relative = formatRelativeTime(DateTime.formatIso(sampledAt));
215 - if (!relative) {
210 + const relative = useRelativeTimeState(sampledAt ? DateTime.formatIso(sampledAt) : null);
211 + if (relative.status !== "relative") {
212 return <span className="text-2xs text-muted-foreground/55">Waiting for sample</span>;
213 }
214 return (
apps/web/src/hooks/useRelativeTimeState.ts new
+36
@@ -0,0 +1,36 @@
1 +import { useSyncExternalStore } from "react";
2 +
3 +import {
4 + formatRelativeElapsed,
5 + msUntilRelativeTimeChange,
6 + relativeTimeElapsedMs,
7 + type RelativeTimeState,
8 +} from "../timestampFormat";
9 +
10 +/**
11 + * A "5m ago" label that re-renders only when its text changes: the snapshot is
12 + * the elapsed time floored to the label's precision, and one timer fires at the
13 + * next boundary instead of polling every second.
14 + */
15 +export function useRelativeTimeState(isoDate: string | null): RelativeTimeState {
16 + const subscribe = (onChange: () => void) => {
17 + if (isoDate === null) return () => {};
18 + let timer: ReturnType<typeof setTimeout> | undefined;
19 + const schedule = () => {
20 + const delayMs = msUntilRelativeTimeChange(isoDate);
21 + if (delayMs === null) return;
22 + timer = setTimeout(() => {
23 + onChange();
24 + schedule();
25 + }, delayMs);
26 + };
27 + schedule();
28 + return () => clearTimeout(timer);
29 + };
30 + const getSnapshot = () => (isoDate === null ? null : relativeTimeElapsedMs(isoDate));
31 + const elapsedMs = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
32 +
33 + if (isoDate === null) return { status: "missing" };
34 + if (elapsedMs === null) return { status: "invalid" };
35 + return { status: "relative", ...formatRelativeElapsed(elapsedMs) };
36 +}
apps/web/src/timestampFormat.test.ts
+26 -20
@@ -11,7 +11,8 @@ import {
11 formatShortTimestamp,
12 formatTimestamp,
13 formatUpcomingTimestamp,
14 - getRelativeTimeState,
14 + msUntilRelativeTimeChange,
15 + relativeTimeElapsedMs,
16 resolveTimestampLocale,
17 } from "./timestampFormat";
18
@@ -253,9 +254,9 @@ describe("invalid timestamp inputs", () => {
254 expect(formatRelativeTimeLabel("not-a-date")).toBe("");
255 });
256
256 - it("distinguishes missing and invalid relative time state", () => {
257 - expect(getRelativeTimeState(null)).toEqual({ status: "missing" });
258 - expect(getRelativeTimeState("not-a-date")).toEqual({ status: "invalid" });
257 + it("has no relative time boundary for an invalid timestamp", () => {
258 + expect(relativeTimeElapsedMs("not-a-date")).toBeNull();
259 + expect(msUntilRelativeTimeChange("not-a-date")).toBeNull();
260 });
261
262 it("returns an empty elapsed duration instead of a NaN label", () => {
@@ -272,22 +273,27 @@ describe("invalid timestamp inputs", () => {
273 });
274 });
275
275 -describe("getRelativeTimeState", () => {
276 - beforeEach(() => {
277 - vi.useFakeTimers();
278 - vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
279 - });
280 -
281 - afterEach(() => {
282 - vi.useRealTimers();
283 - });
284 -
285 - it("returns relative parts for valid timestamps", () => {
286 - expect(getRelativeTimeState("2026-04-07T11:45:00.000Z")).toEqual({
287 - status: "relative",
288 - value: "15m",
289 - suffix: "ago",
290 - });
276 +describe("relative time boundaries", () => {
277 + const nowMs = Date.parse("2026-04-07T12:00:00.000Z");
278 +
279 + it("waits for the label to change instead of the next second", () => {
280 + // "just now" turns into "1m ago" at the one-minute mark.
281 + expect(msUntilRelativeTimeChange("2026-04-07T11:59:40.000Z", nowMs)).toBe(40_000);
282 + // "2m ago" turns into "3m ago" a minute after the 2m mark.
283 + expect(msUntilRelativeTimeChange("2026-04-07T11:57:30.000Z", nowMs)).toBe(30_000);
284 + // "3h ago" holds until the 4h mark.
285 + expect(msUntilRelativeTimeChange("2026-04-07T08:30:00.000Z", nowMs)).toBe(30 * 60_000);
286 + // "2d ago" holds until the 3d mark.
287 + expect(msUntilRelativeTimeChange("2026-04-05T00:00:00.000Z", nowMs)).toBe(12 * 3_600_000);
288 + // A future timestamp reads "just now" until a minute after it.
289 + expect(msUntilRelativeTimeChange("2026-04-07T12:00:10.000Z", nowMs)).toBe(70_000);
290 + });
291 +
292 + it("keeps the elapsed snapshot stable while the label is unchanged", () => {
293 + const at = "2026-04-07T11:57:30.000Z";
294 + expect(relativeTimeElapsedMs(at, nowMs)).toBe(2 * 60_000);
295 + expect(relativeTimeElapsedMs(at, nowMs + 29_999)).toBe(2 * 60_000);
296 + expect(relativeTimeElapsedMs(at, nowMs + 30_000)).toBe(3 * 60_000);
297 });
298 });
299
apps/web/src/timestampFormat.ts
+40 -11
@@ -203,12 +203,43 @@ export type RelativeTimeState =
203 | { status: "invalid" }
204 | { status: "relative"; value: string; suffix: string | null };
205
206 -export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
206 +const MINUTE_MS = 60_000;
207 +const HOUR_MS = 3_600_000;
208 +const DAY_MS = 86_400_000;
209 +
210 +function relativeTimeUnitMs(elapsedMs: number): number {
211 + if (elapsedMs < HOUR_MS) return MINUTE_MS;
212 + if (elapsedMs < DAY_MS) return HOUR_MS;
213 + return DAY_MS;
214 +}
215 +
216 +/**
217 + * Elapsed time floored to the precision `formatRelativeTime` shows, so two
218 + * instants with the same label return the same number.
219 + */
220 +export function relativeTimeElapsedMs(isoDate: string, nowMs: number = Date.now()): number | null {
221 const date = parseTimestampDate(isoDate);
222 if (!date) return null;
209 - const diffMs = Date.now() - date.getTime();
210 - if (diffMs < 0) return { value: "just now", suffix: null };
211 - const seconds = Math.floor(diffMs / 1000);
223 + const elapsedMs = Math.max(0, nowMs - date.getTime());
224 + const unitMs = relativeTimeUnitMs(elapsedMs);
225 + return Math.floor(elapsedMs / unitMs) * unitMs;
226 +}
227 +
228 +/** Milliseconds until the `formatRelativeTime` label for `isoDate` next changes. */
229 +export function msUntilRelativeTimeChange(
230 + isoDate: string,
231 + nowMs: number = Date.now(),
232 +): number | null {
233 + const date = parseTimestampDate(isoDate);
234 + if (!date) return null;
235 + const elapsedMs = Math.max(0, nowMs - date.getTime());
236 + const unitMs = relativeTimeUnitMs(elapsedMs);
237 + const nextChangeMs = date.getTime() + (Math.floor(elapsedMs / unitMs) + 1) * unitMs;
238 + return nextChangeMs - nowMs;
239 +}
240 +
241 +export function formatRelativeElapsed(elapsedMs: number): RelativeTimeParts {
242 + const seconds = Math.floor(elapsedMs / 1000);
243 if (seconds < 60) return { value: "just now", suffix: null };
244 const minutes = Math.floor(seconds / 60);
245 if (minutes < 60) return { value: `${minutes}m`, suffix: "ago" };
@@ -218,19 +249,17 @@ export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
249 return { value: `${days}d`, suffix: "ago" };
250 }
251
252 +export function formatRelativeTime(isoDate: string): RelativeTimeParts | null {
253 + const elapsedMs = relativeTimeElapsedMs(isoDate);
254 + return elapsedMs === null ? null : formatRelativeElapsed(elapsedMs);
255 +}
256 +
257 export function formatRelativeTimeLabel(isoDate: string) {
258 const relative = formatRelativeTime(isoDate);
259 if (!relative) return "";
260 return relative.suffix ? `${relative.value} ${relative.suffix}` : relative.value;
261 }
262
227 -export function getRelativeTimeState(isoDate: string | null): RelativeTimeState {
228 - if (!isoDate) return { status: "missing" };
229 - const relative = formatRelativeTime(isoDate);
230 - if (!relative) return { status: "invalid" };
231 - return { status: "relative", ...relative };
232 -}
233 -
263 export function formatElapsedDurationLabel(isoDate: string, nowMs: number = Date.now()): string {
264 const date = parseTimestampDate(isoDate);
265 if (!date) return "";