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