panel.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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'
  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. handleCodeChange,
  29. filterVar,
  30. // single run
  31. isShowSingleRun,
  32. hideSingleRun,
  33. runningStatus,
  34. handleRun,
  35. handleStop,
  36. varInputs,
  37. inputVarValues,
  38. setInputVarValues,
  39. runResult,
  40. } = useConfig(id, data)
  41. return (
  42. <div className='mt-2'>
  43. <div className='px-4 pb-4 space-y-4'>
  44. <Field
  45. title={t(`${i18nPrefix}.inputVars`)}
  46. operations={
  47. !readOnly ? <AddButton onClick={handleAddVariable} /> : undefined
  48. }
  49. >
  50. <VarList
  51. nodeId={id}
  52. readonly={readOnly}
  53. list={inputs.variables}
  54. onChange={handleVarListChange}
  55. filterVar={filterVar}
  56. />
  57. </Field>
  58. <Split />
  59. <CodeEditor
  60. isInNode
  61. readOnly={readOnly}
  62. language={CodeLanguage.python3}
  63. title={
  64. <div className='uppercase'>{t(`${i18nPrefix}.code`)}</div>
  65. }
  66. headerRight={
  67. <div className='flex items-center'>
  68. <a
  69. className='flex items-center space-x-0.5 h-[18px] text-xs font-normal text-gray-500'
  70. href="https://jinja.palletsprojects.com/en/3.1.x/templates/"
  71. target='_blank'>
  72. <span>{t(`${i18nPrefix}.codeSupportTip`)}</span>
  73. <HelpCircle className='w-3 h-3' />
  74. </a>
  75. <div className='mx-1.5 w-px h-3 bg-gray-200'></div>
  76. </div>
  77. }
  78. value={inputs.template}
  79. onChange={handleCodeChange}
  80. />
  81. </div>
  82. <Split />
  83. <div className='px-4 pt-4 pb-2'>
  84. <OutputVars>
  85. <>
  86. <VarItem
  87. name='output'
  88. type='string'
  89. description={t(`${i18nPrefix}.outputVars.output`)}
  90. />
  91. </>
  92. </OutputVars>
  93. </div>
  94. {isShowSingleRun && (
  95. <BeforeRunForm
  96. nodeName={inputs.title}
  97. onHide={hideSingleRun}
  98. forms={[
  99. {
  100. inputs: varInputs,
  101. values: inputVarValues,
  102. onChange: setInputVarValues,
  103. },
  104. ]}
  105. runningStatus={runningStatus}
  106. onRun={handleRun}
  107. onStop={handleStop}
  108. result={<ResultPanel {...runResult} showSteps={false} />}
  109. />
  110. )}
  111. </div>
  112. )
  113. }
  114. export default React.memo(Panel)