use-watch-change.ts
Overview
The use-watch-change.ts file defines a custom React hook named useWatchFormChange designed to integrate React Hook Form's state watching capabilities with a global state management store (likely Zustand). Its primary purpose is to watch form values for changes and synchronize these changes with a graphical canvas or node-based UI by updating the corresponding node's form data in the global store.
This hook listens to changes in a form's state and, when the form is dirty (i.e., has unsaved changes), it updates the node data in the store. It specifically converts the content field of the form data into a string array before updating, ensuring consistent data formatting for downstream consumers.
Detailed Explanation
Function: useWatchFormChange
function useWatchFormChange(id?: string, form?: UseFormReturn)
Purpose
This hook watches the form state using React Hook Form's useWatch and triggers an update to the global graph store when the form is dirty and an id is provided. It ensures that the form data stays in sync with the node representation in the canvas or graphical interface.
Parameters
id(optional,string):
The unique identifier of the node whose form data should be updated in the global graph store. If noidis provided, the hook does not perform updates.form(optional,UseFormReturn):
The React Hook Form object returned byuseForm(). This includes methods and state necessary to track and retrieve form values.
Returns
This hook does not return any value. It performs side-effects by syncing form state with the global store.
Usage Example
import { useForm } from 'react-hook-form';
import { useWatchFormChange } from './use-watch-change';
function NodeEditor({ nodeId }) {
const form = useForm({ defaultValues: { content: '' } });
// Synchronizes form changes with the graph store node identified by nodeId
useWatchFormChange(nodeId, form);
return (
<form>
<textarea {...form.register('content')} />
{/* other form fields */}
</form>
);
}
Implementation Details
React Hook Form Integration:
UsesuseWatchto observe form values reactively. However, even thoughuseWatchreacts to form field changes, the hook updates the store only when the form is marked as dirty (form.formState.isDirty). This prevents unnecessary updates when no changes have been made.State Synchronization:
When the form is dirty, the current form values are retrieved viaform.getValues(). Thecontentfield is processed byconvertToStringArray, a utility presumably converting multi-line or complex content to an array of strings, ensuring a normalized format.Global Store Update:
The hook callsupdateNodeFormfrom the globaluseGraphStore(likely Zustand or a similar state management library) to update the node data in the central store, enabling other parts of the application (e.g., canvas rendering) to reflect the latest form changes.Dependency Management:
The effect depends onform.formState.isDirty,id,updateNodeForm, andvaluesto trigger updates only when these change, avoiding redundant synchronization.
Interaction with Other Parts of the System
React Hook Form:
This file relies heavily onreact-hook-formfor form state management and reactivity.Global Graph Store (
useGraphStore):
Acts as the interface to update node data globally, presumably affecting other UI components such as a canvas or node graph visualization.Utility Function (
convertToStringArray):
Imported from../../utils, this function helps format thecontentfield before updating the store, ensuring data consistency.Canvas or Node UI Components:
Although not shown here, components responsible for rendering nodes on a canvas likely subscribe touseGraphStoreand react to changes made viaupdateNodeForm.
Mermaid Diagram
flowchart TD
A[useWatchFormChange Hook] --> B[useWatch(form.control)]
A --> C{form.formState.isDirty?}
C -- Yes --> D[form.getValues()]
D --> E[convertToStringArray(values.content)]
E --> F[updateNodeForm(id, updatedValues)]
C -- No --> G[No update]
B --> C
Diagram Explanation:
The hook uses
useWatchto observe form changes.It checks if the form is dirty.
If dirty, it retrieves all form values, converts the
contentfield, and updates the node in the global store.If not dirty, no update occurs.
Summary
use-watch-change.ts provides a focused utility hook that bridges React Hook Form-managed form data with a global graph state, ensuring that UI representations of nodes remain consistent with user input. By leveraging React's hooks and state management patterns, it facilitates efficient and reactive synchronization with minimal boilerplate.
This file is a critical piece in any application with an interactive node or canvas system where form inputs drive the underlying data model dynamically.