| 1 | import { useLayoutEffect, type RefObject } from "react"; |
| 2 | import { useAppNavigationStore } from "../store/appNavigation"; |
| 3 | |
| 4 | /** Keep the workspace geometry and runtime mounted while isolating its input. */ |
| 5 | export function useManagementWorkspace(workspaceRef: RefObject<HTMLDivElement | null>, active: boolean) { |
| 6 | useLayoutEffect(() => { |
| 7 | if (!active) return; |
| 8 | const workspace = workspaceRef.current; |
| 9 | const previous = useAppNavigationStore.getState().workspaceFocus; |
| 10 | if (workspace) workspace.inert = true; |
| 11 | return () => { |
| 12 | if (workspace) workspace.inert = false; |
| 13 | queueMicrotask(() => { |
| 14 | if (useAppNavigationStore.getState().page.kind === "workspace" && previous?.isConnected && !previous.closest("[inert]")) previous.focus({ preventScroll: true }); |
| 15 | }); |
| 16 | }; |
| 17 | }, [workspaceRef, active]); |
| 18 | } |
| 19 |