refactor: optimize-the-performance-of-var-reference-picker (#5918)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import cn from 'classnames'
|
import cn from 'classnames'
|
||||||
import {
|
import {
|
||||||
@@ -71,7 +71,9 @@ const VarReferencePicker: FC<Props> = ({
|
|||||||
const isChatMode = useIsChatMode()
|
const isChatMode = useIsChatMode()
|
||||||
|
|
||||||
const { getTreeLeafNodes, getBeforeNodesInSameBranch } = useWorkflow()
|
const { getTreeLeafNodes, getBeforeNodesInSameBranch } = useWorkflow()
|
||||||
const availableNodes = passedInAvailableNodes || (onlyLeafNodeVar ? getTreeLeafNodes(nodeId) : getBeforeNodesInSameBranch(nodeId))
|
const availableNodes = useMemo(() => {
|
||||||
|
return passedInAvailableNodes || (onlyLeafNodeVar ? getTreeLeafNodes(nodeId) : getBeforeNodesInSameBranch(nodeId))
|
||||||
|
}, [getBeforeNodesInSameBranch, getTreeLeafNodes, nodeId, onlyLeafNodeVar, passedInAvailableNodes])
|
||||||
const startNode = availableNodes.find((node: any) => {
|
const startNode = availableNodes.find((node: any) => {
|
||||||
return node.data.type === BlockEnum.Start
|
return node.data.type === BlockEnum.Start
|
||||||
})
|
})
|
||||||
@@ -91,7 +93,7 @@ const VarReferencePicker: FC<Props> = ({
|
|||||||
const [varKindType, setVarKindType] = useState<VarKindType>(defaultVarKindType)
|
const [varKindType, setVarKindType] = useState<VarKindType>(defaultVarKindType)
|
||||||
const isConstant = isSupportConstantValue && varKindType === VarKindType.constant
|
const isConstant = isSupportConstantValue && varKindType === VarKindType.constant
|
||||||
|
|
||||||
const outputVars = (() => {
|
const outputVars = useMemo(() => {
|
||||||
if (availableVars)
|
if (availableVars)
|
||||||
return availableVars
|
return availableVars
|
||||||
|
|
||||||
@@ -104,7 +106,8 @@ const VarReferencePicker: FC<Props> = ({
|
|||||||
})
|
})
|
||||||
|
|
||||||
return vars
|
return vars
|
||||||
})()
|
}, [iterationNode, availableNodes, isChatMode, filterVar, availableVars, t])
|
||||||
|
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onOpen()
|
onOpen()
|
||||||
@@ -112,16 +115,16 @@ const VarReferencePicker: FC<Props> = ({
|
|||||||
}, [open])
|
}, [open])
|
||||||
const hasValue = !isConstant && value.length > 0
|
const hasValue = !isConstant && value.length > 0
|
||||||
|
|
||||||
const isIterationVar = (() => {
|
const isIterationVar = useMemo(() => {
|
||||||
if (!isInIteration)
|
if (!isInIteration)
|
||||||
return false
|
return false
|
||||||
if (value[0] === node?.parentId && ['item', 'index'].includes(value[1]))
|
if (value[0] === node?.parentId && ['item', 'index'].includes(value[1]))
|
||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
})()
|
}, [isInIteration, value, node])
|
||||||
|
|
||||||
const outputVarNodeId = hasValue ? value[0] : ''
|
const outputVarNodeId = hasValue ? value[0] : ''
|
||||||
const outputVarNode = (() => {
|
const outputVarNode = useMemo(() => {
|
||||||
if (!hasValue || isConstant)
|
if (!hasValue || isConstant)
|
||||||
return null
|
return null
|
||||||
|
|
||||||
@@ -132,16 +135,16 @@ const VarReferencePicker: FC<Props> = ({
|
|||||||
return startNode?.data
|
return startNode?.data
|
||||||
|
|
||||||
return getNodeInfoById(availableNodes, outputVarNodeId)?.data
|
return getNodeInfoById(availableNodes, outputVarNodeId)?.data
|
||||||
})()
|
}, [value, hasValue, isConstant, isIterationVar, iterationNode, availableNodes, outputVarNodeId, startNode])
|
||||||
|
|
||||||
const varName = (() => {
|
const varName = useMemo(() => {
|
||||||
if (hasValue) {
|
if (hasValue) {
|
||||||
const isSystem = isSystemVar(value as ValueSelector)
|
const isSystem = isSystemVar(value as ValueSelector)
|
||||||
const varName = value.length >= 3 ? (value as ValueSelector).slice(-2).join('.') : value[value.length - 1]
|
const varName = value.length >= 3 ? (value as ValueSelector).slice(-2).join('.') : value[value.length - 1]
|
||||||
return `${isSystem ? 'sys.' : ''}${varName}`
|
return `${isSystem ? 'sys.' : ''}${varName}`
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
})()
|
}, [hasValue, value])
|
||||||
|
|
||||||
const varKindTypes = [
|
const varKindTypes = [
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user