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 <PaneHeader store={store} />
51 <NativeColumnContent>{content}</NativeColumnContent>
52 </PaneContext>
53 </InspectorCloseContext>
54 );
55}
56
57/**
58 * The inspector column's panes, each a screen of the column like the detail column's routes.
59 * Only the active one is attached, so UIKit sees one bar, one toolbar and one scroll view.
60 */
61export function InspectorColumnScreens(props: {
62 readonly content: InspectorContent | undefined;
63 readonly onClose: () => void;
64 /** Drawn over the active pane. */
65 readonly overlay?: ReactNode;
66}) {
67 const panes = useMountedInspectorPanes(props.content);
68 return panes.map((pane) => {
69 const active = pane.key === props.content?.active;
70 return (
71 <Split.Inspector
72 key={pane.key}
73 screenKey={`inspector:${pane.key}`}
74 activityMode={active ? "attached" : "detached"}
75 >
76 <PaneScreenContent
77 pane={pane}
78 resetKeys={props.content?.resetKeys}
79 onClose={props.onClose}
80 />
81 {active ? props.overlay : null}
82 </Split.Inspector>
83 );
84 });
85}
86
87/** The inspector's panes where it has no column: a native stack with the active one attached. */
88export function InspectorPanes(props: {
89 readonly content: InspectorContent | undefined;
90 readonly onClose: () => void;
91}) {
92 const panes = useMountedInspectorPanes(props.content);
93 if (panes.length === 0) return null;
94 return (
95 <Stack.Host>
96 {panes.map((pane) => (
97 <Stack.Screen
98 key={pane.key}
99 screenKey={`inspector:${pane.key}`}
100 activityMode={pane.key === props.content?.active ? "attached" : "detached"}
101 >
102 <PaneScreenContent
103 pane={pane}
104 resetKeys={props.content?.resetKeys}
105 onClose={props.onClose}
106 />
107 </Stack.Screen>
108 ))}
109 </Stack.Host>
110 );
111}