use-show-drawer.tsx


Overview

The use-show-drawer.tsx file contains a set of custom React hooks designed to manage the visibility and interaction logic for various UI drawers (side panels) within a graph-based application interface. These drawers include forms for node editing, debugging panels, chat interfaces, and log sheets.

The primary focus of this file is to encapsulate complex UI state management and user interaction behaviors related to showing and hiding drawers based on user actions, node states, and application conditions. It leverages shared state from a centralized graph store (useGraphStore) and modal state management hooks (useSetModalState) to coordinate visibility and interaction flows.


Exports

1. useShowFormDrawer

Purpose

Manages the visibility and interaction logic of a form drawer that allows users to edit or view details of a selected graph node.

Functionality

Return Values

Returns an object containing:

Parameters

None.

Usage Example

const { formDrawerVisible, showFormDrawer, hideFormDrawer, clickedNode } = useShowFormDrawer();

function onNodeClick(e: React.MouseEvent, nodeId: string) {
  showFormDrawer(e, nodeId);
}

2. useShowSingleDebugDrawer

Purpose

Manages the visibility of a drawer used for debugging a single node or operation. It ensures that the graph is saved before opening the debug drawer.

Functionality

Return Values

Returns an object containing:

Parameters

None.

Usage Example

const { singleDebugDrawerVisible, showSingleDebugDrawer, hideSingleDebugDrawer } = useShowSingleDebugDrawer();

async function onDebugClick() {
  await showSingleDebugDrawer();
}

3. useShowDrawer

Purpose

Provides a comprehensive hook that manages multiple drawer states and interactions:

It also manages the logic to switch between run and chat drawers based on the presence of inputs and handles node click events to show appropriate drawers.

Parameters

{
  drawerVisible: boolean;  // Controls overall drawer visibility
  hideDrawer: () => void;  // Callback to hide the drawer
}

Return Values

An object with the following properties and handlers:

Usage Example

const {
  chatVisible,
  runVisible,
  onPaneClick,
  singleDebugDrawerVisible,
  showSingleDebugDrawer,
  hideSingleDebugDrawer,
  formDrawerVisible,
  showFormDrawer,
  clickedNode,
  onNodeClick,
  hideFormDrawer,
  hideRunOrChatDrawer,
  showChatModal,
} = useShowDrawer({
  drawerVisible: isDrawerOpen,
  hideDrawer: () => setIsDrawerOpen(false),
});

Important Implementation Details


4. useShowLogSheet

Purpose

Manages a drawer/sheet that shows logs related to chat messages.

Parameters

{
  setCurrentMessageId: (id: string) => void; // Setter function for the current message id
}

Functionality

Return Values

Returns:

Usage Example

const { logSheetVisible, showLogSheet, hideLogSheet } = useShowLogSheet({ setCurrentMessageId });

showLogSheet('msg-123');

5. useHideFormSheetOnNodeDeletion

Purpose

Automatically hides the form drawer if the currently selected node is deleted from the graph.

Parameters

{
  hideFormDrawer: () => void;
}

Functionality

Usage Example

useHideFormSheetOnNodeDeletion({ hideFormDrawer });

Important Implementation Details & Algorithms


Interaction with Other Parts of the Application


Visual Diagram

componentDiagram
    direction LR
    subgraph Hooks
      useShowFormDrawer
      useShowSingleDebugDrawer
      useShowDrawer
      useShowLogSheet
      useHideFormSheetOnNodeDeletion
    end

    subgraph Dependencies
      useSetModalState
      useGraphStore
      useCacheChatLog
      useGetBeginNodeDataInputs
      useSaveGraph
      Operator
    end

    useShowFormDrawer --> useSetModalState
    useShowFormDrawer --> useGraphStore

    useShowSingleDebugDrawer --> useSetModalState
    useShowSingleDebugDrawer --> useSaveGraph

    useShowDrawer --> useSetModalState
    useShowDrawer --> useShowSingleDebugDrawer
    useShowDrawer --> useShowFormDrawer
    useShowDrawer --> useGetBeginNodeDataInputs

    useShowLogSheet --> useSetModalState
    useShowLogSheet --> useCacheChatLog

    useHideFormSheetOnNodeDeletion --> useGraphStore
    useHideFormSheetOnNodeDeletion --> useShowFormDrawer

    Operator --> useShowFormDrawer
    Operator --> useShowDrawer

Summary

The use-show-drawer.tsx file is a critical piece of UI state management for an interactive graph application, providing modular hooks to control the visibility of various drawers related to node editing, debugging, chat, and logging. It integrates tightly with the graph state store and modal visibility hooks, offering a clean and maintainable approach to complex UI workflows driven by node interactions.