Skip to content

Commit 77f6e97

Browse files
refactor(mobile): subagent timers share the visible second clock
The agents sheet and the subagent group card each ran their own 1s interval for elapsed labels. The sheet's kept ticking while its route was hidden or the app was backgrounded; the group card rebuilt focus and app state gating by hand. File chip sharing mirrored the connection URL into a ref through a layout effect. Both elapsed labels now read useVisibleSecondClock, the shared second tick that pauses while unfocused or backgrounded. useFileChipShare returns a plain handler that closes over the current connection URL. Effects: ThreadAgentsSheet 1 -> 0, thread-subagent-group 2 -> 0, useFileChipShare 2 -> 1 (the unmount abort stays: component-lifetime cleanup). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4cb29a5 commit 77f6e97

3 files changed

Lines changed: 40 additions & 67 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { StackActions, useNavigation, type StaticScreenProps } from "@react-navi
1111
import * as DateTime from "effect/DateTime";
1212
import { Atom } from "effect/reactivity";
1313
import * as Haptics from "expo-haptics";
14-
import { useEffect, useState } from "react";
1514
import { Platform, Pressable, ScrollView, View } from "react-native";
1615
import { ScreenStack } from "react-native-screens";
1716
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -22,6 +21,7 @@ import { environmentThreadDetails } from "../../state/threads";
2221
import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
2322
import { NativeHeader, NativeScreen } from "../../native/ThemedScreenStack";
2423
import { SubagentRow } from "./SubagentRow";
24+
import { useVisibleSecondClock } from "./use-visible-second-clock";
2525
import { ScreenContent } from "../../components/ui/ScreenContent";
2626
import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
2727

@@ -195,13 +195,8 @@ function useSubagentElapsed(
195195
subagent: Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">,
196196
tickSeconds: boolean,
197197
): string | null {
198-
const [nowMs, setNowMs] = useState(() => Date.now());
199198
const running = isOrchestrationV2WorkActive(subagent.status);
200-
useEffect(() => {
201-
if (!tickSeconds || !running) return;
202-
const intervalId = setInterval(() => setNowMs(Date.now()), 1_000);
203-
return () => clearInterval(intervalId);
204-
}, [running, tickSeconds]);
199+
const nowMs = useVisibleSecondClock(tickSeconds && running);
205200
const elapsedMs = deriveSubagentElapsedMs(
206201
{
207202
status: subagent.status,

‎apps/mobile/src/features/threads/thread-subagent-group.tsx‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { StackActions, useIsFocused, useNavigation } from "@react-navigation/native";
2+
import { StackActions, useNavigation } from "@react-navigation/native";
33
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
44
import { summarizeSubagentStatuses } from "@t3tools/client-runtime/state/subagent-display";
55
import { isActiveSubagentStatus } from "@t3tools/client-runtime/state/subagentRuntime";
@@ -8,8 +8,7 @@ import type {
88
OrchestrationV2Subagent,
99
OrchestrationV2TurnItem,
1010
} from "@t3tools/contracts";
11-
import { useEffect, useState } from "react";
12-
import { AppState, Pressable, View, type ColorValue } from "react-native";
11+
import { Pressable, View, type ColorValue } from "react-native";
1312

1413
import { AppText as Text } from "../../components/ui/AppText";
1514
import { SymbolView } from "../../components/ui/AppSymbol";
@@ -20,6 +19,7 @@ import { serverEnvironment } from "../../state/server";
2019
import { environmentThreadDetails } from "../../state/threads";
2120
import { subagentCardElapsed } from "./subagent-card-presentation";
2221
import { SubagentRow } from "./SubagentRow";
22+
import { useVisibleSecondClock } from "./use-visible-second-clock";
2323
import { WorkLogBlock } from "./work-log-layout";
2424

2525
import { useOpenThreadInspector } from "./thread-inspector-navigation";
@@ -28,21 +28,8 @@ type SubagentItem = Extract<OrchestrationV2TurnItem, { type: "subagent" }>;
2828
type AgentTiming = Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">;
2929

3030
function SubagentElapsed({ agents }: { readonly agents: ReadonlyArray<AgentTiming> }) {
31-
const focused = useIsFocused();
3231
const live = agents.some((agent) => isActiveSubagentStatus(agent.status));
33-
const [nowMs, setNowMs] = useState(() => Date.now());
34-
const [appActive, setAppActive] = useState(() => AppState.currentState === "active");
35-
useEffect(() => {
36-
const subscription = AppState.addEventListener("change", (state) =>
37-
setAppActive(state === "active"),
38-
);
39-
return () => subscription.remove();
40-
}, []);
41-
useEffect(() => {
42-
if (!live || !focused || !appActive) return;
43-
const intervalId = setInterval(() => setNowMs(Date.now()), 1_000);
44-
return () => clearInterval(intervalId);
45-
}, [appActive, focused, live]);
32+
const nowMs = useVisibleSecondClock(live);
4633
const elapsed = subagentCardElapsed(agents, nowMs);
4734
return elapsed ? (
4835
<Text className="shrink-0 text-xs tabular-nums text-foreground-muted">{elapsed}</Text>

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

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Dialog from "../../components/ui/Dialog";
22
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
33
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
44
import * as Option from "effect/Option";
5-
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
5+
import { useEffect, useRef } from "react";
66

77
import { downloadAndShareAttachment } from "../../lib/attachmentDownload";
88
import { assetEnvironment } from "../../state/assets";
@@ -21,49 +21,40 @@ export function useFileChipShare(
2121
refresh: true,
2222
reportFailure: false,
2323
});
24-
const connectionRef = useRef(httpBaseUrl);
25-
useLayoutEffect(() => {
26-
connectionRef.current = httpBaseUrl;
27-
}, [httpBaseUrl]);
2824
const requestRef = useRef<AbortController | null>(null);
2925
useEffect(() => () => requestRef.current?.abort(), []);
3026

31-
const share = useCallback(
32-
(target: FileChipTarget) => {
33-
const source = fileChipShareSource(target, threadId);
34-
if (!source || requestRef.current) return;
35-
const request = new AbortController();
36-
requestRef.current = request;
37-
const httpBaseUrl = connectionRef.current;
38-
void (async () => {
39-
if (httpBaseUrl === null) throw new Error("Reconnect to the environment and try again.");
40-
const result = await createUrl({ environmentId, input: { resource: source.resource } });
41-
if (request.signal.aborted) return;
42-
const url =
43-
result._tag === "Success" ? resolveAssetUrl(httpBaseUrl, result.value.relativeUrl) : null;
44-
if (url === null) throw new Error("The file could not be loaded. Reconnect and try again.");
45-
await downloadAndShareAttachment({
46-
url,
47-
attachment: source,
48-
signal: request.signal,
49-
sourceIdentifier,
50-
});
51-
})()
52-
.catch((error: unknown) => {
53-
if (!request.signal.aborted) {
54-
Dialog.alert(
55-
"Could not share file",
56-
error instanceof Error ? error.message : "Try again.",
57-
);
58-
}
59-
})
60-
.finally(() => {
61-
if (requestRef.current === request) {
62-
requestRef.current = null;
63-
}
64-
});
65-
},
66-
[createUrl, environmentId, sourceIdentifier, threadId],
67-
);
68-
return share;
27+
return (target: FileChipTarget) => {
28+
const source = fileChipShareSource(target, threadId);
29+
if (!source || requestRef.current) return;
30+
const request = new AbortController();
31+
requestRef.current = request;
32+
void (async () => {
33+
if (httpBaseUrl === null) throw new Error("Reconnect to the environment and try again.");
34+
const result = await createUrl({ environmentId, input: { resource: source.resource } });
35+
if (request.signal.aborted) return;
36+
const url =
37+
result._tag === "Success" ? resolveAssetUrl(httpBaseUrl, result.value.relativeUrl) : null;
38+
if (url === null) throw new Error("The file could not be loaded. Reconnect and try again.");
39+
await downloadAndShareAttachment({
40+
url,
41+
attachment: source,
42+
signal: request.signal,
43+
sourceIdentifier,
44+
});
45+
})()
46+
.catch((error: unknown) => {
47+
if (!request.signal.aborted) {
48+
Dialog.alert(
49+
"Could not share file",
50+
error instanceof Error ? error.message : "Try again.",
51+
);
52+
}
53+
})
54+
.finally(() => {
55+
if (requestRef.current === request) {
56+
requestRef.current = null;
57+
}
58+
});
59+
};
6960
}

0 commit comments

Comments
 (0)