panel.tsx 5.2 KB

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