import React, { createContext, useContext, type ReactNode } from "react"; import invariant from "tiny-invariant"; import type { WorkspaceTabTarget } from "@/workspace-tabs/model"; import type { WorkspaceFileOpenRequest } from "@/workspace/file-open"; export interface PaneContextValue { serverId: string; workspaceId: string; tabId: string; target: WorkspaceTabTarget; fileNavigationRevision?: number; openTab: (target: WorkspaceTabTarget) => void; closeCurrentTab: () => void; retargetCurrentTab: (target: WorkspaceTabTarget) => void; openFileInWorkspace: (request: WorkspaceFileOpenRequest) => void; openImportSheet: () => void; } export interface PaneFocusContextValue { isWorkspaceFocused: boolean; isPaneFocused: boolean; isInteractive: boolean; focusPane: () => void; } const PaneContext = createContext(null); const PaneFocusContext = createContext(null); const noopFocusPane = () => {}; export function createPaneFocusContextValue(input: { isWorkspaceFocused: boolean; isPaneFocused: boolean; onFocusPane?: () => void; }): PaneFocusContextValue { return { isWorkspaceFocused: input.isWorkspaceFocused, isPaneFocused: input.isPaneFocused, isInteractive: input.isWorkspaceFocused && input.isPaneFocused, focusPane: input.onFocusPane ?? noopFocusPane, }; } export function PaneProvider({ value, children, }: { value: PaneContextValue; children: ReactNode; }) { return {children}; } export function PaneFocusProvider({ value, children, }: { value: PaneFocusContextValue; children: ReactNode; }) { return {children}; } export function usePaneContext(): PaneContextValue { const value = useContext(PaneContext); invariant(value, "PaneContext is required"); return value; } export function usePaneFocus(): PaneFocusContextValue { const value = useContext(PaneFocusContext); invariant(value, "PaneFocusContext is required"); return value; }