Open History to see the last commit
1import { StackActions, useNavigation, useRoute, type Route } from "@react-navigation/native";
2import { use, useCallback } from "react";
3import { linkedThreadStep } from "../../lib/android-workspace-navigation";
4import {
5 ThreadInspectorNavigationContext,
6 useOpenThreadInspector,
7} from "./thread-inspector-navigation";
8
9export function useOpenLinkedThread() {
10 const navigation = useNavigation();
11 const route = useRoute<Route<string, { environmentId?: string; threadId?: string }>>();
12 const openInspector = useOpenThreadInspector();
13 const openInInspector = use(ThreadInspectorNavigationContext);
14 return useCallback(
15 (target: { readonly environmentId: string; readonly threadId: string }) => {
16 if (openInInspector) {
17 openInInspector({ type: "thread", ...target });
18 return;
19 }
20 const state = navigation.getState();
21 const step = state ? linkedThreadStep(state, target) : "push";
22 if (
23 step === "push" &&
24 route.name === "Thread" &&
25 route.params?.environmentId &&
26 route.params.threadId &&
27 openInspector(
28 { environmentId: route.params.environmentId, threadId: route.params.threadId },
29 { type: "thread", ...target },
30 )
31 )
32 return;
33 if (step === "pop") navigation.dispatch(StackActions.pop());
34 else if (step === "push") navigation.dispatch(StackActions.push("Thread", target));
35 },
36 [navigation, openInInspector, openInspector, route],
37 );
38}