use-watch-change.ts

Overview

The use-watch-change.ts file provides React hooks and utility functions designed to facilitate reactive form state management within a graphical node-based editor, likely a workflow or pipeline builder. It primarily integrates react-hook-form's form watching capabilities with a global state management store (useGraphStore) to keep the UI canvas and form data synchronized.

The file handles:

This design ensures that when users modify form fields related to node configurations—such as changing parameters, outputs, or the scripting language—the visual representation and underlying data model remain consistent and up-to-date.


Detailed Explanations

Utility Functions

convertToObject

function convertToObject(list: FormSchemaType['arguments'] = []): Record<string, string>

convertOutputsToObject

function convertOutputsToObject({ lang, outputs }: FormSchemaType): ICodeForm['outputs']

React Hooks

useWatchFormChange

export function useWatchFormChange(
  id?: string,
  form?: UseFormReturn<FormSchemaType>,
): void
const form = useForm<FormSchemaType>();
useWatchFormChange(nodeId, form);

This hook ensures that any change in the form fields immediately reflects on the graphical node's state.


useHandleLanguageChange

export function useHandleLanguageChange(
  id?: string,
  form?: UseFormReturn<FormSchemaType>,
): (lang: string) => void
const form = useForm<FormSchemaType>();
const handleLanguageChange = useHandleLanguageChange(nodeId, form);

// On language selection change:
handleLanguageChange('Python');

This hook abstracts the logic for updating language-dependent form fields and ensures consistency in the node data.


Important Implementation Details and Algorithms


Interaction with Other System Parts


Visual Diagram

classDiagram
    class useWatchFormChange {
        +id?: string
        +form?: UseFormReturn<FormSchemaType>
        +void
    }
    class useHandleLanguageChange {
        +id?: string
        +form?: UseFormReturn<FormSchemaType>
        +handleLanguageChange(lang: string): void
    }
    class convertToObject {
        +list: FormSchemaType['arguments']
        +Record<string, string>
    }
    class convertOutputsToObject {
        +formSchema: FormSchemaType
        +ICodeForm['outputs']
    }
    useWatchFormChange --> useGraphStore : calls updateNodeForm
    useHandleLanguageChange --> useGraphStore : calls updateNodeForm
    useWatchFormChange --> convertToObject : calls
    useWatchFormChange --> convertOutputsToObject : calls
    useHandleLanguageChange --> CodeTemplateStrMap : reads templates

Summary

The use-watch-change.ts file encapsulates core logic for synchronizing form data changes with a global graph state in a node-based editor environment. It provides:

This modular approach improves maintainability, ensures data consistency between the form UI and graphical nodes, and cleanly separates concerns between form state management and graph data synchronization.