refactor(variable-list): improve variable list handling with useMemo

Use useMemo for listWithIds to optimize performance by avoiding unnecessary recalculations. Also rename payloadWithIds to listWithIds for better clarity.
pull/22127/head
Mminamiyama 11 months ago
parent 4095b8c11c
commit 292566b121

@ -1,6 +1,6 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback } from 'react' import React, { useCallback, useMemo } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import produce from 'immer' import produce from 'immer'
import RemoveButton from '../remove-button' import RemoveButton from '../remove-button'
@ -40,13 +40,13 @@ const VarList: FC<Props> = ({
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const payloadWithIds = list.map((item) => { const listWithIds = useMemo(() => list.map((item) => {
const id = uuid4() const id = uuid4()
return { return {
id, id,
v: { ...item }, variable: { ...item },
} }
}) }), [list])
const handleVarNameChange = useCallback((index: number) => { const handleVarNameChange = useCallback((index: number) => {
return (e: React.ChangeEvent<HTMLInputElement>) => { return (e: React.ChangeEvent<HTMLInputElement>) => {
@ -122,24 +122,25 @@ const VarList: FC<Props> = ({
return ( return (
<ReactSortable <ReactSortable
className='space-y-2' className='space-y-2'
list={payloadWithIds} list={listWithIds}
setList={(list) => { onChange(list.map(item => item.v)) }} setList={(list) => { onChange(list.map(item => item.variable)) }}
handle='.handle' handle='.handle'
ghostClass='opacity-50' ghostClass='opacity-50'
animation={150} animation={150}
> >
{list.map((item, index) => { {listWithIds.map((item, index) => {
const canDrag = (() => { const canDrag = (() => {
if (readonly) if (readonly)
return false return false
return varCount > 1 return varCount > 1
})() })()
const variable = item.variable
return ( return (
<div className={cn('flex items-center space-x-1', 'group relative')} key={index}> <div className={cn('flex items-center space-x-1', 'group relative')} key={index}>
<Input <Input
wrapperClassName='w-[120px]' wrapperClassName='w-[120px]'
disabled={readonly} disabled={readonly}
value={list[index].variable} value={variable.variable}
onChange={handleVarNameChange(index)} onChange={handleVarNameChange(index)}
placeholder={t('workflow.common.variableNamePlaceholder')!} placeholder={t('workflow.common.variableNamePlaceholder')!}
/> />
@ -148,10 +149,10 @@ const VarList: FC<Props> = ({
readonly={readonly} readonly={readonly}
isShowNodeName isShowNodeName
className='grow' className='grow'
value={item.variable_type === VarKindType.constant ? (item.value || '') : (item.value_selector || [])} value={variable.variable_type === VarKindType.constant ? (variable.value || '') : (variable.value_selector || [])}
isSupportConstantValue={isSupportConstantValue} isSupportConstantValue={isSupportConstantValue}
onChange={handleVarReferenceChange(index)} onChange={handleVarReferenceChange(index)}
defaultVarKindType={item.variable_type} defaultVarKindType={variable.variable_type}
onlyLeafNodeVar={onlyLeafNodeVar} onlyLeafNodeVar={onlyLeafNodeVar}
filterVar={filterVar} filterVar={filterVar}
isSupportFileVar={isSupportFileVar} isSupportFileVar={isSupportFileVar}

Loading…
Cancel
Save