GeezerVison is showing github.com — refactor(mobile): subagent timers share the visible second clock (#25… · pingdotgg/t3code@edf5a24
File treeExpand file treeCollapse file tree Open diff view settings apps/mobile/src/features/threads
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/ThreadAgentsSheet.tsx +2-7Lines changed: 2 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,7 +11,6 @@ import { StackActions, useNavigation, type StaticScreenProps } from "@react-navi | 11 | 11 | import * as DateTime from "effect/DateTime"; | 12 | 12 | import { Atom } from "effect/reactivity"; | 13 | 13 | import * as Haptics from "expo-haptics"; | 14 | | -import { useEffect, useState } from "react"; | 15 | 14 | import { Platform, Pressable, ScrollView, View } from "react-native"; | 16 | 15 | import { ScreenStack } from "react-native-screens"; | 17 | 16 | import { useSafeAreaInsets } from "react-native-safe-area-context"; | @@ -22,6 +21,7 @@ import { environmentThreadDetails } from "../../state/threads"; | 22 | 21 | import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader"; | 23 | 22 | import { NativeHeader, NativeScreen } from "../../native/ThemedScreenStack"; | 24 | 23 | import { SubagentRow } from "./SubagentRow"; | | 24 | +import { useVisibleSecondClock } from "./use-visible-second-clock"; | 25 | 25 | import { ScreenContent } from "../../components/ui/ScreenContent"; | 26 | 26 | import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout"; | 27 | 27 |
| @@ -195,13 +195,8 @@ function useSubagentElapsed( | 195 | 195 | subagent: Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">, | 196 | 196 | tickSeconds: boolean, | 197 | 197 | ): string | null { | 198 | | - const [nowMs, setNowMs] = useState(() => Date.now()); | 199 | 198 | 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); | 205 | 200 | const elapsedMs = deriveSubagentElapsedMs( | 206 | 201 | { | 207 | 202 | status: subagent.status, | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/thread-subagent-group.tsx +4-17Lines changed: 4 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { useAtomValue } from "@effect/atom-react"; | 2 | | -import { StackActions, useIsFocused, useNavigation } from "@react-navigation/native"; | | 2 | +import { StackActions, useNavigation } from "@react-navigation/native"; | 3 | 3 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; | 4 | 4 | import { summarizeSubagentStatuses } from "@t3tools/client-runtime/state/subagent-display"; | 5 | 5 | import { isActiveSubagentStatus } from "@t3tools/client-runtime/state/subagentRuntime"; | @@ -8,8 +8,7 @@ import type { | 8 | 8 | OrchestrationV2Subagent, | 9 | 9 | OrchestrationV2TurnItem, | 10 | 10 | } 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"; | 13 | 12 |
| 14 | 13 | import { AppText as Text } from "../../components/ui/AppText"; | 15 | 14 | import { SymbolView } from "../../components/ui/AppSymbol"; | @@ -20,6 +19,7 @@ import { serverEnvironment } from "../../state/server"; | 20 | 19 | import { environmentThreadDetails } from "../../state/threads"; | 21 | 20 | import { subagentCardElapsed } from "./subagent-card-presentation"; | 22 | 21 | import { SubagentRow } from "./SubagentRow"; | | 22 | +import { useVisibleSecondClock } from "./use-visible-second-clock"; | 23 | 23 | import { WorkLogBlock } from "./work-log-layout"; | 24 | 24 |
| 25 | 25 | import { useOpenThreadInspector } from "./thread-inspector-navigation"; | @@ -28,21 +28,8 @@ type SubagentItem = Extract<OrchestrationV2TurnItem, { type: "subagent" }>; | 28 | 28 | type AgentTiming = Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">; | 29 | 29 |
| 30 | 30 | function SubagentElapsed({ agents }: { readonly agents: ReadonlyArray<AgentTiming> }) { | 31 | | - const focused = useIsFocused(); | 32 | 31 | 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); | 46 | 33 | const elapsed = subagentCardElapsed(agents, nowMs); | 47 | 34 | return elapsed ? ( | 48 | 35 | <Text className="shrink-0 text-xs tabular-nums text-foreground-muted">{elapsed}</Text> | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/useFileChipShare.ts +34-43Lines changed: 34 additions & 43 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import * as Dialog from "../../components/ui/Dialog"; | 2 | 2 | import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets"; | 3 | 3 | import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; | 4 | 4 | import * as Option from "effect/Option"; | 5 | | -import { useCallback, useEffect, useLayoutEffect, useRef } from "react"; | | 5 | +import { useEffect, useRef } from "react"; | 6 | 6 |
| 7 | 7 | import { downloadAndShareAttachment } from "../../lib/attachmentDownload"; | 8 | 8 | import { assetEnvironment } from "../../state/assets"; | @@ -21,49 +21,40 @@ export function useFileChipShare( | 21 | 21 | refresh: true, | 22 | 22 | reportFailure: false, | 23 | 23 | }); | 24 | | - const connectionRef = useRef(httpBaseUrl); | 25 | | - useLayoutEffect(() => { | 26 | | - connectionRef.current = httpBaseUrl; | 27 | | - }, [httpBaseUrl]); | 28 | 24 | const requestRef = useRef<AbortController | null>(null); | 29 | 25 | useEffect(() => () => requestRef.current?.abort(), []); | 30 | 26 |
| 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 | + }; | 69 | 60 | } |
|