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

thread-inspector-navigation.ts

79 lines2.9 KB

1import { StackActions, useNavigation } from "@react-navigation/native";
2import { createContext, use } from "react";
3import { Platform } from "react-native";
4​
5import { resolveMobileSideThreadPresentation } from "@t3tools/client-runtime/side-thread";
6​
7import { useWorkspacePaneState } from "../layout/AdaptiveWorkspaceLayout";
8​
9export type ThreadInspectorMode = "route" | "git" | "files" | "preview";
10​
11export 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​
26export const ThreadInspectorNavigationContext = createContext<
27 ((target: ThreadInspectorTarget) => void) | null
28>(null);
29​
30export 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 if (!isNearestThreadRoute(navigation.getState(), source)) return false;
45 navigation.dispatch(
46 StackActions.popTo("Thread", { ...source, inspector: target }, { merge: true }),
47 );
48 }
49 return true;
50 };
52​
53/** Closes the screens above `thread`, or opens it when the nearest thread route is another one. */
54export function useReturnToThread() {
55 const navigation = useNavigation();
56 return (thread: { readonly environmentId: string; readonly threadId: string }) =>
57 navigation.dispatch(
58 isNearestThreadRoute(navigation.getState(), thread)
59 ? StackActions.popTo("Thread", thread, { merge: true })
60 : StackActions.replace("Thread", thread),
61 );
63​
64// `Thread` has no `getId`, so `popTo("Thread")` lands on the nearest one whatever thread it shows.
65function isNearestThreadRoute(
66 state:
67 | { readonly routes: ReadonlyArray<{ readonly name: string; readonly params?: object }> }
68 | undefined,
69 thread: { readonly environmentId: string; readonly threadId: string },
70) {
71 const params = state?.routes.findLast((route) => route.name === "Thread")?.params;
72 return (
73 params !== undefined &&
74 "environmentId" in params &&
75 params.environmentId === thread.environmentId &&
76 "threadId" in params &&
77 params.threadId === thread.threadId
78 );