TypeScript
fix(mobile): Back from a sub-agent opened from its delegator returns to the delegator (#23795)
shivamd53873b6a479
diff --git a/apps/mobile/src/components/ComposerEditor.tsx b/apps/mobile/src/components/ComposerEditor.tsxindex f68c5396d84..8dfbb5587b6 100644--- a/apps/mobile/src/components/ComposerEditor.tsx+++ b/apps/mobile/src/components/ComposerEditor.tsx@@ -3,7 +3,6 @@ import { ComposerContextId } from "@t3tools/contracts";import { useAtomValue } from "@effect/atom-react";import { AsyncResult } from "effect/reactivity";import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";-import { useNavigation } from "@react-navigation/native";import { useEffect, useRef, useState } from "react";import type { EnvironmentId } from "@t3tools/contracts";import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";@@ -24,6 +23,7 @@ import {} from "../state/use-composer-drafts";import { importComposerContextClipboard } from "../lib/composerContextClipboard";import { mobilePreferencesAtom } from "../state/preferences";+import { useOpenLinkedThread } from "../features/threads/useOpenLinkedThread";import { ComposerContextSheet } from "./ComposerContextSheet";import { AppText as Text } from "./ui/AppText";import {@@ -154,7 +154,7 @@ export function ComposerEditor({}),}): "";- const navigation = useNavigation();+ const openLinkedThread = useOpenLinkedThread();const selectedReference = selected? collectComposerContextReferences(selected.source)[0]: undefined;@@ -236,7 +236,7 @@ export function ComposerEditor({: {})}onOpenThread={(thread) => {setSelected(null);- navigation.navigate("Thread", {+ openLinkedThread({environmentId: String(thread.environmentId),threadId: String(thread.threadId),});diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsxindex 3a8cb765b6e..83fd54f2ac0 100644--- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx@@ -147,7 +147,6 @@ import {pendingThreadMessageJump,subscribeThreadMessageJump,} from "./threadMessageJump";-import { parentThreadIsBelow } from "../../lib/android-workspace-navigation";import {clearThreadComposerError,threadComposerErrorsAtom,@@ -191,6 +190,7 @@ import { ComposerQueuedEditBanner } from "./ComposerQueuedEdit";import { useThreadQueuedCount } from "./ThreadQueueControl";import type { ThreadContentPresentation } from "./threadContentPresentation";import { resolveThreadFeedSubmissionAnchor } from "./thread-feed-live-follow";+import { useOpenLinkedThread } from "./useOpenLinkedThread";import { useGlobalVoiceInput } from "../voice-input/VoiceInputProvider";export interface ThreadDetailScreenProps {@@ -513,6 +513,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetaiconst [composerOwnsKeyboard, setComposerOwnsKeyboard] = useState(false);if (sidebarSearchActive && composerOwnsKeyboard) setComposerOwnsKeyboard(false);const navigation = useNavigation();+ const openLinkedThread = useOpenLinkedThread();const { session: voiceInputSession } = useGlobalVoiceInput();const reportedModelSelection = useThreadReportedModelSelection(props.selectedThreadDetailRef);const deviceState = useEnvironmentQuery(@@ -1747,17 +1748,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetaionOpenParent={props.selectedThread.lineage.parentThreadId === null? null- : () => {- const parent = {+ : () =>+ openLinkedThread({environmentId: String(props.environmentId),threadId: String(props.selectedThread.lineage.parentThreadId),- };- navigation.dispatch((state) =>- parentThreadIsBelow(state, parent)- ? StackActions.pop()- : StackActions.push("Thread", parent),- );- }+ })}/></View>diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsxindex 5d29e519917..48b1346646a 100644--- a/apps/mobile/src/features/threads/ThreadFeed.tsx+++ b/apps/mobile/src/features/threads/ThreadFeed.tsx@@ -270,6 +270,7 @@ import { waitForThreadShellReady } from "./threadForkNavigation";import { resolveUserMessageIntentBadge } from "./userMessageIntentBadge";import { fileChipClipboardPath, fileChipMenu, resolveFileChipTarget } from "./fileChipMenu";import { useFileChipShare } from "./useFileChipShare";+import { useOpenLinkedThread } from "./useOpenLinkedThread";import {MarkdownImageAvailableWidthContext,ThreadMarkdownImage,@@ -1055,13 +1056,13 @@ function OpenSenderThreadButton(props: {readonly environmentId: EnvironmentId;readonly senderThreadId: ThreadId;}) {- const navigation = useNavigation();+ const openLinkedThread = useOpenLinkedThread();return (<PressableaccessibilityRole="link"hitSlop={6}onPress={() =>- navigation.navigate("Thread", {+ openLinkedThread({environmentId: String(props.environmentId),threadId: String(props.senderThreadId),})@@ -1797,6 +1798,7 @@ type UserMessageContentProps = {function UserMessageContent(props: UserMessageContentProps) {const [selected, setSelected] = useState<{ contextId: string; label: string } | null>(null);const navigation = useNavigation();+ const openLinkedThread = useOpenLinkedThread();const { selectedThread } = useThreadSelection();const liveText = useLiveThreadLinkLabels(props.text, props.environmentId);const threadIcons = useThreadChipIcons(props.environmentId, liveText, props.context?.records);@@ -1815,7 +1817,7 @@ function UserMessageContent(props: UserMessageContentProps) {return;}if (record?.kind === "thread" && "threadId" in record) {- navigation.navigate("Thread", {+ openLinkedThread({environmentId: String(record.environmentId),threadId: String(record.threadId),});@@ -2026,6 +2028,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {const duoColumns = useNativeWorkspaceColumnsSupported() && isDuoDevice(deviceType);const openPullRequest = useOpenPullRequest();const navigation = useNavigation();+ const openLinkedThread = useOpenLinkedThread();const onOpenTurnDiff = useCallback((checkpoint: ThreadCheckpointSummary) => {setReviewSelectedSectionId(@@ -2210,7 +2213,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {}const target = resolveLinkedThread(href);if (target) {- navigation.navigate("Thread", {+ openLinkedThread({environmentId: String(target.environmentId),threadId: String(target.threadId),});diff --git a/apps/mobile/src/features/threads/useOpenLinkedThread.ts b/apps/mobile/src/features/threads/useOpenLinkedThread.tsnew file mode 100644index 00000000000..a3ecd7f685a--- /dev/null+++ b/apps/mobile/src/features/threads/useOpenLinkedThread.ts@@ -0,0 +1,16 @@+import { StackActions, useNavigation } from "@react-navigation/native";+import { useCallback } from "react";+import { linkedThreadStep } from "../../lib/android-workspace-navigation";++export function useOpenLinkedThread() {+ const navigation = useNavigation();+ return useCallback(+ (target: { readonly environmentId: string; readonly threadId: string }) => {+ const state = navigation.getState();+ const step = state ? linkedThreadStep(state, target) : "push";+ if (step === "pop") navigation.dispatch(StackActions.pop());+ else if (step === "push") navigation.dispatch(StackActions.push("Thread", target));+ },+ [navigation],+ );+}diff --git a/apps/mobile/src/lib/android-workspace-navigation.test.ts b/apps/mobile/src/lib/android-workspace-navigation.test.tsindex ac163d77214..88c4e945fe3 100644--- a/apps/mobile/src/lib/android-workspace-navigation.test.ts+++ b/apps/mobile/src/lib/android-workspace-navigation.test.ts@@ -7,6 +7,7 @@ import {} from "../features/workspace/android-workspace-tabs";import {androidWorkspaceNavigationState,+ linkedThreadStep,parentThreadIsBelow,resetAndroidWorkspace,returnToAndroidWorkspaceList,@@ -202,6 +203,30 @@ describe("Android workspace navigation", () => {});});+ it("opens a linked thread above the current one so Back returns to it", () => {+ const parent = select(router.getInitialState(options), thread);+ const child = { ...thread.params, threadId: "child-a" };+ const open = (+ state: ReturnType<typeof router.getInitialState>,+ target: ReactNavigation.RootParamList["Thread"],+ ) => {+ const step = linkedThreadStep(state, target);+ if (step === "stay") return state;+ return apply(+ state,+ step === "pop" ? StackActions.pop() : StackActions.push("Thread", target),+ );+ };++ const inChild = open(parent, child);+ expect(inChild.routes.map((route) => route.params)).toEqual([undefined, thread.params, child]);+ expect(open(inChild, child)).toBe(inChild);++ const backInParent = open(inChild, thread.params);+ expect(backInParent.routes).toEqual(parent.routes);+ expect(backInParent.index).toBe(parent.index);+ });+it("recognizes a parent only when its environment and thread are immediately below", () => {const parent = select(router.getInitialState(options), thread);const child = apply(diff --git a/apps/mobile/src/lib/android-workspace-navigation.ts b/apps/mobile/src/lib/android-workspace-navigation.tsindex 138704a03e6..af7ff01440f 100644--- a/apps/mobile/src/lib/android-workspace-navigation.ts+++ b/apps/mobile/src/lib/android-workspace-navigation.ts@@ -92,6 +92,27 @@ export function parentThreadIsBelow();}+/**+ * How a thread link opened from inside a thread reaches its target. `navigate` would reuse the+ * current Thread route, so Back would skip the thread the link was in and land on the list.+ */+export function linkedThreadStep(+ state: NavigationState,+ target: ReactNavigation.RootParamList["Thread"],+): "stay" | "pop" | "push" {+ const params = state.routes[state.index]?.params as+ | ReactNavigation.RootParamList["Thread"]+ | undefined;+ if (+ state.routes[state.index]?.name === "Thread" &&+ params?.environmentId === target?.environmentId &&+ params?.threadId === target?.threadId+ ) {+ return "stay";+ }+ return parentThreadIsBelow(state, target) ? "pop" : "push";+}+export function returnToAndroidWorkspaceList(navigation: WorkspaceNavigation & { readonly goBack: () => void },selectTab: () => void,