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

Returns

An object with the following structure:

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

Returns

An array of objects, each with:

Implementation Details

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


Interaction With Other Parts of the Application


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.