The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { StackActions, useNavigation } from "@react-navigation/native"; |
| 2 | import { createContext, use } from "react"; |
| 3 | import { Platform } from "react-native"; |
| 4 | |
| 5 | import { resolveMobileSideThreadPresentation } from "@t3tools/client-runtime/side-thread"; |
| 6 | |
| 7 | import { useWorkspacePaneState } from "../layout/AdaptiveWorkspaceLayout"; |
| 8 | |
| 9 | export type ThreadInspectorMode = "route" | "git" | "files" | "preview"; |
| 10 | |
| 11 | export type ThreadInspectorTarget = { |
| 12 | /** "linkedThread" is a thread on a linked environment, read through `linked.viaEnvironmentId`. */ |
| 13 | readonly type: "thread" | "linkedThread" | "device" | "localPreview"; |
| 14 | readonly environmentId: string; |
| 15 | readonly threadId: string; |
| 16 | readonly linked?: { |
| 17 | readonly viaEnvironmentId: string; |
| 18 | readonly label: string; |
| 19 | readonly title?: string; |
| 20 | }; |
| 21 | readonly deviceKey?: string; |
| 22 | /** The environment's localhost page a "localPreview" shows. */ |
| 23 | readonly url?: string; |
| 24 | }; |
| 25 | |
| 26 | export const ThreadInspectorNavigationContext = createContext< |
| 27 | ((target: ThreadInspectorTarget) => void) | null |
| 28 | >(null); |
| 29 | |
| 30 | export function useOpenThreadInspector() { |
| 31 | const navigation = useNavigation(); |
| 32 | const { fileInspector } = useWorkspacePaneState(); |
| 33 | const openInInspector = use(ThreadInspectorNavigationContext); |
| 34 | return ( |
| 35 | source: { readonly environmentId: string; readonly threadId: string }, |
| 36 | target: ThreadInspectorTarget, |
| 37 | ) => { |
| 38 | const presentation = resolveMobileSideThreadPresentation({ |
| 39 | inspectorAvailable: Platform.OS === "ios" && fileInspector.supported, |
| 40 | }); |
| 41 | if (presentation === "full-screen") return false; |
| 42 | if (openInInspector) openInInspector(target); |
| 43 | else { |
| 44 | const parent = navigation.getState()?.routes.findLast((route) => route.name === "Thread"); |
| 45 | if ( |
| 46 | !parent?.params || |
| 47 | !("environmentId" in parent.params) || |
| 48 | parent.params.environmentId !== source.environmentId || |
| 49 | !("threadId" in parent.params) || |
| 50 | parent.params.threadId !== source.threadId |
| 51 | ) |
| 52 | return false; |
| 53 | navigation.dispatch( |
| 54 | StackActions.popTo("Thread", { ...source, inspector: target }, { merge: true }), |
| 55 | ); |
| 56 | } |
| 57 | return true; |
| 58 | }; |
| 59 | } |