panel.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  5. import OutputVars, { VarItem } from '../_base/components/output-vars'
  6. import OptionCard from '../_base/components/option-card'
  7. import Split from '../_base/components/split'
  8. import useConfig from './use-config'
  9. import SubVariablePicker from './components/sub-variable-picker'
  10. import { type ListFilterNodeType, OrderBy } from './types'
  11. import LimitConfig from './components/limit-config'
  12. import FilterCondition from './components/filter-condition'
  13. import Field from '@/app/components/workflow/nodes/_base/components/field'
  14. import { type NodePanelProps } from '@/app/components/workflow/types'
  15. import Switch from '@/app/components/base/switch'
  16. import ExtractInput from '@/app/components/workflow/nodes/list-operator/components/extract-input'
  17. const i18nPrefix = 'workflow.nodes.listFilter'
  18. const Panel: FC<NodePanelProps<ListFilterNodeType>> = ({
  19. id,
  20. data,
  21. }) => {
  22. const { t } = useTranslation()
  23. const {
  24. readOnly,
  25. inputs,
  26. itemVarType,
  27. itemVarTypeShowName,
  28. hasSubVariable,
  29. handleVarChanges,
  30. filterVar,
  31. handleFilterEnabledChange,
  32. handleFilterChange,
  33. handleExtractsEnabledChange,
  34. handleExtractsChange,
  35. handleLimitChange,
  36. handleOrderByEnabledChange,
  37. handleOrderByKeyChange,
  38. handleOrderByTypeChange,
  39. } = useConfig(id, data)
  40. return (
  41. <div className='mt-2'>
  42. <div className='px-4 pb-4 space-y-4'>
  43. <Field
  44. title={t(`${i18nPrefix}.inputVar`)}
  45. >
  46. <VarReferencePicker
  47. readonly={readOnly}
  48. nodeId={id}
  49. isShowNodeName
  50. value={inputs.variable || []}
  51. onChange={handleVarChanges}
  52. filterVar={filterVar}
  53. typePlaceHolder='Array'
  54. />
  55. </Field>
  56. <Field
  57. title={t(`${i18nPrefix}.filterCondition`)}
  58. operations={
  59. <Switch
  60. defaultValue={inputs.filter_by?.enabled}
  61. onChange={handleFilterEnabledChange}
  62. size='md'
  63. disabled={readOnly}
  64. />
  65. }
  66. >
  67. {inputs.filter_by?.enabled
  68. ? (
  69. <FilterCondition
  70. condition={inputs.filter_by.conditions[0]}
  71. onChange={handleFilterChange}
  72. varType={itemVarType}
  73. hasSubVariable={hasSubVariable}
  74. readOnly={readOnly}
  75. />
  76. )
  77. : null}
  78. </Field>
  79. <Split />
  80. <Field
  81. title={t(`${i18nPrefix}.extractsCondition`)}
  82. operations={
  83. <Switch
  84. defaultValue={inputs.extract_by?.enabled}
  85. onChange={handleExtractsEnabledChange}
  86. size='md'
  87. disabled={readOnly}
  88. />
  89. }
  90. >
  91. {inputs.extract_by?.enabled
  92. ? (
  93. <div className='flex items-center justify-between'>
  94. {hasSubVariable && (
  95. <div className='grow mr-2'>
  96. <ExtractInput
  97. value={inputs.extract_by.serial as string}
  98. onChange={handleExtractsChange}
  99. readOnly={readOnly}
  100. nodeId={id}
  101. />
  102. </div>
  103. )}
  104. </div>
  105. )
  106. : null}
  107. </Field>
  108. <Split />
  109. <LimitConfig
  110. config={inputs.limit}
  111. onChange={handleLimitChange}
  112. readonly={readOnly}
  113. />
  114. <Split />
  115. <Field
  116. title={t(`${i18nPrefix}.orderBy`)}
  117. operations={
  118. <Switch
  119. defaultValue={inputs.order_by?.enabled}
  120. onChange={handleOrderByEnabledChange}
  121. size='md'
  122. disabled={readOnly}
  123. />
  124. }
  125. >
  126. {inputs.order_by?.enabled
  127. ? (
  128. <div className='flex items-center justify-between'>
  129. {hasSubVariable && (
  130. <div className='grow mr-2'>
  131. <SubVariablePicker
  132. value={inputs.order_by.key as string}
  133. onChange={handleOrderByKeyChange}
  134. />
  135. </div>
  136. )}
  137. <div className={!hasSubVariable ? 'w-full grid grid-cols-2 gap-1' : 'shrink-0 flex space-x-1'}>
  138. <OptionCard
  139. title={t(`${i18nPrefix}.asc`)}
  140. onSelect={handleOrderByTypeChange(OrderBy.ASC)}
  141. selected={inputs.order_by.value === OrderBy.ASC}
  142. />
  143. <OptionCard
  144. title={t(`${i18nPrefix}.desc`)}
  145. onSelect={handleOrderByTypeChange(OrderBy.DESC)}
  146. selected={inputs.order_by.value === OrderBy.DESC}
  147. />
  148. </div>
  149. </div>
  150. )
  151. : null}
  152. </Field>
  153. <Split />
  154. </div>
  155. <div className='px-4 pt-4 pb-2'>
  156. <OutputVars>
  157. <>
  158. <VarItem
  159. name='result'
  160. type={`Array[${itemVarTypeShowName}]`}
  161. description={t(`${i18nPrefix}.outputVars.result`)}
  162. />
  163. <VarItem
  164. name='first_record'
  165. type={itemVarTypeShowName}
  166. description={t(`${i18nPrefix}.outputVars.first_record`)}
  167. />
  168. <VarItem
  169. name='last_record'
  170. type={itemVarTypeShowName}
  171. description={t(`${i18nPrefix}.outputVars.last_record`)}
  172. />
  173. </>
  174. </OutputVars>
  175. </div>
  176. </div>
  177. )
  178. }
  179. export default React.memo(Panel)