Skip to content

Commit 6369972

Browse files
playcationsclaude
andcommitted
fix(mobile): a run that finishes during a resume is not marked read unseen
An open thread marked a finished run read the moment it arrived. Resuming the app on a thread, a completion that landed during the sync was marked read even if the user backed out before seeing it. Opening a synced thread still counts at once; after the app leaves the foreground or the environment syncs, the view counts again only once the thread has shown, synced and in the foreground, for two seconds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 15fe283 commit 6369972

4 files changed

Lines changed: 98 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ import type { ThreadContentPresentation } from "./threadContentPresentation";
200200
import { resolveThreadFeedSubmissionAnchor } from "./thread-feed-live-follow";
201201
import { useOpenLinkedThread } from "./useOpenLinkedThread";
202202
import { useGlobalVoiceInput } from "../voice-input/VoiceInputProvider";
203+
import { useEnvironmentShellReadiness } from "../../state/shell";
204+
import { useSettledThreadView } from "./useSettledThreadView";
203205

204206
export interface ThreadDetailScreenProps {
205207
readonly header?: React.ReactNode;
@@ -1115,14 +1117,16 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
11151117
}
11161118
};
11171119
const showContent = props.showContent ?? true;
1118-
// Showing the thread counts as viewing it, before its live shell arrives too.
1119-
useAtomMount(
1120-
threadVisitTracker(
1121-
showContent && !props.draftMode
1122-
? { environmentId: props.environmentId, threadId: props.selectedThread.id }
1123-
: null,
1124-
),
1120+
// Showing the thread counts as viewing it; after a resume or sync, only once it settles.
1121+
const shellSynchronizing =
1122+
useEnvironmentShellReadiness(props.environmentId).status === "synchronizing";
1123+
const viewedThread = useSettledThreadView(
1124+
showContent && !props.draftMode
1125+
? { environmentId: props.environmentId, threadId: props.selectedThread.id }
1126+
: null,
1127+
shellSynchronizing,
11251128
);
1129+
useAtomMount(threadVisitTracker(viewedThread));
11261130
const layoutVariant = props.layoutVariant ?? "compact";
11271131
const isSplitLayout = layoutVariant === "split";
11281132
const contentMaxWidth =
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { nextThreadViewState } from "./threadViewInterruption";
4+
5+
describe("nextThreadViewState", () => {
6+
it("counts a thread opened while synced and in the foreground at once", () => {
7+
expect(nextThreadViewState(null, "env:a", true)).toEqual({ key: "env:a", interrupted: false });
8+
});
9+
10+
it("waits for a thread opened while syncing or in the background", () => {
11+
expect(nextThreadViewState(null, "env:a", false)).toEqual({ key: "env:a", interrupted: true });
12+
});
13+
14+
it("keeps waiting after a resume or sync interrupts the view, until another thread opens", () => {
15+
const opened = nextThreadViewState(null, "env:a", true);
16+
const hidden = nextThreadViewState(opened, "env:a", false);
17+
expect(hidden).toEqual({ key: "env:a", interrupted: true });
18+
expect(nextThreadViewState(hidden, "env:a", true)).toBe(hidden);
19+
expect(nextThreadViewState(hidden, "env:b", true)).toEqual({
20+
key: "env:b",
21+
interrupted: false,
22+
});
23+
});
24+
25+
it("returns the same state while nothing changes", () => {
26+
const opened = nextThreadViewState(null, "env:a", true);
27+
expect(nextThreadViewState(opened, "env:a", true)).toBe(opened);
28+
expect(nextThreadViewState(opened, null, true)).toBeNull();
29+
});
30+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface ThreadViewState {
2+
readonly key: string;
3+
/** The view began, or has since been, hidden or syncing, so reading waits for it to settle. */
4+
readonly interrupted: boolean;
5+
}
6+
7+
/** The next view state for the shown thread; returns `previous` itself when nothing changed. */
8+
export function nextThreadViewState(
9+
previous: ThreadViewState | null,
10+
key: string | null,
11+
visible: boolean,
12+
): ThreadViewState | null {
13+
if (key === null) return null;
14+
if (previous?.key !== key) return { key, interrupted: !visible };
15+
if (!visible && !previous.interrupted) return { key, interrupted: true };
16+
return previous;
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
import { useEffect, useMemo, useState } from "react";
3+
4+
import { useAppActive } from "../../lib/appState";
5+
import { scopedThreadKey } from "../../lib/scopedEntities";
6+
import { nextThreadViewState, type ThreadViewState } from "./threadViewInterruption";
7+
8+
/** How long a thread shows, synced and in the foreground, after a resume or sync before it counts as read. */
9+
const THREAD_READ_DWELL_MS = 2_000;
10+
11+
/**
12+
* The thread while it counts as viewed, otherwise null. Opening a synced thread counts at once.
13+
* After the app leaves the foreground or the environment syncs, a run that finished meanwhile
14+
* may land before the user has looked, so the view counts again only once the thread has shown,
15+
* synced and in the foreground, for a moment; backing out sooner keeps it unread.
16+
*/
17+
export function useSettledThreadView(
18+
ref: ScopedThreadRef | null,
19+
synchronizing: boolean,
20+
): ScopedThreadRef | null {
21+
const active = useAppActive();
22+
const key = ref === null ? null : scopedThreadKey(ref.environmentId, ref.threadId);
23+
const visible = active && !synchronizing && key !== null;
24+
const [opened, setOpened] = useState<ThreadViewState | null>(null);
25+
const next = nextThreadViewState(opened, key, visible);
26+
if (next !== opened) setOpened(next);
27+
const interrupted = next?.interrupted ?? false;
28+
// A fresh view each time the thread becomes visible again, so the wait restarts even if no
29+
// timer ran while the app was in the background.
30+
const view = useMemo(() => (visible && key !== null ? { key } : null), [visible, key]);
31+
const [settled, setSettled] = useState<{ readonly key: string } | null>(null);
32+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- times how long a resumed or synced thread has shown in the foreground
33+
useEffect(() => {
34+
if (view === null || !interrupted) return;
35+
const timer = setTimeout(() => setSettled(view), THREAD_READ_DWELL_MS);
36+
return () => clearTimeout(timer);
37+
}, [view, interrupted]);
38+
if (view === null) return null;
39+
return !interrupted || settled === view ? ref : null;
40+
}

0 commit comments

Comments
 (0)