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

inspector-host.tsx

120 lines3.9 KB

1import { useGT } from "gt-react-native";
2import { useSyncExternalStore, type ReactNode } from "react";
3import { Split, Stack } from "react-native-screens";
4​
5import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary";
6import { PaneContext } from "../../components/ui/Pane";
7import { GLASS_HEADER_OPTIONS } from "../../native/glass-header-options";
8import { NativeColumnContent } from "../../native/NativeColumnContent.ios";
9import { V5StackHeader } from "../../native/V5StackHeader.ios";
10import { WorkspaceSlotOptionsContext } from "../../native/v5-workspace-context";
11import {
12 InspectorCloseContext,
13 useInspectorBarSlot,
14 type InspectorBarStore,
15} from "./inspector-bar-store";
16import {
17 useMountedInspectorPanes,
18 type InspectorContent,
19 type InspectorPane,
20} from "./inspector-panes";
21​
22// A pane without an AppBar has no bar; one with it gets the glass the workspace's screens get.
23const PANE_HEADER_OPTIONS = { ...GLASS_HEADER_OPTIONS, headerShown: false };
24​
25function PaneHeader(props: { readonly store: InspectorBarStore }) {
26 const options = useSyncExternalStore(props.store.subscribe, props.store.getSnapshot);
27 // Its own search identity, so opening the inspector doesn't morph the sidebar's search into it.
28 return (
29 <V5StackHeader
30 options={{ ...PANE_HEADER_OPTIONS, ...options }}
31 canGoBack={false}
32 searchIdentifier="inspector-search"
33 />
34 );
36​
37function PaneScreenContent(props: {
38 readonly pane: InspectorPane;
39 readonly resetKeys: ReadonlyArray<unknown> | undefined;
40 readonly onClose: () => void;
41}) {
42 const gt = useGT();
43 const { store, slot } = useInspectorBarSlot();
44 const content = (
45 <WorkspaceSlotOptionsContext value={slot}>
46 <RenderErrorBoundary
47 resetKeys={props.resetKeys}
48 renderFallback={(fallback) => (
49 <RenderFailureView {...fallback} title={gt("The inspector couldn't be displayed")} />
50 )}
51 >
52 {props.pane.render()}
53 </RenderErrorBoundary>
54 </WorkspaceSlotOptionsContext>
55 );
56 return (
57 <InspectorCloseContext value={props.onClose}>
58 <PaneContext value="inspector">
59 <PaneHeader store={store} />
60 <NativeColumnContent>{content}</NativeColumnContent>
61 </PaneContext>
62 </InspectorCloseContext>
63 );
65​
66/**
67 * The inspector column's panes, each a screen of the column like the detail column's routes.
68 * Only the active one is attached, so UIKit sees one bar, one toolbar and one scroll view.
69 */
70export function InspectorColumnScreens(props: {
71 readonly content: InspectorContent | undefined;
72 readonly onClose: () => void;
73 /** Drawn over the active pane. */
74 readonly overlay?: ReactNode;
75}) {
76 const panes = useMountedInspectorPanes(props.content);
77 return panes.map((pane) => {
78 const active = pane.key === props.content?.active;
79 return (
80 <Split.Inspector
81 key={pane.key}
82 screenKey={`inspector:${pane.key}`}
83 activityMode={active ? "attached" : "detached"}
84 >
85 <PaneScreenContent
86 pane={pane}
87 resetKeys={props.content?.resetKeys}
88 onClose={props.onClose}
89 />
90 {active ? props.overlay : null}
91 </Split.Inspector>
92 );
93 });
95​
96/** The inspector's panes where it has no column: a native stack with the active one attached. */
97export function InspectorPanes(props: {
98 readonly content: InspectorContent | undefined;
99 readonly onClose: () => void;
100}) {
101 const panes = useMountedInspectorPanes(props.content);
102 if (panes.length === 0) return null;
103 return (
104 <Stack.Host>
105 {panes.map((pane) => (
106 <Stack.Screen
107 key={pane.key}
108 screenKey={`inspector:${pane.key}`}
109 activityMode={pane.key === props.content?.active ? "attached" : "detached"}
110 >
111 <PaneScreenContent
112 pane={pane}
113 resetKeys={props.content?.resetKeys}
114 onClose={props.onClose}
115 />
116 </Stack.Screen>
117 ))}
118 </Stack.Host>
119 );