panel.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. readOnly={readOnly}
  61. language={CodeLanguage.python3}
  62. title={
  63. <div className='uppercase'>{t(`${i18nPrefix}.code`)}</div>
  64. }
  65. headerRight={
  66. <div className='flex items-center'>
  67. <a
  68. className='flex items-center space-x-0.5 h-[18px] text-xs font-normal text-gray-500'
  69. href="https://jinja.palletsprojects.com/en/3.1.x/templates/"
  70. target='_blank'>
  71. <span>{t(`${i18nPrefix}.codeSupportTip`)}</span>
  72. <HelpCircle className='w-3 h-3' />
  73. </a>
  74. <div className='mx-1.5 w-px h-3 bg-gray-200'></div>
  75. </div>
  76. }
  77. value={inputs.template}
  78. onChange={handleCodeChange}
  79. />
  80. </div>
  81. <Split />
  82. <div className='px-4 pt-4 pb-2'>
  83. <OutputVars>
  84. <>
  85. <VarItem
  86. name='output'
  87. type='string'
  88. description={t(`${i18nPrefix}.outputVars.output`)}
  89. />
  90. </>
  91. </OutputVars>
  92. </div>
  93. {isShowSingleRun && (
  94. <BeforeRunForm
  95. nodeName={inputs.title}
  96. onHide={hideSingleRun}
  97. forms={[
  98. {
  99. inputs: varInputs,
  100. values: inputVarValues,
  101. onChange: setInputVarValues,
  102. },
  103. ]}
  104. runningStatus={runningStatus}
  105. onRun={handleRun}
  106. onStop={handleStop}
  107. result={<ResultPanel {...runResult} showSteps={false} />}
  108. />
  109. )}
  110. </div>
  111. )
  112. }
  113. export default React.memo(Panel)