use-watch-change.ts

Overview

The use-watch-change.ts file defines a custom React hook named useWatchFormChange. This hook integrates react-hook-form's form state watching mechanism with a global state management store (likely Zustand) to synchronize form data changes with a graphical canvas or node-based UI representation.

In essence, this hook listens for changes in a form and, when detected, updates a corresponding node's data in the global graph store. It converts certain form fields to a specific format before updating, ensuring data consistency between the form UI and the underlying graph model.


Detailed Explanation

useWatchFormChange

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

Purpose

This custom hook watches the form data using react-hook-form's useWatch and synchronizes updates to a node identified by id in the application's graph store.

Parameters

Behavior

Return Value

Usage Example

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

function MyNodeForm({ nodeId }) {
  const form = useForm({
    defaultValues: {
      include_domains: [],
      exclude_domains: [],
      // ... other fields
    },
  });

  useWatchFormChange(nodeId, form);

  return (
    <form>
      {/* form inputs bound to form control */}
    </form>
  );
}

Implementation Details


Interaction with Other Parts of the System


Mermaid Class Diagram

classDiagram
    class useWatchFormChange {
        +useWatchFormChange(id?: string, form?: UseFormReturn<any>): void
    }

    class react-hook-form {
        +useWatch(options: { control }): any
        +UseFormReturn
    }

    class useGraphStore {
        +updateNodeForm(id: string, data: any): void
    }

    class convertToStringArray {
        +convertToStringArray(input: any): string[]
    }

    useWatchFormChange ..> react-hook-form : uses
    useWatchFormChange ..> useGraphStore : calls
    useWatchFormChange ..> convertToStringArray : calls

Summary

The use-watch-change.ts file provides a focused integration hook that bridges form state management and a global graph data store. It abstracts the synchronization logic between form inputs and the graph's node data, ensuring form changes are immediately reflected in the application's graph model with proper data formatting.

This hook is a key piece in maintaining consistency between the UI layer (forms) and the underlying graph data, facilitating reactive and user-driven updates in complex node-based applications such as flow editors, diagram tools, or data visualization platforms.