panel.tsx 3.9 KB

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