The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { StackActions, useNavigation, useRoute, type Route } from "@react-navigation/native"; |
| 2 | import { use } from "react"; |
| 3 | import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 4 | |
| 5 | import { linkedThreadStep } from "../../lib/android-workspace-navigation"; |
| 6 | import { linkedThreadRouteParams } from "./linkedThreadRoute"; |
| 7 | import { |
| 8 | ThreadInspectorNavigationContext, |
| 9 | type ThreadInspectorTarget, |
| 10 | useOpenThreadInspector, |
| 11 | } from "./thread-inspector-navigation"; |
| 12 | |
| 13 | export function useOpenLinkedThread() { |
| 14 | const navigation = useNavigation(); |
| 15 | const route = useRoute<Route<string, { environmentId?: string; threadId?: string }>>(); |
| 16 | const openInspector = useOpenThreadInspector(); |
| 17 | const openInInspector = use(ThreadInspectorNavigationContext); |
| 18 | return (target: { readonly environmentId: string; readonly threadId: string }) => { |
| 19 | if (openInInspector) { |
| 20 | openInInspector({ type: "thread", ...target }); |
| 21 | return; |
| 22 | } |
| 23 | const state = navigation.getState(); |
| 24 | const step = state ? linkedThreadStep(state, target) : "push"; |
| 25 | if ( |
| 26 | step === "push" && |
| 27 | route.name === "Thread" && |
| 28 | route.params?.environmentId && |
| 29 | route.params.threadId && |
| 30 | openInspector( |
| 31 | { environmentId: route.params.environmentId, threadId: route.params.threadId }, |
| 32 | { type: "thread", ...target }, |
| 33 | ) |
| 34 | ) |
| 35 | return; |
| 36 | if (step === "pop") navigation.dispatch(StackActions.pop()); |
| 37 | else if (step === "push") navigation.dispatch(StackActions.push("Thread", target)); |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Opens a linked environment's thread read-only, read through `linked.viaEnvironmentId`: in the |
| 43 | * inspector where the layout has one, as subagents and side chats do, else as its own screen. |
| 44 | * `source` is the thread it opens from when the caller is not that thread's own screen. |
| 45 | */ |
| 46 | export function useOpenLinkedThreadRead() { |
| 47 | const navigation = useNavigation(); |
| 48 | const route = useRoute<Route<string, { environmentId?: string; threadId?: string }>>(); |
| 49 | const openInspector = useOpenThreadInspector(); |
| 50 | const openInInspector = use(ThreadInspectorNavigationContext); |
| 51 | return ( |
| 52 | thread: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId }, |
| 53 | linked: { readonly viaEnvironmentId: EnvironmentId; readonly label: string }, |
| 54 | title: string | null, |
| 55 | options?: { |
| 56 | readonly source?: { readonly environmentId: string; readonly threadId: string }; |
| 57 | /** Replaces the calling sheet instead of stacking on it. */ |
| 58 | readonly replace?: boolean; |
| 59 | }, |
| 60 | ) => { |
| 61 | const params = linkedThreadRouteParams(thread, linked, title); |
| 62 | const target: ThreadInspectorTarget = { |
| 63 | type: "linkedThread", |
| 64 | environmentId: params.environmentId, |
| 65 | threadId: params.threadId, |
| 66 | linked: { |
| 67 | viaEnvironmentId: params.viaEnvironmentId, |
| 68 | label: params.label, |
| 69 | ...(params.title === undefined ? {} : { title: params.title }), |
| 70 | }, |
| 71 | }; |
| 72 | if (openInInspector) { |
| 73 | openInInspector(target); |
| 74 | return; |
| 75 | } |
| 76 | const source = |
| 77 | options?.source ?? |
| 78 | (route.name === "Thread" && route.params?.environmentId && route.params.threadId |
| 79 | ? { environmentId: route.params.environmentId, threadId: route.params.threadId } |
| 80 | : null); |
| 81 | if (source !== null && openInspector(source, target)) return; |
| 82 | navigation.dispatch( |
| 83 | options?.replace |
| 84 | ? StackActions.replace("LinkedThread", params) |
| 85 | : StackActions.push("LinkedThread", params), |
| 86 | ); |
| 87 | }; |
| 88 | } |