Skip to content
Source
Sign inSign up

thread-inspector-navigation.ts

46 lines1.6 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";
11 readonly environmentId: string;
12 readonly threadId: string;
13 readonly deviceKey?: string;
14};
15​
16export const ThreadInspectorNavigationContext = createContext<
17 ((target: ThreadInspectorTarget) => void) | null
18>(null);
19​
20export function useOpenThreadInspector() {
21 const navigation = useNavigation();
22 const { fileInspector } = useWorkspacePaneState();
23 const openInInspector = use(ThreadInspectorNavigationContext);
24 return (
25 source: { readonly environmentId: string; readonly threadId: string },
26 target: ThreadInspectorTarget,
27 ) => {
28 if (Platform.OS !== "ios" || !fileInspector.supported) return false;
29 if (openInInspector) openInInspector(target);
30 else {
31 const parent = navigation.getState()?.routes.findLast((route) => route.name === "Thread");
32 if (
33 !parent?.params ||
34 !("environmentId" in parent.params) ||
35 parent.params.environmentId !== source.environmentId ||
36 !("threadId" in parent.params) ||
37 parent.params.threadId !== source.threadId
38 )
39 return false;
40 navigation.dispatch(
41 StackActions.popTo("Thread", { ...source, inspector: target }, { merge: true }),
42 );
43 }
44 return true;
45 };