use-values.ts
Overview
The use-values.ts file provides a utility React hook useValues designed to extract and transform form-related data from a node object, specifically of type RAGFlowNodeType. It processes the node's form data, converts output objects into an array format suitable for UI or further computation, and supplies a default initial structure when no form data exists.
This file is primarily used within React components or hooks that need to access and manipulate node form data in a standardized and memoized way, optimizing rendering performance by using React's useMemo.
Exports
useValues(node?: RAGFlowNodeType): { ... }
Description
A React hook that returns processed form data from a given node. It memoizes the result so that it only recomputes when the form data changes.
The returned object contains all properties from the node's form data, but with the outputs property transformed from an object map into an array of output descriptors.
If the node or its form data is empty, it returns a predefined initial state with empty outputs.
Parameters
node(optional): An object of typeRAGFlowNodeTypewhich is expected to have adata.formproperty containing form-related data.
Returns
An object with the following structure:
All key-value pairs from
node.data.form(if present).outputs: An array where each element represents an output with the following properties:name(string): The key from the original outputs object.ref: Therefproperty from the original output value.type: Thetypeproperty from the original output value.
If node or node.data.form is empty, returns a copy of initialIterationValues with an empty outputs array.
Usage Example
import { useValues } from './use-values';
import { RAGFlowNodeType } from '@/interfaces/database/flow';
const MyComponent = ({ node }: { node?: RAGFlowNodeType }) => {
const formValues = useValues(node);
return (
<div>
<h3>Form Outputs</h3>
<ul>
{formValues.outputs.map(output => (
<li key={output.name}>
{output.name} - Type: {output.type}
</li>
))}
</ul>
</div>
);
};
Internal Functions
convertToArray(outputObject: OutputObject): Array<{ name: string; ref: any; type: any }>
Description
Transforms an OutputObject (an object with keys as output names and values containing ref and type) into an array of output descriptors. Each array element contains the output name along with its ref and type.
Parameters
outputObject: An object where each key corresponds to an output name, and the value has at leastrefandtypeproperties.
Returns
An array of objects, each with:
name: The output key from the input object.ref: Therefproperty of the output.type: Thetypeproperty of the output.
Implementation Details
Uses
Object.entriesto iterate over the key-value pairs.Maps each pair into a new object with the desired structure.
Usage Example
const outputs = {
output1: { ref: 'ref1', type: 'string' },
output2: { ref: 'ref2', type: 'number' },
};
const arrayOutputs = convertToArray(outputs);
/*
[
{ name: 'output1', ref: 'ref1', type: 'string' },
{ name: 'output2', ref: 'ref2', type: 'number' },
]
*/
Important Implementation Details
The hook uses
useMemofrom React to memoize the processed values, ensuring that the transformation only recalculates when thenode.data.formchanges, improving performance by avoiding unnecessary recalculations on re-renders.The hook depends on lodash's
isEmptyto check if the form data is empty or undefined.The
initialIterationValuesconstant provides a fallback structure, ensuring components consuminguseValuesalways receive a consistent shape of data.
Interaction With Other Parts of the Application
RAGFlowNodeType: This type is imported from the database flow interfaces and represents the structure of a node in the flow system, particularly containing form data.initialIterationValues: Imported from a constants file, it provides default initial values for iteration.OutputObject: An interface defining the shape of the output data expected inform.outputs.Used inside React components or other hooks that need to read or manipulate node form values.
Supports UI rendering logic that depends on dynamic output fields by converting object maps into arrays, which are easier to iterate over in React JSX.
File Structure Flowchart
flowchart TD
A[useValues(node?)] --> B{Is node.data.form empty?}
B -- Yes --> C[Return initialIterationValues + outputs: []]
B -- No --> D[Convert formData.outputs using convertToArray]
D --> E[Return formData with outputs as array]
subgraph convertToArray(outputObject)
F[Object.entries(outputObject)]
G[Map each entry to {name, ref, type}]
end
D --> F
F --> G
Summary
The use-values.ts file offers a compact, memoized, and reusable React hook for safely extracting and normalizing form data from flow nodes. Its main contribution is transforming output objects into arrays for easier consumption in components, backed with sensible defaults and performance-conscious memoization.
This module is a utility piece in the larger system of flow nodes and form state management, bridging database node data with frontend UI logic in React.