use-save-graph.ts


Overview

use-save-graph.ts is a React hook utility file designed to manage the saving and state synchronization of a graph-based flow within a React application. It provides hooks that encapsulate the logic for persisting the flow graph data, handling flow resets before debugging sessions, and monitoring changes in the agent's state within the graph editor.

This file interacts primarily with flow-related APIs (fetching, setting, resetting flows), a graph state store, and utility hooks to build DSL (Domain Specific Language) data representation of the flow graph. It is intended to be used within functional React components that handle flow editing, saving, and debugging workflows.


Exports and Their Functionalities

1. useSaveGraph

Purpose

A hook to save the current state of the graph flow to a backend or persistent storage by dispatching a "setFlow" API call with the latest DSL representation of the graph.

Usage

const { saveGraph, loading } = useSaveGraph();

await saveGraph(currentNodes);

Details


2. useSaveGraphBeforeOpeningDebugDrawer

Purpose

A hook to save the graph and reset the flow's state before opening a debugging drawer UI. This ensures that the flow is saved and reset for a fresh debugging session.

Usage

const { handleRun, loading } = useSaveGraphBeforeOpeningDebugDrawer(() => {
  // logic to show debug drawer
});

await handleRun(nextNodes);

Details


3. useWatchAgentChange

Purpose

Monitors changes to the graph's nodes and edges and tracks the last save time of the flow. It is designed to detect and reflect updates in the graph state, primarily used to display or log the last saved time.

Usage

const lastSaveTime = useWatchAgentChange(chatDrawerVisible);

Details


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[useSaveGraph] -->|calls| B[useFetchFlow]
    A -->|calls| C[useSetFlow]
    A -->|calls| D[useBuildDslData]
    A -->|uses| E[useParams]

    F[useSaveGraphBeforeOpeningDebugDrawer] -->|uses| A
    F -->|calls| G[useResetFlow]
    F -->|invokes| H[show() callback]

    I[useWatchAgentChange] -->|reads| J[useGraphStore.nodes & edges]
    I -->|reads| B
    I -->|uses| K[useDebounceEffect]
    I --> L[useState(time)]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style F fill:#bbf,stroke:#333,stroke-width:1px
    style I fill:#bfb,stroke:#333,stroke-width:1px

Summary

use-save-graph.ts is a utility file that provides React hooks to manage graph flow persistence and debugging state in a React application. It encapsulates asynchronous save operations, flow resetting before debug sessions, and time tracking for changes in the graph. The design leverages React hooks, debounced effects, and centralized state management to optimize performance and ensure data consistency. This file is a critical part of ensuring that graph editing and debugging workflows are smooth, reliable, and integrated with backend services.