panel.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Field from '../_base/components/field'
  5. import RemoveEffectVarConfirm from '../_base/components/remove-effect-var-confirm'
  6. import useConfig from './use-config'
  7. import type { VariableAssignerNodeType } from './types'
  8. import VarGroupItem from './components/var-group-item'
  9. import cn from '@/utils/classnames'
  10. import { type NodePanelProps } from '@/app/components/workflow/types'
  11. import Split from '@/app/components/workflow/nodes/_base/components/split'
  12. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  13. import Switch from '@/app/components/base/switch'
  14. import AddButton from '@/app/components/workflow/nodes/_base/components/add-button'
  15. const i18nPrefix = 'workflow.nodes.variableAssigner'
  16. const Panel: FC<NodePanelProps<VariableAssignerNodeType>> = ({
  17. id,
  18. data,
  19. }) => {
  20. const { t } = useTranslation()
  21. const {
  22. readOnly,
  23. inputs,
  24. handleListOrTypeChange,
  25. isEnableGroup,
  26. handleGroupEnabledChange,
  27. handleAddGroup,
  28. handleListOrTypeChangeInGroup,
  29. handleGroupRemoved,
  30. handleVarGroupNameChange,
  31. isShowRemoveVarConfirm,
  32. hideRemoveVarConfirm,
  33. onRemoveVarConfirm,
  34. getAvailableVars,
  35. filterVar,
  36. } = useConfig(id, data)
  37. return (
  38. <div className='mt-2'>
  39. <div className='px-4 pb-4 space-y-4'>
  40. {!isEnableGroup
  41. ? (
  42. <VarGroupItem
  43. readOnly={readOnly}
  44. nodeId={id}
  45. payload={{
  46. output_type: inputs.output_type,
  47. variables: inputs.variables,
  48. }}
  49. onChange={handleListOrTypeChange}
  50. groupEnabled={false}
  51. availableVars={getAvailableVars(id, 'target', filterVar(inputs.output_type), true)}
  52. />
  53. )
  54. : (<div>
  55. <div className='space-y-2'>
  56. {inputs.advanced_settings?.groups.map((item, index) => (
  57. <div key={item.groupId}>
  58. <VarGroupItem
  59. readOnly={readOnly}
  60. nodeId={id}
  61. payload={item}
  62. onChange={handleListOrTypeChangeInGroup(item.groupId)}
  63. groupEnabled
  64. canRemove={!readOnly && inputs.advanced_settings?.groups.length > 1}
  65. onRemove={handleGroupRemoved(item.groupId)}
  66. onGroupNameChange={handleVarGroupNameChange(item.groupId)}
  67. availableVars={getAvailableVars(id, item.groupId, filterVar(item.output_type), true)}
  68. />
  69. {index !== inputs.advanced_settings?.groups.length - 1 && <Split className='my-4' />}
  70. </div>
  71. ))}
  72. </div>
  73. <AddButton
  74. className='mt-2'
  75. text={t(`${i18nPrefix}.addGroup`)}
  76. onClick={handleAddGroup}
  77. />
  78. </div>)}
  79. </div>
  80. <Split />
  81. <div className={cn('px-4 pt-4', isEnableGroup ? 'pb-4' : 'pb-2')}>
  82. <Field
  83. title={t(`${i18nPrefix}.aggregationGroup`)}
  84. tooltip={t(`${i18nPrefix}.aggregationGroupTip`)!}
  85. operations={
  86. <Switch
  87. defaultValue={isEnableGroup}
  88. onChange={handleGroupEnabledChange}
  89. size='md'
  90. disabled={readOnly}
  91. />
  92. }
  93. />
  94. </div>
  95. {isEnableGroup && (
  96. <>
  97. <Split />
  98. <div className='px-4 pt-4 pb-2'>
  99. <OutputVars>
  100. <>
  101. {inputs.advanced_settings?.groups.map((item, index) => (
  102. <VarItem
  103. key={index}
  104. name={`${item.group_name}.output`}
  105. type={item.output_type}
  106. description={t(`${i18nPrefix}.outputVars.varDescribe`, {
  107. groupName: item.group_name,
  108. })}
  109. />
  110. ))}
  111. </>
  112. </OutputVars>
  113. </div>
  114. </>
  115. )}
  116. <RemoveEffectVarConfirm
  117. isShow={isShowRemoveVarConfirm}
  118. onCancel={hideRemoveVarConfirm}
  119. onConfirm={onRemoveVarConfirm}
  120. />
  121. </div>
  122. )
  123. }
  124. export default React.memo(Panel)