use-show-drawer.tsx


Overview

use-show-drawer.tsx is a React hooks utility module designed to manage the visibility and interactions of multiple drawer components within a graph-based user interface. These drawers include form drawers, debug drawers, run/chat drawers, and log sheets. The file encapsulates logic for showing/hiding these UI elements based on user actions such as node clicks, pane clicks, or graph state changes.

The hooks in this file centralize modal state management, node selection, and interaction handling, serving as a bridge between the graph store (state management for nodes and tools) and UI components responsible for displaying modals/drawers.


Exports and Their Functionalities

1. useShowFormDrawer

Purpose:
Manages the form drawer visibility and selected node/tool state when a user interacts with a node.

Functionality:

Returns:

Usage Example:

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

<button onClick={(e) => showFormDrawer(e, nodeId)}>Open Form</button>
{formDrawerVisible && <FormDrawer node={clickedNode} onClose={hideFormDrawer} />}

2. useShowSingleDebugDrawer

Purpose:
Manages the visibility of a "single debug" drawer and ensures the graph is saved before showing the drawer.

Functionality:

Returns:

Usage Example:

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

<button onClick={showSingleDebugDrawer}>Debug Node</button>
{singleDebugDrawerVisible && <DebugDrawer onClose={hideSingleDebugDrawer} />}

3. useShowDrawer

Purpose:
Coordinates the display logic of multiple drawers including run modal, chat modal, single debug drawer, and form drawer based on application state and user interactions.

Parameters:

Functionality:

Returns:

Usage Example:

const { 
  chatVisible, runVisible, onPaneClick, onNodeClick,
  formDrawerVisible, showFormDrawer, hideFormDrawer,
  singleDebugDrawerVisible, showSingleDebugDrawer, hideSingleDebugDrawer,
  hideRunOrChatDrawer
} = useShowDrawer({ drawerVisible, hideDrawer });

// Use these handlers in the graph component and render drawers conditionally

4. useShowLogSheet

Purpose:
Manages the visibility of a log sheet drawer that displays chat logs based on a selected message ID.

Parameters:

Functionality:

Returns:

Usage Example:

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

<button onClick={() => showLogSheet('msg123')}>Show Log</button>
{logSheetVisible && <LogSheet messageId={currentMessageId} onClose={hideLogSheet} />}

5. useHideFormSheetOnNodeDeletion

Purpose:
Automatically hides the form drawer if the currently clicked node is deleted from the graph.

Parameters:

Functionality:

Returns:

Usage Example:

useHideFormSheetOnNodeDeletion({ hideFormDrawer });

Important Implementation Details


Interaction With Other Parts of the System

This file thus acts as a coordination layer between graph state, UI modal states, and domain-specific logic for displaying various drawers in response to user interactions.


Visual Diagram

componentDiagram
    component useShowFormDrawer {
        +formDrawerVisible: boolean
        +hideFormDrawer(): void
        +showFormDrawer(e: MouseEvent, nodeId: string): void
        +clickedNode: Node | undefined
    }
    component useShowSingleDebugDrawer {
        +singleDebugDrawerVisible: boolean
        +showSingleDebugDrawer(): Promise<void>
        +hideSingleDebugDrawer(): void
    }
    component useShowDrawer {
        +chatVisible: boolean
        +runVisible: boolean
        +onPaneClick(): void
        +onNodeClick(e, node): void
        +formDrawerVisible: boolean
        +showFormDrawer(e, nodeId): void
        +hideFormDrawer(): void
        +singleDebugDrawerVisible: boolean
        +showSingleDebugDrawer(): Promise<void>
        +hideSingleDebugDrawer(): void
        +hideRunOrChatDrawer(): void
        +showChatModal(): void
    }
    component useShowLogSheet {
        +logSheetVisible: boolean
        +showLogSheet(messageId: string): void
        +hideLogSheet(): void
    }
    component useHideFormSheetOnNodeDeletion

    useShowDrawer --> useShowFormDrawer : uses
    useShowDrawer --> useShowSingleDebugDrawer : uses
    useShowDrawer --> useSetModalState : uses (for run/chat modals)
    useShowSingleDebugDrawer --> useSaveGraph : uses
    useShowLogSheet --> useSetModalState : uses
    useHideFormSheetOnNodeDeletion --> useGraphStore : observes

Summary

This file provides a cohesive set of React hooks that manage complex UI drawer visibility and behavior for a graph-based interface, abstracting event handling, modal state, and graph state synchronization. It helps keep the UI responsive to user interactions while ensuring data consistency (e.g., saving graph before debug) and improves separation of concerns by encapsulating modal and node interaction logic in reusable hooks.