panel.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import cn from 'classnames'
  5. import useConfig from './use-config'
  6. import ApiInput from './components/api-input'
  7. import KeyValue from './components/key-value'
  8. import EditBody from './components/edit-body'
  9. import AuthorizationModal from './components/authorization'
  10. import type { HttpNodeType } from './types'
  11. import Field from '@/app/components/workflow/nodes/_base/components/field'
  12. import Split from '@/app/components/workflow/nodes/_base/components/split'
  13. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  14. import { Settings01 } from '@/app/components/base/icons/src/vender/line/general'
  15. import type { NodePanelProps } from '@/app/components/workflow/types'
  16. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  17. import ResultPanel from '@/app/components/workflow/run/result-panel'
  18. const i18nPrefix = 'workflow.nodes.http'
  19. const Panel: FC<NodePanelProps<HttpNodeType>> = ({
  20. id,
  21. data,
  22. }) => {
  23. const { t } = useTranslation()
  24. const {
  25. readOnly,
  26. inputs,
  27. handleMethodChange,
  28. handleUrlChange,
  29. headers,
  30. setHeaders,
  31. addHeader,
  32. params,
  33. setParams,
  34. addParam,
  35. setBody,
  36. isShowAuthorization,
  37. showAuthorization,
  38. hideAuthorization,
  39. setAuthorization,
  40. // single run
  41. isShowSingleRun,
  42. hideSingleRun,
  43. runningStatus,
  44. handleRun,
  45. handleStop,
  46. varInputs,
  47. inputVarValues,
  48. setInputVarValues,
  49. runResult,
  50. } = useConfig(id, data)
  51. return (
  52. <div className='mt-2'>
  53. <div className='px-4 pb-4 space-y-4'>
  54. <Field
  55. title={t(`${i18nPrefix}.api`)}
  56. operations={
  57. <div
  58. onClick={showAuthorization}
  59. className={cn(!readOnly && 'cursor-pointer hover:bg-gray-50', 'flex items-center h-6 space-x-1 px-2 rounded-md ')}
  60. >
  61. {!readOnly && <Settings01 className='w-3 h-3 text-gray-500' />}
  62. <div className='text-xs font-medium text-gray-500'>
  63. {t(`${i18nPrefix}.authorization.authorization`)}
  64. <span className='ml-1 text-gray-700'>{t(`${i18nPrefix}.authorization.${inputs.authorization.type}`)}</span>
  65. </div>
  66. </div>
  67. }
  68. >
  69. <ApiInput
  70. nodeId={id}
  71. readonly={readOnly}
  72. method={inputs.method}
  73. onMethodChange={handleMethodChange}
  74. url={inputs.url}
  75. onUrlChange={handleUrlChange}
  76. />
  77. </Field>
  78. <Field
  79. title={t(`${i18nPrefix}.headers`)}
  80. >
  81. <KeyValue
  82. nodeId={id}
  83. list={headers}
  84. onChange={setHeaders}
  85. onAdd={addHeader}
  86. readonly={readOnly}
  87. />
  88. </Field>
  89. <Field
  90. title={t(`${i18nPrefix}.params`)}
  91. >
  92. <KeyValue
  93. nodeId={id}
  94. list={params}
  95. onChange={setParams}
  96. onAdd={addParam}
  97. readonly={readOnly}
  98. />
  99. </Field>
  100. <Field
  101. title={t(`${i18nPrefix}.body`)}
  102. >
  103. <EditBody
  104. nodeId={id}
  105. readonly={readOnly}
  106. payload={inputs.body}
  107. onChange={setBody}
  108. />
  109. </Field>
  110. </div>
  111. {(isShowAuthorization && !readOnly) && (
  112. <AuthorizationModal
  113. isShow
  114. onHide={hideAuthorization}
  115. payload={inputs.authorization}
  116. onChange={setAuthorization}
  117. />
  118. )}
  119. <Split />
  120. <div className='px-4 pt-4 pb-2'>
  121. <OutputVars>
  122. <>
  123. <VarItem
  124. name='body'
  125. type='string'
  126. description={t(`${i18nPrefix}.outputVars.body`)}
  127. />
  128. <VarItem
  129. name='status_code'
  130. type='number'
  131. description={t(`${i18nPrefix}.outputVars.statusCode`)}
  132. />
  133. <VarItem
  134. name='headers'
  135. type='object'
  136. description={t(`${i18nPrefix}.outputVars.headers`)}
  137. />
  138. <VarItem
  139. name='files'
  140. type='Array[File]'
  141. description={t(`${i18nPrefix}.outputVars.files`)}
  142. />
  143. </>
  144. </OutputVars>
  145. </div>
  146. {isShowSingleRun && (
  147. <BeforeRunForm
  148. nodeName={inputs.title}
  149. onHide={hideSingleRun}
  150. forms={[
  151. {
  152. inputs: varInputs,
  153. values: inputVarValues,
  154. onChange: setInputVarValues,
  155. },
  156. ]}
  157. runningStatus={runningStatus}
  158. onRun={handleRun}
  159. onStop={handleStop}
  160. result={<ResultPanel {...runResult} showSteps={false} />}
  161. />
  162. )}
  163. </div >
  164. )
  165. }
  166. export default React.memo(Panel)