use-watch-change.ts
Overview
The use-watch-change.ts file provides utilities and React hooks to monitor changes in form data managed by react-hook-form and synchronize those changes with a centralized graph store. This setup is primarily used in contexts where form state changes need to be propagated to update nodes in a graph-based data structure or UI component, such as a visual editor or workflow builder.
Key functionalities include:
Converting an array of input configurations (of type
BeginQuery[]) into a keyed object for easier manipulation and storage.A React hook
useWatchFormChangethat watches form state changes and triggers updates to graph nodes via a state management store (useGraphStore).
Detailed Explanations
Imports
omitfromlodash: Utility to create a copy of an object excluding specified keys.useEffectfromreact: React hook to perform side effects in function components.UseFormReturn,useWatchfromreact-hook-form: Types and hooks for form state management.BeginQueryinterface: Represents the shape of input query objects.useGraphStore: Custom hook to access a centralized graph store that manages nodes and their forms.
Function: transferInputsArrayToObject
export function transferInputsArrayToObject(inputs: BeginQuery[] = []): Record<string, Omit<BeginQuery, 'key'>>
Purpose
Transforms an array of BeginQuery objects into an object where each entry is keyed by the key property of the BeginQuery object, and the value is the rest of the BeginQuery fields excluding the key.
Parameters
inputs(optional): An array ofBeginQueryobjects. Defaults to an empty array if not provided.
Returns
A record (object) where each key is a
string(thekeyproperty from eachBeginQuery), and the value is the correspondingBeginQueryobject without thekeyproperty.
Usage Example
const inputsArray = [
{ key: 'input1', name: 'Input 1', type: 'string' },
{ key: 'input2', name: 'Input 2', type: 'number' }
];
const inputsObject = transferInputsArrayToObject(inputsArray);
/* Result:
{
input1: { name: 'Input 1', type: 'string' },
input2: { name: 'Input 2', type: 'number' }
}
*/
Implementation Details
Uses
Array.reduceto iterate over each element in the array.For each element, it creates a property on the accumulator object keyed by
cur.key.Uses
lodash.omitto exclude thekeyfield from each object in the result.
Hook: useWatchFormChange
export function useWatchFormChange(id?: string, form?: UseFormReturn): void
Purpose
A custom React hook that listens for changes to a form's inputs and updates the corresponding node in the graph store with the latest form values.
Parameters
id(optional): The identifier string for the node in the graph store that should be updated.form(optional): TheUseFormReturnobject fromreact-hook-formthat provides access to form state and methods.
Returns
void: This hook does not return a value; it performs side effects.
Usage Example
function MyComponent({ nodeId }: { nodeId: string }) {
const form = useForm<SomeFormType>();
// Watch changes and update graph node
useWatchFormChange(nodeId, form);
return (
<form>
{/* form inputs */}
</form>
);
}
Implementation Details
Uses
useWatchfromreact-hook-formto subscribe to form value changes.Extracts the
updateNodeFormmethod fromuseGraphStoreto update the graph node.In a
useEffecthook, it triggers whenever the form's dirty state,id, or watched values change.Inside the effect:
Retrieves current form values.
Converts the
inputsarray in the form values to an object usingtransferInputsArrayToObject.Calls
updateNodeFormwith the nodeidand the transformed form values.
Ensures updates only occur if a valid
idis provided.
Important Implementation Details and Algorithms
State Synchronization: The hook uses
useWatchfor efficient subscription to form value changes without redundant renders.Data Transformation: The conversion of the
inputsarray to an object keyed bykeyensures consistent and optimized updates to the graph store, which likely expects inputs in this format.Store Interaction:
useGraphStoreabstracts the state management (likely using Zustand or similar) for nodes in a graph structure, enabling modular updates without prop drilling.Dependency Awareness: The effect depends on
form.formState.isDirtyto avoid unnecessary updates until the form has changed.
Interactions with Other Parts of the System
React Hook Form Integration: Integrates tightly with
react-hook-formfor form state management.Graph Store (
useGraphStore): Updates nodes in the graph, suggesting this file is part of a larger system managing graph-based data or UI components (e.g., node editors, flowcharts).BeginQuery Interface: The shape of inputs processed here likely originates from other parts of the system defining query parameters or node inputs.
Lodash Utility: Uses
omitto safely exclude keys from objects without mutating originals.
Mermaid Diagram: File Structure and Workflow
flowchart TD
A[transferInputsArrayToObject(inputs: BeginQuery[])]
B[useWatchFormChange(id?: string, form?: UseFormReturn)]
C[useWatch({ control: form?.control })]
D[useEffect(() => { ... })]
E[useGraphStore(state => state.updateNodeForm)]
F[updateNodeForm(id, nextValues)]
G[transferInputsArrayToObject(values.inputs)]
B --> C
B --> D
D -->|on form change| E
D -->|calls| F
D --> G
A -->|used by| G
Diagram Explanation:
The
useWatchFormChangehook usesuseWatchto monitor form changes.On detected changes, the
useEffecttriggers an update.It derives
updateNodeFormfromuseGraphStoreand calls it with the updated form values.The
inputsfield from the form values is transformed usingtransferInputsArrayToObject.transferInputsArrayToObjectis a standalone utility function used within the hook.
Summary
The use-watch-change.ts file is a small yet crucial part of a React + react-hook-form + graph state management system. It provides a utility to convert form input arrays to keyed objects and a hook that watches form state changes to update nodes in a graph store. This promotes a clean, reactive workflow for synchronizing UI form states with underlying graph data structures, enabling dynamic and responsive graph-based applications.
If you need further details on related files or system-wide architecture, please provide additional context or files.