Skip to content

Commit 803fba2

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-16000
2 parents 4a3dab0 + d53873b commit 803fba2

6 files changed

Lines changed: 77 additions & 17 deletions

File tree

‎apps/mobile/src/components/ComposerEditor.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ComposerContextId } from "@t3tools/contracts";
33
import { useAtomValue } from "@effect/atom-react";
44
import { AsyncResult } from "effect/reactivity";
55
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
6-
import { useNavigation } from "@react-navigation/native";
76
import { useEffect, useRef, useState } from "react";
87
import type { EnvironmentId } from "@t3tools/contracts";
98
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
@@ -24,6 +23,7 @@ import {
2423
} from "../state/use-composer-drafts";
2524
import { importComposerContextClipboard } from "../lib/composerContextClipboard";
2625
import { mobilePreferencesAtom } from "../state/preferences";
26+
import { useOpenLinkedThread } from "../features/threads/useOpenLinkedThread";
2727
import { ComposerContextSheet } from "./ComposerContextSheet";
2828
import { AppText as Text } from "./ui/AppText";
2929
import {
@@ -154,7 +154,7 @@ export function ComposerEditor({
154154
}),
155155
})
156156
: "";
157-
const navigation = useNavigation();
157+
const openLinkedThread = useOpenLinkedThread();
158158
const selectedReference = selected
159159
? collectComposerContextReferences(selected.source)[0]
160160
: undefined;
@@ -236,7 +236,7 @@ export function ComposerEditor({
236236
: {})}
237237
onOpenThread={(thread) => {
238238
setSelected(null);
239-
navigation.navigate("Thread", {
239+
openLinkedThread({
240240
environmentId: String(thread.environmentId),
241241
threadId: String(thread.threadId),
242242
});

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ import {
147147
pendingThreadMessageJump,
148148
subscribeThreadMessageJump,
149149
} from "./threadMessageJump";
150-
import { parentThreadIsBelow } from "../../lib/android-workspace-navigation";
151150
import {
152151
clearThreadComposerError,
153152
threadComposerErrorsAtom,
@@ -191,6 +190,7 @@ import { ComposerQueuedEditBanner } from "./ComposerQueuedEdit";
191190
import { useThreadQueuedCount } from "./ThreadQueueControl";
192191
import type { ThreadContentPresentation } from "./threadContentPresentation";
193192
import { resolveThreadFeedSubmissionAnchor } from "./thread-feed-live-follow";
193+
import { useOpenLinkedThread } from "./useOpenLinkedThread";
194194
import { useGlobalVoiceInput } from "../voice-input/VoiceInputProvider";
195195

196196
export interface ThreadDetailScreenProps {
@@ -513,6 +513,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
513513
const [composerOwnsKeyboard, setComposerOwnsKeyboard] = useState(false);
514514
if (sidebarSearchActive && composerOwnsKeyboard) setComposerOwnsKeyboard(false);
515515
const navigation = useNavigation();
516+
const openLinkedThread = useOpenLinkedThread();
516517
const { session: voiceInputSession } = useGlobalVoiceInput();
517518
const reportedModelSelection = useThreadReportedModelSelection(props.selectedThreadDetailRef);
518519
const deviceState = useEnvironmentQuery(
@@ -1747,17 +1748,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
17471748
onOpenParent={
17481749
props.selectedThread.lineage.parentThreadId === null
17491750
? null
1750-
: () => {
1751-
const parent = {
1751+
: () =>
1752+
openLinkedThread({
17521753
environmentId: String(props.environmentId),
17531754
threadId: String(props.selectedThread.lineage.parentThreadId),
1754-
};
1755-
navigation.dispatch((state) =>
1756-
parentThreadIsBelow(state, parent)
1757-
? StackActions.pop()
1758-
: StackActions.push("Thread", parent),
1759-
);
1760-
}
1755+
})
17611756
}
17621757
/>
17631758
</View>

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ import { waitForThreadShellReady } from "./threadForkNavigation";
270270
import { resolveUserMessageIntentBadge } from "./userMessageIntentBadge";
271271
import { fileChipClipboardPath, fileChipMenu, resolveFileChipTarget } from "./fileChipMenu";
272272
import { useFileChipShare } from "./useFileChipShare";
273+
import { useOpenLinkedThread } from "./useOpenLinkedThread";
273274
import {
274275
MarkdownImageAvailableWidthContext,
275276
ThreadMarkdownImage,
@@ -1055,13 +1056,13 @@ function OpenSenderThreadButton(props: {
10551056
readonly environmentId: EnvironmentId;
10561057
readonly senderThreadId: ThreadId;
10571058
}) {
1058-
const navigation = useNavigation();
1059+
const openLinkedThread = useOpenLinkedThread();
10591060
return (
10601061
<Pressable
10611062
accessibilityRole="link"
10621063
hitSlop={6}
10631064
onPress={() =>
1064-
navigation.navigate("Thread", {
1065+
openLinkedThread({
10651066
environmentId: String(props.environmentId),
10661067
threadId: String(props.senderThreadId),
10671068
})
@@ -1797,6 +1798,7 @@ type UserMessageContentProps = {
17971798
function UserMessageContent(props: UserMessageContentProps) {
17981799
const [selected, setSelected] = useState<{ contextId: string; label: string } | null>(null);
17991800
const navigation = useNavigation();
1801+
const openLinkedThread = useOpenLinkedThread();
18001802
const { selectedThread } = useThreadSelection();
18011803
const liveText = useLiveThreadLinkLabels(props.text, props.environmentId);
18021804
const threadIcons = useThreadChipIcons(props.environmentId, liveText, props.context?.records);
@@ -1815,7 +1817,7 @@ function UserMessageContent(props: UserMessageContentProps) {
18151817
return;
18161818
}
18171819
if (record?.kind === "thread" && "threadId" in record) {
1818-
navigation.navigate("Thread", {
1820+
openLinkedThread({
18191821
environmentId: String(record.environmentId),
18201822
threadId: String(record.threadId),
18211823
});
@@ -2026,6 +2028,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20262028
const duoColumns = useNativeWorkspaceColumnsSupported() && isDuoDevice(deviceType);
20272029
const openPullRequest = useOpenPullRequest();
20282030
const navigation = useNavigation();
2031+
const openLinkedThread = useOpenLinkedThread();
20292032
const onOpenTurnDiff = useCallback(
20302033
(checkpoint: ThreadCheckpointSummary) => {
20312034
setReviewSelectedSectionId(
@@ -2210,7 +2213,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
22102213
}
22112214
const target = resolveLinkedThread(href);
22122215
if (target) {
2213-
navigation.navigate("Thread", {
2216+
openLinkedThread({
22142217
environmentId: String(target.environmentId),
22152218
threadId: String(target.threadId),
22162219
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { StackActions, useNavigation } from "@react-navigation/native";
2+
import { useCallback } from "react";
3+
import { linkedThreadStep } from "../../lib/android-workspace-navigation";
4+
5+
export function useOpenLinkedThread() {
6+
const navigation = useNavigation();
7+
return useCallback(
8+
(target: { readonly environmentId: string; readonly threadId: string }) => {
9+
const state = navigation.getState();
10+
const step = state ? linkedThreadStep(state, target) : "push";
11+
if (step === "pop") navigation.dispatch(StackActions.pop());
12+
else if (step === "push") navigation.dispatch(StackActions.push("Thread", target));
13+
},
14+
[navigation],
15+
);
16+
}

‎apps/mobile/src/lib/android-workspace-navigation.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "../features/workspace/android-workspace-tabs";
88
import {
99
androidWorkspaceNavigationState,
10+
linkedThreadStep,
1011
parentThreadIsBelow,
1112
resetAndroidWorkspace,
1213
returnToAndroidWorkspaceList,
@@ -202,6 +203,30 @@ describe("Android workspace navigation", () => {
202203
});
203204
});
204205

206+
it("opens a linked thread above the current one so Back returns to it", () => {
207+
const parent = select(router.getInitialState(options), thread);
208+
const child = { ...thread.params, threadId: "child-a" };
209+
const open = (
210+
state: ReturnType<typeof router.getInitialState>,
211+
target: ReactNavigation.RootParamList["Thread"],
212+
) => {
213+
const step = linkedThreadStep(state, target);
214+
if (step === "stay") return state;
215+
return apply(
216+
state,
217+
step === "pop" ? StackActions.pop() : StackActions.push("Thread", target),
218+
);
219+
};
220+
221+
const inChild = open(parent, child);
222+
expect(inChild.routes.map((route) => route.params)).toEqual([undefined, thread.params, child]);
223+
expect(open(inChild, child)).toBe(inChild);
224+
225+
const backInParent = open(inChild, thread.params);
226+
expect(backInParent.routes).toEqual(parent.routes);
227+
expect(backInParent.index).toBe(parent.index);
228+
});
229+
205230
it("recognizes a parent only when its environment and thread are immediately below", () => {
206231
const parent = select(router.getInitialState(options), thread);
207232
const child = apply(

‎apps/mobile/src/lib/android-workspace-navigation.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ export function parentThreadIsBelow(
9292
);
9393
}
9494

95+
/**
96+
* How a thread link opened from inside a thread reaches its target. `navigate` would reuse the
97+
* current Thread route, so Back would skip the thread the link was in and land on the list.
98+
*/
99+
export function linkedThreadStep(
100+
state: NavigationState,
101+
target: ReactNavigation.RootParamList["Thread"],
102+
): "stay" | "pop" | "push" {
103+
const params = state.routes[state.index]?.params as
104+
| ReactNavigation.RootParamList["Thread"]
105+
| undefined;
106+
if (
107+
state.routes[state.index]?.name === "Thread" &&
108+
params?.environmentId === target?.environmentId &&
109+
params?.threadId === target?.threadId
110+
) {
111+
return "stay";
112+
}
113+
return parentThreadIsBelow(state, target) ? "pop" : "push";
114+
}
115+
95116
export function returnToAndroidWorkspaceList(
96117
navigation: WorkspaceNavigation & { readonly goBack: () => void },
97118
selectTab: () => void,

0 commit comments

Comments
 (0)