panel.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useState,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { ReactSortable } from 'react-sortablejs'
  8. import {
  9. RiAddLine,
  10. RiDeleteBinLine,
  11. RiDraggable,
  12. } from '@remixicon/react'
  13. import useConfig from './use-config'
  14. import ConditionAdd from './components/condition-add'
  15. import ConditionList from './components/condition-list'
  16. import type { IfElseNodeType } from './types'
  17. import Button from '@/app/components/base/button'
  18. import type { NodePanelProps } from '@/app/components/workflow/types'
  19. import Field from '@/app/components/workflow/nodes/_base/components/field'
  20. import { useGetAvailableVars } from '@/app/components/workflow/nodes/variable-assigner/hooks'
  21. import cn from '@/utils/classnames'
  22. const i18nPrefix = 'workflow.nodes.ifElse'
  23. const Panel: FC<NodePanelProps<IfElseNodeType>> = ({
  24. id,
  25. data,
  26. }) => {
  27. const { t } = useTranslation()
  28. const getAvailableVars = useGetAvailableVars()
  29. const {
  30. readOnly,
  31. inputs,
  32. filterVar,
  33. filterNumberVar,
  34. handleAddCase,
  35. handleRemoveCase,
  36. handleSortCase,
  37. handleAddCondition,
  38. handleUpdateCondition,
  39. handleRemoveCondition,
  40. handleUpdateConditionLogicalOperator,
  41. nodesOutputVars,
  42. availableNodes,
  43. } = useConfig(id, data)
  44. const [willDeleteCaseId, setWillDeleteCaseId] = useState('')
  45. const cases = inputs.cases || []
  46. const casesLength = cases.length
  47. return (
  48. <div className='p-1'>
  49. <ReactSortable
  50. list={cases.map(caseItem => ({ ...caseItem, id: caseItem.case_id }))}
  51. setList={handleSortCase}
  52. handle='.handle'
  53. ghostClass='bg-components-panel-bg'
  54. animation={150}
  55. >
  56. {
  57. cases.map((item, index) => (
  58. <div key={item.case_id}>
  59. <div
  60. className={cn(
  61. 'group relative py-1 px-3 min-h-[40px] rounded-[10px] bg-components-panel-bg',
  62. willDeleteCaseId === item.case_id && 'bg-state-destructive-hover',
  63. )}
  64. >
  65. <RiDraggable className={cn(
  66. 'hidden handle absolute top-2 left-1 w-3 h-3 text-text-quaternary cursor-pointer',
  67. casesLength > 1 && 'group-hover:block',
  68. )} />
  69. <div className={cn(
  70. 'absolute left-4 leading-4 text-[13px] font-semibold text-text-secondary',
  71. casesLength === 1 ? 'top-2.5' : 'top-1',
  72. )}>
  73. {
  74. index === 0 ? 'IF' : 'ELIF'
  75. }
  76. {
  77. casesLength > 1 && (
  78. <div className='text-[10px] text-text-tertiary font-medium'>CASE {index + 1}</div>
  79. )
  80. }
  81. </div>
  82. {
  83. !!item.conditions.length && (
  84. <div className='mb-2'>
  85. <ConditionList
  86. disabled={readOnly}
  87. caseItem={item}
  88. onUpdateCondition={handleUpdateCondition}
  89. onRemoveCondition={handleRemoveCondition}
  90. onUpdateConditionLogicalOperator={handleUpdateConditionLogicalOperator}
  91. nodesOutputVars={nodesOutputVars}
  92. availableNodes={availableNodes}
  93. numberVariables={getAvailableVars(id, '', filterNumberVar)}
  94. />
  95. </div>
  96. )
  97. }
  98. <div className={cn(
  99. 'flex items-center justify-between pl-[60px] pr-[30px]',
  100. !item.conditions.length && 'mt-1',
  101. )}>
  102. <ConditionAdd
  103. disabled={readOnly}
  104. caseId={item.case_id}
  105. variables={getAvailableVars(id, '', filterVar)}
  106. onSelectVariable={handleAddCondition}
  107. />
  108. {
  109. ((index === 0 && casesLength > 1) || (index > 0)) && (
  110. <Button
  111. className='hover:text-components-button-destructive-ghost-text hover:bg-components-button-destructive-ghost-bg-hover'
  112. size='small'
  113. variant='ghost'
  114. disabled={readOnly}
  115. onClick={() => handleRemoveCase(item.case_id)}
  116. onMouseEnter={() => setWillDeleteCaseId(item.case_id)}
  117. onMouseLeave={() => setWillDeleteCaseId('')}
  118. >
  119. <RiDeleteBinLine className='mr-1 w-3.5 h-3.5' />
  120. {t('common.operation.remove')}
  121. </Button>
  122. )
  123. }
  124. </div>
  125. </div>
  126. <div className='my-2 mx-3 h-[1px] bg-divider-subtle'></div>
  127. </div>
  128. ))
  129. }
  130. </ReactSortable>
  131. <div className='px-4 py-2'>
  132. <Button
  133. className='w-full'
  134. variant='tertiary'
  135. onClick={() => handleAddCase()}
  136. disabled={readOnly}
  137. >
  138. <RiAddLine className='mr-1 w-4 h-4' />
  139. ELIF
  140. </Button>
  141. </div>
  142. <div className='my-2 mx-3 h-[1px] bg-divider-subtle'></div>
  143. <Field
  144. title={t(`${i18nPrefix}.else`)}
  145. className='px-4 py-2'
  146. >
  147. <div className='leading-[18px] text-xs font-normal text-text-tertiary'>{t(`${i18nPrefix}.elseDescription`)}</div>
  148. </Field>
  149. </div>
  150. )
  151. }
  152. export default memo(Panel)