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

thread-inspector-navigation.ts

44 lines1.5 KB

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