use-watch-form-change.ts
Overview
use-watch-form-change.ts provides a custom React hook, useWatchFormChange, designed to synchronize changes in a form managed by react-hook-form with an external state store (useGraphStore). This hook watches form values and, when the form is dirty (i.e., has unsaved changes), it updates a corresponding node's form data in the global graph state. The main use case is to keep a visual canvas or graph representation in sync with user inputs on a form in real-time.
Detailed Explanation
useWatchFormChange
export function useWatchFormChange(id?: string, form?: UseFormReturn<any>)
Purpose
A React hook that watches changes in a form and updates the corresponding node's form data in the global graph store whenever the form becomes dirty.
Parameters
id?: string
The identifier of the node whose form data should be updated. Optional; if not provided, no update will occur.form?: UseFormReturn<any>
The form object returned byreact-hook-form'suseFormhook. This contains form state, control, and helper methods. Optional; if not provided, the hook will not watch any form.
Returns
This hook does not return any value. It performs subscription and side effects internally.
Usage Example
import { useForm } from 'react-hook-form';
import { useWatchFormChange } from './use-watch-form-change';
function NodeForm({ nodeId }) {
const form = useForm({
defaultValues: {
delimiters: '',
method: 'Split', // example default
},
});
// Synchronize form changes with node state
useWatchFormChange(nodeId, form);
return (
<form>
{/* form inputs here */}
</form>
);
}
Implementation Details
Form Watching:
The hook usesuseWatchfromreact-hook-formto subscribe to form value changes viaform.control.Effect Hook:
TheuseEffecthook triggers whenever the form's dirty state (form.formState.isDirty), the nodeid, or the watchedvalueschange.Data Synchronization:
When the form is dirty and anidis provided, the hook fetches the current form values. If themethodfield equalsStringTransformMethod.Mergeanddelimitersis defined, it transformsdelimitersinto an array containing a single element (the existing delimiters string). This likely aligns with expected data structure in the downstream graph store.State Update:
The hook then callsupdateNodeForm(id, nextValues), a method from the global graph state (useGraphStore), to update the node's form data.
Interaction with Other System Parts
react-hook-formLibrary:
Provides the form state management and theuseWatchhook to listen for form changes.useGraphStore(Zustand or similar store):
This is the global state store managing the graph/canvas nodes. The hook retrieves theupdateNodeFormmethod from this store to update node data.StringTransformMethodEnum/Constant:
Imported from a constants file, this controls how string transformation methods are handled, specifically checking for the.Mergemethod.Graph or Canvas Component:
The updated form values are synchronized to a visual node on a canvas or graph UI, ensuring the UI reflects the latest form inputs.
Visual Diagram
flowchart TD
A[useWatchFormChange Hook] --> B[useWatch -> Watches form values]
A --> C[useEffect -> on form dirty & id change]
C --> D{Check conditions}
D -- isDirty true & id exists --> E[Get current form values]
E --> F{Check method}
F -- method === Merge --> G[Transform delimiters into array]
F -- otherwise --> H[Use values as-is]
G & H --> I[Call updateNodeForm(id, nextValues)]
I --> J[Update global graph store]
J --> K[Graph / Canvas nodes update]
Summary
Purpose: Synchronize form data changes to global graph node state in real time.
Key Concepts: React hook, form watching, effect synchronization, global state update.
Important Logic: Special handling of
delimiterswhenmethodisMerge.Integration: Connects
react-hook-formwith a global graph store to keep UI and form data consistent.
This hook is a critical piece for maintaining bi-directional synchronization between user input forms and a visual graph or canvas representation in the application.