panel.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { CodeLanguage } from '../code/types'
  5. import useConfig from './use-config'
  6. import type { TemplateTransformNodeType } from './types'
  7. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  8. import AddButton from '@/app/components/base/button/add-button'
  9. import Field from '@/app/components/workflow/nodes/_base/components/field'
  10. import Split from '@/app/components/workflow/nodes/_base/components/split'
  11. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor/editor-support-vars'
  12. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  13. import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
  14. import type { NodePanelProps } from '@/app/components/workflow/types'
  15. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  16. import ResultPanel from '@/app/components/workflow/run/result-panel'
  17. const i18nPrefix = 'workflow.nodes.templateTransform'
  18. const Panel: FC<NodePanelProps<TemplateTransformNodeType>> = ({
  19. id,
  20. data,
  21. }) => {
  22. const { t } = useTranslation()
  23. const {
  24. readOnly,
  25. inputs,
  26. handleVarListChange,
  27. handleAddVariable,
  28. handleAddEmptyVariable,
  29. handleCodeChange,
  30. filterVar,
  31. // single run
  32. isShowSingleRun,
  33. hideSingleRun,
  34. runningStatus,
  35. handleRun,
  36. handleStop,
  37. varInputs,
  38. inputVarValues,
  39. setInputVarValues,
  40. runResult,
  41. } = useConfig(id, data)
  42. return (
  43. <div className='mt-2'>
  44. <div className='px-4 pb-4 space-y-4'>
  45. <Field
  46. title={t(`${i18nPrefix}.inputVars`)}
  47. operations={
  48. !readOnly ? <AddButton onClick={handleAddEmptyVariable} /> : undefined
  49. }
  50. >
  51. <VarList
  52. nodeId={id}
  53. readonly={readOnly}
  54. list={inputs.variables}
  55. onChange={handleVarListChange}
  56. filterVar={filterVar}
  57. />
  58. </Field>
  59. <Split />
  60. <CodeEditor
  61. nodeId={id}
  62. varList={inputs.variables}
  63. onAddVar={handleAddVariable}
  64. isInNode
  65. readOnly={readOnly}
  66. language={CodeLanguage.python3}
  67. title={
  68. <div className='uppercase'>{t(`${i18nPrefix}.code`)}</div>
  69. }
  70. headerRight={
  71. <div className='flex items-center'>
  72. <a
  73. className='flex items-center space-x-0.5 h-[18px] text-xs font-normal text-gray-500'
  74. href="https://jinja.palletsprojects.com/en/3.1.x/templates/"
  75. target='_blank'>
  76. <span>{t(`${i18nPrefix}.codeSupportTip`)}</span>
  77. <HelpCircle className='w-3 h-3' />
  78. </a>
  79. <div className='mx-1.5 w-px h-3 bg-gray-200'></div>
  80. </div>
  81. }
  82. value={inputs.template}
  83. onChange={handleCodeChange}
  84. />
  85. </div>
  86. <Split />
  87. <div className='px-4 pt-4 pb-2'>
  88. <OutputVars>
  89. <>
  90. <VarItem
  91. name='output'
  92. type='string'
  93. description={t(`${i18nPrefix}.outputVars.output`)}
  94. />
  95. </>
  96. </OutputVars>
  97. </div>
  98. {isShowSingleRun && (
  99. <BeforeRunForm
  100. nodeName={inputs.title}
  101. onHide={hideSingleRun}
  102. forms={[
  103. {
  104. inputs: varInputs,
  105. values: inputVarValues,
  106. onChange: setInputVarValues,
  107. },
  108. ]}
  109. runningStatus={runningStatus}
  110. onRun={handleRun}
  111. onStop={handleStop}
  112. result={<ResultPanel {...runResult} showSteps={false} />}
  113. />
  114. )}
  115. </div>
  116. )
  117. }
  118. export default React.memo(Panel)