Skip to content

Commit ee2800e

Browse files
only21milcodex
andauthored
fix(mobile): relative-time labels keep counting on archive and pull request screens (#22151)
Co-authored-by: GPT-6.1 Sol <[email protected]>
1 parent 0d51b14 commit ee2800e

7 files changed

Lines changed: 86 additions & 11 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useSyncExternalStore } from "react";
2+
import { msUntilRelativeTimeChange, relativeTime } from "../lib/time";
3+
4+
// The React Compiler caches inline relativeTime calls on the timestamp alone.
5+
export function RelativeTime(props: { timestamp: string }) {
6+
const subscribe = (onChange: () => void) => {
7+
let timer: ReturnType<typeof setTimeout> | undefined;
8+
const schedule = () => {
9+
const delayMs = msUntilRelativeTimeChange(props.timestamp);
10+
if (delayMs === null) return;
11+
timer = setTimeout(() => {
12+
onChange();
13+
schedule();
14+
}, delayMs);
15+
};
16+
schedule();
17+
return () => clearTimeout(timer);
18+
};
19+
const getSnapshot = () => relativeTime(props.timestamp);
20+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
21+
}

‎apps/mobile/src/features/archive/ArchivedThreadsScreen.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { AppText as Text } from "../../components/ui/AppText";
2424
import { EmptyState } from "../../components/ui/EmptyState";
2525
import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
2626
import { ProjectFavicon } from "../../components/ProjectFavicon";
27-
import { relativeTime } from "../../lib/time";
27+
import { RelativeTime } from "../../components/RelativeTime";
2828
import { useUniwindTheme } from "../../lib/useUniwindTheme";
2929
import { useServerConfigs } from "../../state/entities";
3030
import { useEnvironmentScope } from "../../state/session";
@@ -193,7 +193,6 @@ function ArchivedThreadRow(props: {
193193
props.thread.environmentId,
194194
AuthOrchestrationOperateScope,
195195
);
196-
const timestamp = relativeTime(props.thread.archivedAt ?? props.thread.updatedAt);
197196
const subtitle = [props.environmentLabel, props.thread.branch].filter((part): part is string =>
198197
Boolean(part),
199198
);
@@ -219,7 +218,7 @@ function ArchivedThreadRow(props: {
219218
{props.thread.title}
220219
</Text>
221220
<Text className="min-w-[30px] text-right text-xs tabular-nums text-foreground-tertiary">
222-
{timestamp}
221+
<RelativeTime timestamp={props.thread.archivedAt ?? props.thread.updatedAt} />
223222
</Text>
224223
</View>
225224
{subtitle.length > 0 ? (

‎apps/mobile/src/features/pull-requests/PullRequestListRow.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SymbolView } from "../../components/ui/AppSymbol";
99
import { Menu } from "../../components/ui/Menu";
1010
import type { MenuProps } from "../../components/ui/Menu.types";
1111
import { cn } from "../../lib/cn";
12-
import { relativeTime } from "../../lib/time";
12+
import { RelativeTime } from "../../components/RelativeTime";
1313
import {
1414
selectedThreadRowColors,
1515
THREAD_LIST_V2_SIDEBAR_ROW_STYLE,
@@ -132,7 +132,7 @@ export const PullRequestListRow = memo(
132132
</View>
133133
) : null}
134134
<Text className={cn("text-2xs tabular-nums", tertiaryForeground)}>
135-
{relativeTime(item.updatedAt)}
135+
<RelativeTime timestamp={item.updatedAt} />
136136
</Text>
137137
</View>
138138
<Text

‎apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import { useAndroidWorkspaceBack } from "../workspace/useAndroidWorkspaceNavigat
7474
import { EmptyState } from "../../components/ui/EmptyState";
7575
import { SegmentedControl } from "../../components/ui/SegmentedControl";
7676
import { SymbolView } from "../../components/ui/AppSymbol";
77-
import { relativeTime } from "../../lib/time";
77+
import { RelativeTime } from "../../components/RelativeTime";
7878
import { NativeStackScreenOptions } from "../../native/StackHeader";
7979
import { usePullRequestConnected } from "./usePullRequestConnected";
8080
import { usePullRequestLiveChecks } from "./usePullRequestLiveChecks";
@@ -213,7 +213,7 @@ function PullRequestIdentity(props: {
213213
{pr.author?.login ?? "Unknown author"}
214214
</Text>
215215
<Text className="shrink text-right text-2xs text-foreground-muted" numberOfLines={1}>
216-
Updated {relativeTime(pr.updatedAt)} ago
216+
Updated <RelativeTime timestamp={pr.updatedAt} /> ago
217217
</Text>
218218
</View>
219219
<View className="flex-row items-start gap-1.5">

‎apps/mobile/src/features/threads/LinkedPullRequestList.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { AppText as Text } from "../../components/ui/AppText";
1717
import * as Dialog from "../../components/ui/Dialog";
1818
import { Menu } from "../../components/ui/Menu";
1919
import { cn } from "../../lib/cn";
20-
import { relativeTime } from "../../lib/time";
20+
import { RelativeTime } from "../../components/RelativeTime";
2121
import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types";
2222
import { useScaledTextRole } from "../settings/appearance/useScaledTextRole";
2323
import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";
@@ -150,7 +150,7 @@ export const LinkedPullRequestRow = memo(function LinkedPullRequestRow(props: {
150150
) : null}
151151
{snapshot?.updatedAt ? (
152152
<Text className="text-2xs tabular-nums text-foreground-tertiary">
153-
{relativeTime(snapshot.updatedAt)}
153+
<RelativeTime timestamp={snapshot.updatedAt} />
154154
</Text>
155155
) : null}
156156
</View>

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
import { msUntilRelativeTimeChange, relativeTime } from "./time";
3+
4+
const at = "2026-10-19T12:00:00.000Z";
5+
const timestamp = Date.parse(at);
6+
7+
describe("relative time", () => {
8+
it.each([
9+
[59_999, "<1m", "1m"],
10+
[119_999, "1m", "2m"],
11+
[3_599_999, "59m", "1h"],
12+
[7_199_999, "1h", "2h"],
13+
[86_399_999, "23h", "1d"],
14+
[172_799_999, "1d", "2d"],
15+
] as const)("updates at the next boundary after %i ms", (elapsed, before, after) => {
16+
const nowMs = timestamp + elapsed;
17+
expect(relativeTime(at, nowMs)).toBe(before);
18+
expect(msUntilRelativeTimeChange(at, nowMs)).toBe(1);
19+
expect(relativeTime(at, nowMs + 1)).toBe(after);
20+
});
21+
22+
it.each([
23+
[0, 60_000],
24+
[60_000, 60_000],
25+
[3_600_000, 3_600_000],
26+
[86_400_000, 86_400_000],
27+
])("waits a full label interval at %i ms", (elapsed, delay) => {
28+
expect(msUntilRelativeTimeChange(at, timestamp + elapsed)).toBe(delay);
29+
});
30+
31+
it("keeps invalid timestamps static", () => {
32+
expect(relativeTime("invalid", timestamp)).toBe("<1m");
33+
expect(msUntilRelativeTimeChange("invalid", timestamp)).toBeNull();
34+
});
35+
36+
it("waits until a future timestamp is a minute old, with bounded timers", () => {
37+
expect(relativeTime(at, timestamp - 30_000)).toBe("<1m");
38+
expect(msUntilRelativeTimeChange(at, timestamp - 30_000)).toBe(90_000);
39+
expect(msUntilRelativeTimeChange(at, timestamp - 30 * 86_400_000)).toBe(86_400_000);
40+
});
41+
});

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export function relativeTime(input: string): string {
1+
export function relativeTime(input: string, nowMs: number = Date.now()): string {
22
const timestamp = Date.parse(input);
33
if (Number.isNaN(timestamp)) {
44
return "<1m";
55
}
66

7-
const deltaSeconds = Math.max(0, Math.floor((Date.now() - timestamp) / 1000));
7+
const deltaSeconds = Math.max(0, Math.floor((nowMs - timestamp) / 1000));
88
if (deltaSeconds < 60) return "<1m";
99

1010
const deltaMinutes = Math.floor(deltaSeconds / 60);
@@ -16,3 +16,17 @@ export function relativeTime(input: string): string {
1616
const deltaDays = Math.floor(deltaHours / 24);
1717
return `${deltaDays}d`;
1818
}
19+
20+
/** Milliseconds until the relative-time label next changes. */
21+
export function msUntilRelativeTimeChange(
22+
input: string,
23+
nowMs: number = Date.now(),
24+
): number | null {
25+
const timestamp = Date.parse(input);
26+
if (Number.isNaN(timestamp)) return null;
27+
const elapsedMs = Math.max(0, nowMs - timestamp);
28+
const unitMs = elapsedMs < 3_600_000 ? 60_000 : elapsedMs < 86_400_000 ? 3_600_000 : 86_400_000;
29+
const nextChangeMs = timestamp + (Math.floor(elapsedMs / unitMs) + 1) * unitMs;
30+
// Far-future timestamps must not overflow setTimeout's signed 32-bit delay.
31+
return Math.min(nextChangeMs - nowMs, 86_400_000);
32+
}

0 commit comments

Comments
 (0)