1import { createContext, useMemo, useState } from "react";
2
3/** Closes the inspector from its bar: hides the column, or dismisses the sheet. */
4export const InspectorCloseContext = createContext<(() => void) | null>(null);
5
6const NO_ROUTES: ReadonlyMap<string, object> = new Map();
7
8/** The options a pane's AppBar sets. External so a write re-renders the bar, not the pane. */
9function createInspectorBarStore() {
10 let snapshot: object | undefined;
11 const listeners = new Set<() => void>();
12 return {
13 set(options: object) {
14 snapshot = { ...snapshot, ...options };
15 for (const listener of listeners) listener();
16 },
17 subscribe(listener: () => void) {
18 listeners.add(listener);
19 return () => {
20 listeners.delete(listener);
21 };
22 },
23 getSnapshot: () => snapshot,
24 };
25}
26
27export type InspectorBarStore = ReturnType<typeof createInspectorBarStore>;
28
29/** Routes a pane's AppBar options into its own bar instead of the thread route's. */
30export function useInspectorBarSlot() {
31 const [store] = useState(createInspectorBarStore);
32 const slot = useMemo(
33 () => ({
34 byRoute: NO_ROUTES,
35 set: (_routeKey: string, options: object) => store.set(options),
36 }),
37 [store],
38 );
39 return { store, slot };
40}