use-watch-change.ts


Overview

The use-watch-change.ts file defines a custom React hook, useWatchFormChange, designed to synchronize form state changes with a graphical node-based data store. It leverages React Hook Form's useWatch to observe form value changes and updates the corresponding node's data in a centralized graph store. This ensures that any modifications made in a form UI are immediately reflected in the application's graph data model.

This hook is primarily used in contexts where users interact with form inputs tied to graph nodes (such as agents and their tools), and there is a need to keep the canvas or visual graph representation consistent with form state changes.


Detailed Explanation

Imports


Function: useWatchFormChange

export function useWatchFormChange(form?: UseFormReturn<any>)

Purpose

A custom React hook that watches form value changes and updates the corresponding graph node's form data in the global state store.

Parameters

Behavior and Implementation Details

  1. Watching form values:

    The hook uses useWatch with the form's control to subscribe to form field changes and get the current form values reactively.

  2. Accessing graph store state and actions:

    Using the useGraphStore hook, it extracts:

    • clickedToolId: the currently selected tool's identifier.

    • clickedNodeId: the currently selected node's identifier.

    • findUpstreamNodeById(id): a function to retrieve a node upstream in the graph by its ID.

    • updateNodeForm(id, values): a function to update the form data of a node in the graph.

  3. Effect hook for synchronization:

    Inside a useEffect, the hook checks when:

    • The form is dirty (form?.formState.isDirty): meaning the form has unsaved changes.

    • The graph store update function or watched values change.

    If the form is dirty and the upstream agent node exists, it proceeds to:

    • Retrieve the agent node via findUpstreamNodeById(clickedNodeId).

    • Extract the current tools from the agent node using getAgentNodeTools.

    • Get the latest form values using form.getValues().

    • Update the specific tool in the tools array that matches clickedToolId by replacing its params with the new form values.

    • Construct the new node form data with the updated tools.

    • Call updateNodeForm to update the node's form data in the store.

Return Value

Usage Example

import { useForm } from 'react-hook-form';
import { useWatchFormChange } from './use-watch-change';

function AgentToolForm() {
  const form = useForm({ defaultValues: {/*...*/} });
  
  // Synchronize form changes to graph store
  useWatchFormChange(form);

  // Render form fields...
  return (
    <form>
      {/* form inputs bound to form */}
    </form>
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram

classDiagram
    class useWatchFormChange {
        +form?: UseFormReturn<any>
        -values: any
        +useEffect()
    }

    class useGraphStore {
        +clickedToolId: string
        +clickedNodeId: string
        +findUpstreamNodeById(id: string): Node | undefined
        +updateNodeForm(id: string, values: any): void
    }

    class getAgentNodeTools {
        +getAgentNodeTools(node: Node): Tool[]
    }

    useWatchFormChange --> useGraphStore : reads/writes state
    useWatchFormChange --> getAgentNodeTools : extracts tools
    useWatchFormChange --> useEffect : sync logic

Summary

The use-watch-change.ts file provides a specialized React hook, useWatchFormChange, that tightly couples form input changes to updates in a graph node's form data within a centralized store. It bridges React Hook Form's reactive form capabilities with a graph-based data model, making it crucial for applications involving node-tool editing on a canvas or graph interface.

This hook ensures that user modifications in forms are promptly and accurately reflected in the graph data structure, fostering consistency between UI and backend state. The design favors immutability, selective updates, and integration with global graph state, making it a key utility for managing form-driven node configurations.