use-watch-change.ts

Overview

The use-watch-change.ts file provides React hooks and utility functions designed to synchronize and manage form state changes within a graphical node-based editor environment. It primarily integrates with react-hook-form to watch form inputs and updates a graph store representing nodes on a canvas. The file also manages language-dependent form adjustments, such as changing code templates and output formats depending on the selected programming language.

This file plays a critical role in keeping the form UI and the underlying node data model synchronized, enabling a seamless and reactive user experience in applications involving dynamic code/script editing and multi-language support.


Detailed Documentation

Utility Functions

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

Converts an array of argument objects to a key-value map where keys are argument names and values are their types.


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

Transforms the form's outputs property into a normalized object format depending on the programming language:


Hooks

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

A React hook that watches changes in a form's values and synchronizes those changes back to the graph node store.


useHandleLanguageChange(id?: string, form?: UseFormReturn<FormSchemaType>): (lang: string) => void

A hook that returns a callback function to handle programming language changes within the form.


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>
        +(lang: string) => void
    }
    class convertToObject {
        +list: FormSchemaType['arguments'][]
        +Record<string, string>
    }
    class convertOutputsToObject {
        +params: { lang: string; outputs: FormSchemaType['outputs'] }
        +ICodeForm['outputs']
    }
    useWatchFormChange --> useGraphStore : updateNodeForm()
    useHandleLanguageChange --> useGraphStore : updateNodeForm()
    useHandleLanguageChange --> CodeTemplateStrMap : get template string
    useWatchFormChange --> convertToObject
    useWatchFormChange --> convertOutputsToObject

Summary

The use-watch-change.ts file is a utility and hook provider that enables reactive synchronization between form inputs and a graph-based node system in a React application. It abstracts the complexity of data normalization, language-specific adjustments, and state propagation to ensure consistent, real-time updates within a code editing or node programming interface. Its reliance on react-hook-form and a centralized graph store makes it a critical piece in maintaining the integrity and responsiveness of the application's node editing workflow.