Skip to content

Commit 378e8ef

Browse files
playcationsclaude
andcommitted
fix(mobile): a covered thread does not count down to read after a resume
A screen over the thread, such as the browser or device preview, kept the post-resume wait running, so a run that finished during the resume could still be marked read unseen. The wait now runs only while the thread is on top, and restarts when it is uncovered. A synced visit is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 75ebe83 commit 378e8ef

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { parseThreadKey, threadKey } from "@t3tools/client-runtime/state/entitie
1515
import { createThreadVisitTracker } from "@t3tools/client-runtime/state/thread-visits";
1616
import { useChildThreadInputs, useThreadReportedModelSelection } from "../../state/entities";
1717
import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard";
18-
import { useFocusEffect, useNavigation } from "@react-navigation/native";
18+
import { useFocusEffect, useIsFocused, useNavigation } from "@react-navigation/native";
1919
import {
2020
effectiveSnoozed,
2121
threadWokeAt,
@@ -1222,11 +1222,13 @@ function ThreadDetailBody(
12221222
// Showing the thread counts as viewing it; after a resume or sync, only once it settles.
12231223
const shellSynchronizing =
12241224
useEnvironmentShellReadiness(props.environmentId).status === "synchronizing";
1225+
const covered = !useIsFocused();
12251226
const viewedThread = useSettledThreadView(
12261227
showContent && !props.draftMode
12271228
? { environmentId: props.environmentId, threadId: props.selectedThread.id }
12281229
: null,
12291230
shellSynchronizing,
1231+
covered,
12301232
);
12311233
useAtomMount(threadVisitTracker(viewedThread));
12321234
const layoutVariant = props.layoutVariant ?? "compact";

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { nextThreadViewState, type ThreadViewState } from "./threadViewInterrupt
99
const THREAD_READ_DWELL_MS = 2_000;
1010

1111
/**
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.
12+
* The thread while it counts as viewed. A synced open counts at once; after a resume or sync a
13+
* finished run may land unseen, so the view counts only once the thread has shown, synced, in the
14+
* foreground and uncovered, for a moment. Backing out sooner keeps it unread.
1615
*/
1716
export function useSettledThreadView(
1817
ref: ScopedThreadRef | null,
1918
synchronizing: boolean,
19+
covered: boolean,
2020
): ScopedThreadRef | null {
2121
const active = useAppActive();
2222
const key = ref === null ? null : scopedThreadKey(ref.environmentId, ref.threadId);
@@ -25,16 +25,20 @@ export function useSettledThreadView(
2525
const next = nextThreadViewState(opened, key, visible);
2626
if (next !== opened) setOpened(next);
2727
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]);
28+
// A fresh view each time the thread shows again, so the wait restarts even if no timer ran
29+
// while the app was in the background or the thread was covered.
30+
const view = useMemo(
31+
() => (visible && !covered && key !== null ? { key } : null),
32+
[visible, covered, key],
33+
);
3134
const [settled, setSettled] = useState<{ readonly key: string } | null>(null);
3235
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- times how long a resumed or synced thread has shown in the foreground
3336
useEffect(() => {
3437
if (view === null || !interrupted) return;
3538
const timer = setTimeout(() => setSettled(view), THREAD_READ_DWELL_MS);
3639
return () => clearTimeout(timer);
3740
}, [view, interrupted]);
38-
if (view === null) return null;
39-
return !interrupted || settled === view ? ref : null;
41+
if (!visible) return null;
42+
if (!interrupted) return ref;
43+
return view !== null && settled === view ? ref : null;
4044
}

0 commit comments

Comments
 (0)