use-watch-change.ts

Overview

The use-watch-change.ts file provides two custom React hooks, useWatchFormChange and useWatchNameFormChange, designed to integrate React Hook Form's useWatch functionality with a centralized state management store (likely Zustand, given the naming convention and usage pattern). These hooks listen for changes in form data and synchronize the updates with a graph-related store that manages node data on a canvas or graphical interface.

This file plays a crucial role in keeping the UI state in sync with user input by automatically updating the graph state whenever the form values or specific form fields change. It is primarily used to reflect real-time changes in form fields directly onto nodes in a graphical editor or canvas.


Detailed Description of Functions

1. useWatchFormChange

Purpose

Synchronizes the entire form's current values with the graph store node identified by id. It listens for any changes in the form's data and pushes those changes to the centralized graph store.

Signature

function useWatchFormChange(id?: string, form?: UseFormReturn<any>): void

Parameters

Behavior

Returns

Usage Example

const form = useForm({ defaultValues: { label: '', description: '' } });
const nodeId = 'node-123';

useWatchFormChange(nodeId, form);
// When form values change, the node with id 'node-123' will be updated with the new form values.

2. useWatchNameFormChange

Purpose

Specifically watches the name property of a form and updates the name of a node in the graph store when it changes.

Signature

function useWatchNameFormChange(id?: string, form?: UseFormReturn<any>): void

Parameters

Behavior

Returns

Usage Example

const form = useForm({ defaultValues: { name: 'Initial Node Name' } });
const nodeId = 'node-123';

useWatchNameFormChange(nodeId, form);
// When the 'name' field changes, the node name in the graph store will be updated accordingly.

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Function Flowchart

flowchart TD
    A[useWatchFormChange] --> B[useWatch(form.control)]
    A --> C[useEffect triggered on id or values change]
    C --> D[Get latest form values with form.getValues()]
    D --> E[updateNodeForm(id, values) via useGraphStore]

    F[useWatchNameFormChange] --> G[useWatch(form.control)]
    F --> H[useEffect triggered on id or values change]
    H --> I[updateNodeName(id, values.name) via useGraphStore]

Summary

The use-watch-change.ts file provides two custom hooks that bridge React Hook Form's reactive form state management with a graph store managing node data. They enable real-time synchronization of form inputs with graph nodes, keeping the UI consistent and responsive to user changes. These hooks facilitate modular, clean, and reactive updates in a graphical editor context.

This file is a utility layer that simplifies form-to-graph synchronization and ensures that updates to form fields propagate immediately to the underlying graph data model.