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

thread-inspector-navigation.ts

78 lines2.8 KB

1import { StackActions, useNavigation } from "@react-navigation/native";
2import { createContext, use } from "react";
3​
4import { resolveMobileSideThreadPresentation } from "@t3tools/client-runtime/side-thread";
5​
6import { useWorkspacePaneState } from "../layout/AdaptiveWorkspaceLayout";
7​
8export type ThreadInspectorMode = "route" | "git" | "files" | "preview";
9​
10export type ThreadInspectorTarget = {
11 /** "linkedThread" is a thread on a linked environment, read through `linked.viaEnvironmentId`. */
12 readonly type: "thread" | "linkedThread" | "device" | "localPreview";
13 readonly environmentId: string;
14 readonly threadId: string;
15 readonly linked?: {
16 readonly viaEnvironmentId: string;
17 readonly label: string;
18 readonly title?: string;
19 };
20 readonly deviceKey?: string;
21 /** The environment's localhost page a "localPreview" shows. */
22 readonly url?: string;
23};
24​
25export const ThreadInspectorNavigationContext = createContext<
26 ((target: ThreadInspectorTarget) => void) | null
27>(null);
28​
29export function useOpenThreadInspector() {
30 const navigation = useNavigation();
31 const { fileInspector } = useWorkspacePaneState();
32 const openInInspector = use(ThreadInspectorNavigationContext);
33 return (
34 source: { readonly environmentId: string; readonly threadId: string },
35 target: ThreadInspectorTarget,
36 ) => {
37 const presentation = resolveMobileSideThreadPresentation({
38 inspectorAvailable: fileInspector.supported,
39 });
40 if (presentation === "full-screen") return false;
41 if (openInInspector) openInInspector(target);
42 else {
43 if (!isNearestThreadRoute(navigation.getState(), source)) return false;
44 navigation.dispatch(
45 StackActions.popTo("Thread", { ...source, inspector: target }, { merge: true }),
46 );
47 }
48 return true;
49 };
51​
52/** Closes the screens above `thread`, or opens it when the nearest thread route is another one. */
53export function useReturnToThread() {
54 const navigation = useNavigation();
55 return (thread: { readonly environmentId: string; readonly threadId: string }) =>
56 navigation.dispatch(
57 isNearestThreadRoute(navigation.getState(), thread)
58 ? StackActions.popTo("Thread", thread, { merge: true })
59 : StackActions.replace("Thread", thread),
60 );
62​
63// `Thread` has no `getId`, so `popTo("Thread")` lands on the nearest one whatever thread it shows.
64function isNearestThreadRoute(
65 state:
66 | { readonly routes: ReadonlyArray<{ readonly name: string; readonly params?: object }> }
67 | undefined,
68 thread: { readonly environmentId: string; readonly threadId: string },
69) {
70 const params = state?.routes.findLast((route) => route.name === "Thread")?.params;
71 return (
72 params !== undefined &&
73 "environmentId" in params &&
74 params.environmentId === thread.environmentId &&
75 "threadId" in params &&
76 params.threadId === thread.threadId
77 );