Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

panel.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import RemoveEffectVarConfirm from '../_base/components/remove-effect-var-confirm'
  5. import useConfig from './use-config'
  6. import type { CodeNodeType } from './types'
  7. import { CodeLanguage } from './types'
  8. import { extractFunctionParams, extractReturnType } from './code-parser'
  9. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  10. import OutputVarList from '@/app/components/workflow/nodes/_base/components/variable/output-var-list'
  11. import AddButton from '@/app/components/base/button/add-button'
  12. import Field from '@/app/components/workflow/nodes/_base/components/field'
  13. import Split from '@/app/components/workflow/nodes/_base/components/split'
  14. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  15. import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
  16. import type { NodePanelProps } from '@/app/components/workflow/types'
  17. import SyncButton from '@/app/components/base/button/sync-button'
  18. const i18nPrefix = 'workflow.nodes.code'
  19. const codeLanguages = [
  20. {
  21. label: 'Python3',
  22. value: CodeLanguage.python3,
  23. },
  24. {
  25. label: 'JavaScript',
  26. value: CodeLanguage.javascript,
  27. },
  28. ]
  29. const Panel: FC<NodePanelProps<CodeNodeType>> = ({
  30. id,
  31. data,
  32. }) => {
  33. const { t } = useTranslation()
  34. const {
  35. readOnly,
  36. inputs,
  37. outputKeyOrders,
  38. handleCodeAndVarsChange,
  39. handleVarListChange,
  40. handleAddVariable,
  41. handleRemoveVariable,
  42. handleSyncFunctionSignature,
  43. handleCodeChange,
  44. handleCodeLanguageChange,
  45. handleVarsChange,
  46. handleAddOutputVariable,
  47. filterVar,
  48. isShowRemoveVarConfirm,
  49. hideRemoveVarConfirm,
  50. onRemoveVarConfirm,
  51. } = useConfig(id, data)
  52. const handleGeneratedCode = (value: string) => {
  53. const params = extractFunctionParams(value, inputs.code_language)
  54. const codeNewInput = params.map((p) => {
  55. return {
  56. variable: p,
  57. value_selector: [],
  58. }
  59. })
  60. const returnTypes = extractReturnType(value, inputs.code_language)
  61. handleCodeAndVarsChange(value, codeNewInput, returnTypes)
  62. }
  63. return (
  64. <div className='mt-2'>
  65. <div className='space-y-4 px-4 pb-4'>
  66. <Field
  67. title={t(`${i18nPrefix}.inputVars`)}
  68. operations={
  69. !readOnly ? (
  70. <div className="flex gap-2">
  71. <SyncButton popupContent={t(`${i18nPrefix}.syncFunctionSignature`)} onClick={handleSyncFunctionSignature} />
  72. <AddButton onClick={handleAddVariable} />
  73. </div>
  74. ) : undefined
  75. }
  76. >
  77. <VarList
  78. readonly={readOnly}
  79. nodeId={id}
  80. list={inputs.variables}
  81. onChange={handleVarListChange}
  82. filterVar={filterVar}
  83. isSupportFileVar={false}
  84. />
  85. </Field>
  86. <Split />
  87. <CodeEditor
  88. nodeId={id}
  89. isInNode
  90. readOnly={readOnly}
  91. title={
  92. <TypeSelector
  93. options={codeLanguages}
  94. value={inputs.code_language}
  95. onChange={handleCodeLanguageChange}
  96. />
  97. }
  98. language={inputs.code_language}
  99. value={inputs.code}
  100. onChange={handleCodeChange}
  101. onGenerated={handleGeneratedCode}
  102. showCodeGenerator={true}
  103. />
  104. </div>
  105. <Split />
  106. <div className='px-4 pb-2 pt-4'>
  107. <Field
  108. title={t(`${i18nPrefix}.outputVars`)}
  109. operations={
  110. <AddButton onClick={handleAddOutputVariable} />
  111. }
  112. required
  113. >
  114. <OutputVarList
  115. readonly={readOnly}
  116. outputs={inputs.outputs}
  117. outputKeyOrders={outputKeyOrders}
  118. onChange={handleVarsChange}
  119. onRemove={handleRemoveVariable}
  120. />
  121. </Field>
  122. </div>
  123. <RemoveEffectVarConfirm
  124. isShow={isShowRemoveVarConfirm}
  125. onCancel={hideRemoveVarConfirm}
  126. onConfirm={onRemoveVarConfirm}
  127. />
  128. </div >
  129. )
  130. }
  131. export default React.memo(Panel)