panel.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import type { FC } from 'react'
  2. import { memo } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import useConfig from './use-config'
  5. import ApiInput from './components/api-input'
  6. import KeyValue from './components/key-value'
  7. import EditBody from './components/edit-body'
  8. import AuthorizationModal from './components/authorization'
  9. import type { HttpNodeType } from './types'
  10. import Timeout from './components/timeout'
  11. import CurlPanel from './components/curl-panel'
  12. import Field from '@/app/components/workflow/nodes/_base/components/field'
  13. import Split from '@/app/components/workflow/nodes/_base/components/split'
  14. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  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. isDataReady,
  27. inputs,
  28. handleMethodChange,
  29. handleUrlChange,
  30. headers,
  31. setHeaders,
  32. addHeader,
  33. params,
  34. setParams,
  35. addParam,
  36. setBody,
  37. isShowAuthorization,
  38. showAuthorization,
  39. hideAuthorization,
  40. setAuthorization,
  41. setTimeout,
  42. // single run
  43. isShowSingleRun,
  44. hideSingleRun,
  45. runningStatus,
  46. handleRun,
  47. handleStop,
  48. varInputs,
  49. inputVarValues,
  50. setInputVarValues,
  51. runResult,
  52. isShowCurlPanel,
  53. showCurlPanel,
  54. hideCurlPanel,
  55. handleCurlImport,
  56. } = useConfig(id, data)
  57. // To prevent prompt editor in body not update data.
  58. if (!isDataReady)
  59. return null
  60. return (
  61. <div className='pt-2'>
  62. <div className='space-y-4 px-4 pb-4'>
  63. <Field
  64. title={t(`${i18nPrefix}.api`)}
  65. // operations={
  66. // <div className='flex'>
  67. // <div
  68. // onClick={showAuthorization}
  69. // className={cn(!readOnly && 'cursor-pointer hover:bg-gray-50', 'flex h-6 items-center space-x-1 rounded-md px-2 ')}
  70. // >
  71. // {!readOnly && <Settings01 className='h-3 w-3 text-gray-500' />}
  72. // <div className='text-xs font-medium text-gray-500'>
  73. // {t(`${i18nPrefix}.authorization.authorization`)}
  74. // <span className='ml-1 text-gray-700'>{t(`${i18nPrefix}.authorization.${inputs.authorization.type}`)}</span>
  75. // </div>
  76. // </div>
  77. // <div
  78. // onClick={showCurlPanel}
  79. // className={cn(!readOnly && 'cursor-pointer hover:bg-gray-50', 'flex h-6 items-center space-x-1 rounded-md px-2 ')}
  80. // >
  81. // {!readOnly && <FileArrow01 className='h-3 w-3 text-gray-500' />}
  82. // <div className='text-xs font-medium text-gray-500'>
  83. // {t(`${i18nPrefix}.curl.title`)}
  84. // </div>
  85. // </div>
  86. // </div>
  87. // }
  88. >
  89. <ApiInput
  90. nodeId={id}
  91. readonly={true}
  92. method={inputs.method}
  93. onMethodChange={handleMethodChange}
  94. url={inputs.url}
  95. onUrlChange={handleUrlChange}
  96. />
  97. </Field>
  98. <Field
  99. title={t(`${i18nPrefix}.headers`)}
  100. >
  101. <KeyValue
  102. nodeId={id}
  103. list={headers}
  104. onChange={setHeaders}
  105. onAdd={addHeader}
  106. readonly={readOnly}
  107. />
  108. </Field>
  109. <Field
  110. title={t(`${i18nPrefix}.params`)}
  111. >
  112. <KeyValue
  113. nodeId={id}
  114. list={params}
  115. onChange={setParams}
  116. onAdd={addParam}
  117. readonly={readOnly}
  118. />
  119. </Field>
  120. <Field
  121. title={t(`${i18nPrefix}.body`)}
  122. >
  123. <EditBody
  124. nodeId={id}
  125. readonly={readOnly}
  126. payload={inputs.body}
  127. onChange={setBody}
  128. />
  129. </Field>
  130. </div>
  131. <Split />
  132. <Timeout
  133. nodeId={id}
  134. readonly={readOnly}
  135. payload={inputs.timeout}
  136. onChange={setTimeout}
  137. />
  138. {(isShowAuthorization && !readOnly) && (
  139. <AuthorizationModal
  140. nodeId={id}
  141. isShow
  142. onHide={hideAuthorization}
  143. payload={inputs.authorization}
  144. onChange={setAuthorization}
  145. />
  146. )}
  147. <Split />
  148. <div className=''>
  149. <OutputVars>
  150. <>
  151. <VarItem
  152. name='body'
  153. type='string'
  154. description={t(`${i18nPrefix}.outputVars.body`)}
  155. />
  156. <VarItem
  157. name='status_code'
  158. type='number'
  159. description={t(`${i18nPrefix}.outputVars.statusCode`)}
  160. />
  161. <VarItem
  162. name='headers'
  163. type='object'
  164. description={t(`${i18nPrefix}.outputVars.headers`)}
  165. />
  166. <VarItem
  167. name='files'
  168. type='Array[File]'
  169. description={t(`${i18nPrefix}.outputVars.files`)}
  170. />
  171. </>
  172. </OutputVars>
  173. </div>
  174. {isShowSingleRun && (
  175. <BeforeRunForm
  176. nodeName={inputs.title}
  177. nodeType={inputs.type}
  178. onHide={hideSingleRun}
  179. forms={[
  180. {
  181. inputs: varInputs,
  182. values: inputVarValues,
  183. onChange: setInputVarValues,
  184. },
  185. ]}
  186. runningStatus={runningStatus}
  187. onRun={handleRun}
  188. onStop={handleStop}
  189. result={<ResultPanel {...runResult} showSteps={false} />}
  190. />
  191. )}
  192. {(isShowCurlPanel && !readOnly) && (
  193. <CurlPanel
  194. nodeId={id}
  195. isShow
  196. onHide={hideCurlPanel}
  197. handleCurlImport={handleCurlImport}
  198. />
  199. )}
  200. </div>
  201. )
  202. }
  203. export default memo(Panel)