Open History to see the last commit
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 );
35}
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 );
64}
65
66/**
67 * The inspector column's panes, each a screen of its stack 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 * The inspector stays mounted without panes: Split.Host recreates its columns when it appears.
70 */
71export function InspectorColumnScreens(props: {
72 readonly content: InspectorContent | undefined;
73 readonly onClose: () => void;
74 /** Drawn over the active pane. */
75 readonly overlay?: ReactNode;
76}) {
77 const panes = useMountedInspectorPanes(props.content);
78 return (
79 <Split.Inspector>
80 <Stack.Host>
81 {panes.map((pane) => {
82 const active = pane.key === props.content?.active;
83 return (
84 <Stack.Screen
85 key={pane.key}
86 screenKey={`inspector:${pane.key}`}
87 activityMode={active ? "attached" : "detached"}
88 >
89 <PaneScreenContent
90 pane={pane}
91 resetKeys={props.content?.resetKeys}
92 onClose={props.onClose}
93 />
94 {active ? props.overlay : null}
95 </Stack.Screen>
96 );
97 })}
98 </Stack.Host>
99 </Split.Inspector>
100 );
101}
102
103/** The inspector's panes where it has no column: a native stack with the active one attached. */
104export function InspectorPanes(props: {
105 readonly content: InspectorContent | undefined;
106 readonly onClose: () => void;
107}) {
108 const panes = useMountedInspectorPanes(props.content);
109 if (panes.length === 0) return null;
110 return (
111 <Stack.Host>
112 {panes.map((pane) => (
113 <Stack.Screen
114 key={pane.key}
115 screenKey={`inspector:${pane.key}`}
116 activityMode={pane.key === props.content?.active ? "attached" : "detached"}
117 >
118 <PaneScreenContent
119 pane={pane}
120 resetKeys={props.content?.resetKeys}
121 onClose={props.onClose}
122 />
123 </Stack.Screen>
124 ))}
125 </Stack.Host>
126 );
127}