Skip to content

Commit c55dd68

Browse files
committed
fix(mobile): preserve remote unread state during retries
1 parent 8a516e5 commit c55dd68

3 files changed

Lines changed: 108 additions & 19 deletions

File tree

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import {
7575
ThreadInspectorContentStack,
7676
type ThreadInspectorMode,
7777
} from "./thread-inspector-content-stack";
78-
import { resolveAppliedThreadViewBoundary, resolveThreadViewCommand } from "./threadViewState";
78+
import { resolveThreadViewCommand, resolveThreadViewTrackingState } from "./threadViewState";
7979

8080
interface ThreadInspectorSelection {
8181
readonly routeThreadIdentity: string | null;
@@ -292,11 +292,25 @@ function ThreadRouteContent(
292292
const selectedThreadViewedAtRef = useRef(selectedThread?.viewedAt);
293293
selectedThreadViewedAtRef.current = selectedThread?.viewedAt;
294294
const requestedThreadViewBoundariesRef = useRef(new Set<string>());
295-
const appliedThreadViewBoundary = resolveAppliedThreadViewBoundary({
296-
completedAt: selectedThreadCompletedAt,
297-
viewedAt: selectedThread?.viewedAt,
298-
requestedBoundaries: requestedThreadViewBoundariesRef.current,
299-
});
295+
const [threadViewRetryBoundary, setThreadViewRetryBoundary] = useState<string>();
296+
useEffect(() => {
297+
requestedThreadViewBoundariesRef.current.clear();
298+
setThreadViewRetryBoundary(undefined);
299+
}, [selectedThreadEnvironmentId, selectedThreadId]);
300+
useEffect(() => {
301+
const trackingState = resolveThreadViewTrackingState({
302+
completedAt: selectedThreadCompletedAt,
303+
viewedAt: selectedThread?.viewedAt,
304+
requestedBoundaries: requestedThreadViewBoundariesRef.current,
305+
retryBoundary: threadViewRetryBoundary,
306+
});
307+
if (trackingState.shouldClearRequestedBoundaries) {
308+
requestedThreadViewBoundariesRef.current.clear();
309+
}
310+
if (trackingState.retryBoundary !== threadViewRetryBoundary) {
311+
setThreadViewRetryBoundary(trackingState.retryBoundary);
312+
}
313+
}, [selectedThread?.viewedAt, selectedThreadCompletedAt, threadViewRetryBoundary]);
300314
const supportsThreadViewState = serverConfig?.environment.capabilities.threadViewState === true;
301315
const [appState, setAppState] = useState(() => AppState.currentState);
302316
useEffect(() => {
@@ -315,19 +329,10 @@ function ThreadRouteContent(
315329
appState,
316330
connectionState: routeConnectionState,
317331
completedAt: selectedThreadCompletedAt,
318-
viewedAt: appliedThreadViewBoundary ?? selectedThreadViewedAtRef.current,
332+
viewedAt: selectedThreadViewedAtRef.current,
319333
supported: supportsThreadViewState,
320334
});
321-
if (command === undefined) {
322-
if (
323-
selectedThreadCompletedAt !== null &&
324-
selectedThreadViewedAtRef.current !== undefined &&
325-
Date.parse(selectedThreadViewedAtRef.current) >= Date.parse(selectedThreadCompletedAt)
326-
) {
327-
requestedThreadViewBoundariesRef.current.clear();
328-
}
329-
return;
330-
}
335+
if (command === undefined) return;
331336

332337
requestedThreadViewBoundariesRef.current.add(command.viewedThrough);
333338
void viewThread({
@@ -339,12 +344,12 @@ function ThreadRouteContent(
339344
});
340345
}, [
341346
appState,
342-
appliedThreadViewBoundary,
343347
routeConnectionState,
344348
selectedThreadCompletedAt,
345349
selectedThreadEnvironmentId,
346350
selectedThreadId,
347351
supportsThreadViewState,
352+
threadViewRetryBoundary,
348353
viewThread,
349354
]),
350355
);

‎apps/mobile/src/features/threads/threadViewState.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { resolveAppliedThreadViewBoundary, resolveThreadViewCommand } from "./threadViewState";
3+
import {
4+
resolveAppliedThreadViewBoundary,
5+
resolveThreadViewCommand,
6+
resolveThreadViewTrackingState,
7+
} from "./threadViewState";
48

59
const input = {
610
appState: "active" as const,
@@ -103,3 +107,45 @@ describe("resolveAppliedThreadViewBoundary", () => {
103107
).toBeUndefined();
104108
});
105109
});
110+
111+
describe("resolveThreadViewTrackingState", () => {
112+
const earlierCompletion = "2026-01-01T00:01:00.000Z";
113+
const laterCompletion = "2026-01-01T00:02:00.000Z";
114+
const requestedBoundaries = new Set([earlierCompletion, laterCompletion]);
115+
116+
it("keeps its retry signal when a remote unread action replaces an applied view", () => {
117+
const appliedEarlierView = resolveThreadViewTrackingState({
118+
completedAt: laterCompletion,
119+
viewedAt: earlierCompletion,
120+
requestedBoundaries,
121+
retryBoundary: undefined,
122+
});
123+
expect(appliedEarlierView).toEqual({
124+
retryBoundary: earlierCompletion,
125+
shouldClearRequestedBoundaries: false,
126+
});
127+
128+
expect(
129+
resolveThreadViewTrackingState({
130+
completedAt: laterCompletion,
131+
viewedAt: "2026-01-01T00:01:59.999Z",
132+
requestedBoundaries,
133+
retryBoundary: appliedEarlierView.retryBoundary,
134+
}),
135+
).toEqual(appliedEarlierView);
136+
});
137+
138+
it("clears requested views without changing the retry signal", () => {
139+
expect(
140+
resolveThreadViewTrackingState({
141+
completedAt: laterCompletion,
142+
viewedAt: laterCompletion,
143+
requestedBoundaries,
144+
retryBoundary: earlierCompletion,
145+
}),
146+
).toEqual({
147+
retryBoundary: earlierCompletion,
148+
shouldClearRequestedBoundaries: true,
149+
});
150+
});
151+
});

‎apps/mobile/src/features/threads/threadViewState.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,41 @@ export function resolveAppliedThreadViewBoundary(input: {
5050

5151
return undefined;
5252
}
53+
54+
export function resolveThreadViewTrackingState(input: {
55+
readonly completedAt: string | null;
56+
readonly viewedAt: string | undefined;
57+
readonly requestedBoundaries: ReadonlySet<string>;
58+
readonly retryBoundary: string | undefined;
59+
}): {
60+
readonly retryBoundary: string | undefined;
61+
readonly shouldClearRequestedBoundaries: boolean;
62+
} {
63+
if (input.completedAt === null || input.viewedAt === undefined) {
64+
return {
65+
retryBoundary: input.retryBoundary,
66+
shouldClearRequestedBoundaries: false,
67+
};
68+
}
69+
70+
const completedAtMs = Date.parse(input.completedAt);
71+
const viewedAtMs = Date.parse(input.viewedAt);
72+
if (!Number.isFinite(completedAtMs) || !Number.isFinite(viewedAtMs)) {
73+
return {
74+
retryBoundary: input.retryBoundary,
75+
shouldClearRequestedBoundaries: false,
76+
};
77+
}
78+
79+
if (viewedAtMs >= completedAtMs) {
80+
return {
81+
retryBoundary: input.retryBoundary,
82+
shouldClearRequestedBoundaries: true,
83+
};
84+
}
85+
86+
return {
87+
retryBoundary: resolveAppliedThreadViewBoundary(input) ?? input.retryBoundary,
88+
shouldClearRequestedBoundaries: false,
89+
};
90+
}

0 commit comments

Comments
 (0)