Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-inspector-navigation.ts

48 lines1.7 KB

1import { StackActions, useNavigation } from "@react-navigation/native";
2import { createContext, use } from "react";
3import { Platform } from "react-native";
4​
5import { useWorkspacePaneState } from "../layout/AdaptiveWorkspaceLayout";
6​
7export type ThreadInspectorMode = "route" | "git" | "files" | "preview";
8​
9export type ThreadInspectorTarget = {
10 readonly type: "thread" | "device" | "localPreview";
11 readonly environmentId: string;
12 readonly threadId: string;
13 readonly deviceKey?: string;
14 /** The environment's localhost page a "localPreview" shows. */
15 readonly url?: string;
16};
17​
18export const ThreadInspectorNavigationContext = createContext<
19 ((target: ThreadInspectorTarget) => void) | null
20>(null);
21​
22export function useOpenThreadInspector() {
23 const navigation = useNavigation();
24 const { fileInspector } = useWorkspacePaneState();
25 const openInInspector = use(ThreadInspectorNavigationContext);
26 return (
27 source: { readonly environmentId: string; readonly threadId: string },
28 target: ThreadInspectorTarget,
29 ) => {
30 if (Platform.OS !== "ios" || !fileInspector.supported) return false;
31 if (openInInspector) openInInspector(target);
32 else {
33 const parent = navigation.getState()?.routes.findLast((route) => route.name === "Thread");
34 if (
35 !parent?.params ||
36 !("environmentId" in parent.params) ||
37 parent.params.environmentId !== source.environmentId ||
38 !("threadId" in parent.params) ||
39 parent.params.threadId !== source.threadId
40 )
41 return false;
42 navigation.dispatch(
43 StackActions.popTo("Thread", { ...source, inspector: target }, { merge: true }),
44 );
45 }
46 return true;
47 };