您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.tsx 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. 'use client'
  2. import { useMemo, useRef, useState } from 'react'
  3. import { useRouter } from 'next/navigation'
  4. import { useContext } from 'use-context-selector'
  5. import { useTranslation } from 'react-i18next'
  6. import { useDebounceFn, useKeyPress } from 'ahooks'
  7. import Button from '@/app/components/base/button'
  8. import Input from '@/app/components/base/input'
  9. import Modal from '@/app/components/base/modal'
  10. import { ToastContext } from '@/app/components/base/toast'
  11. import {
  12. DSLImportMode,
  13. DSLImportStatus,
  14. } from '@/models/app'
  15. import { useProviderContextSelector } from '@/context/provider-context'
  16. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  17. import { usePluginDependencies } from '@/app/components/workflow/plugin-dependency/hooks'
  18. import { noop } from 'lodash-es'
  19. import Uploader from './uploader'
  20. import Header from './header'
  21. import Tab from './tab'
  22. import { useImportPipelineDSL, useImportPipelineDSLConfirm } from '@/service/use-pipeline'
  23. type CreateFromDSLModalProps = {
  24. show: boolean
  25. onSuccess?: () => void
  26. onClose: () => void
  27. activeTab?: CreateFromDSLModalTab
  28. dslUrl?: string
  29. }
  30. export enum CreateFromDSLModalTab {
  31. FROM_FILE = 'from-file',
  32. FROM_URL = 'from-url',
  33. }
  34. const CreateFromDSLModal = ({
  35. show,
  36. onSuccess,
  37. onClose,
  38. activeTab = CreateFromDSLModalTab.FROM_FILE,
  39. dslUrl = '',
  40. }: CreateFromDSLModalProps) => {
  41. const { push } = useRouter()
  42. const { t } = useTranslation()
  43. const { notify } = useContext(ToastContext)
  44. const [currentFile, setDSLFile] = useState<File>()
  45. const [fileContent, setFileContent] = useState<string>()
  46. const [currentTab, setCurrentTab] = useState(activeTab)
  47. const [dslUrlValue, setDslUrlValue] = useState(dslUrl)
  48. const [showErrorModal, setShowErrorModal] = useState(false)
  49. const [versions, setVersions] = useState<{ importedVersion: string; systemVersion: string }>()
  50. const [importId, setImportId] = useState<string>()
  51. const { handleCheckPluginDependencies } = usePluginDependencies()
  52. const readFile = (file: File) => {
  53. const reader = new FileReader()
  54. reader.onload = function (event) {
  55. const content = event.target?.result
  56. setFileContent(content as string)
  57. }
  58. reader.readAsText(file)
  59. }
  60. const handleFile = (file?: File) => {
  61. setDSLFile(file)
  62. if (file)
  63. readFile(file)
  64. if (!file)
  65. setFileContent('')
  66. }
  67. const plan = useProviderContextSelector(state => state.plan)
  68. const enableBilling = useProviderContextSelector(state => state.enableBilling)
  69. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  70. const isCreatingRef = useRef(false)
  71. const { mutateAsync: importDSL } = useImportPipelineDSL()
  72. const onCreate = async () => {
  73. if (currentTab === CreateFromDSLModalTab.FROM_FILE && !currentFile)
  74. return
  75. if (currentTab === CreateFromDSLModalTab.FROM_URL && !dslUrlValue)
  76. return
  77. if (isCreatingRef.current)
  78. return
  79. isCreatingRef.current = true
  80. try {
  81. let response
  82. if (currentTab === CreateFromDSLModalTab.FROM_FILE) {
  83. response = await importDSL({
  84. mode: DSLImportMode.YAML_CONTENT,
  85. yaml_content: fileContent || '',
  86. })
  87. }
  88. if (currentTab === CreateFromDSLModalTab.FROM_URL) {
  89. response = await importDSL({
  90. mode: DSLImportMode.YAML_URL,
  91. yaml_url: dslUrlValue || '',
  92. })
  93. }
  94. if (!response)
  95. return
  96. const { id, status, pipeline_id, dataset_id, imported_dsl_version, current_dsl_version } = response
  97. if (status === DSLImportStatus.COMPLETED || status === DSLImportStatus.COMPLETED_WITH_WARNINGS) {
  98. if (onSuccess)
  99. onSuccess()
  100. if (onClose)
  101. onClose()
  102. notify({
  103. type: status === DSLImportStatus.COMPLETED ? 'success' : 'warning',
  104. message: t(status === DSLImportStatus.COMPLETED ? 'app.newApp.appCreated' : 'app.newApp.caution'),
  105. children: status === DSLImportStatus.COMPLETED_WITH_WARNINGS && t('app.newApp.appCreateDSLWarning'),
  106. })
  107. if (pipeline_id)
  108. await handleCheckPluginDependencies(pipeline_id, true)
  109. push(`datasets/${dataset_id}/pipeline`)
  110. }
  111. else if (status === DSLImportStatus.PENDING) {
  112. setVersions({
  113. importedVersion: imported_dsl_version ?? '',
  114. systemVersion: current_dsl_version ?? '',
  115. })
  116. if (onClose)
  117. onClose()
  118. setTimeout(() => {
  119. setShowErrorModal(true)
  120. }, 300)
  121. setImportId(id)
  122. }
  123. else {
  124. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  125. }
  126. }
  127. catch {
  128. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  129. }
  130. finally {
  131. isCreatingRef.current = false
  132. }
  133. }
  134. const { run: handleCreateApp } = useDebounceFn(onCreate, { wait: 300 })
  135. useKeyPress('esc', () => {
  136. if (show && !showErrorModal)
  137. onClose()
  138. })
  139. const { mutateAsync: importDSLConfirm } = useImportPipelineDSLConfirm()
  140. const onDSLConfirm = async () => {
  141. try {
  142. if (!importId)
  143. return
  144. const response = await importDSLConfirm(importId)
  145. const { status, pipeline_id, dataset_id } = response
  146. if (status === DSLImportStatus.COMPLETED) {
  147. if (onSuccess)
  148. onSuccess()
  149. if (onClose)
  150. onClose()
  151. notify({
  152. type: 'success',
  153. message: t('app.newApp.appCreated'),
  154. })
  155. if (pipeline_id)
  156. await handleCheckPluginDependencies(pipeline_id, true)
  157. push(`datasets/${dataset_id}/pipeline`)
  158. }
  159. else if (status === DSLImportStatus.FAILED) {
  160. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  161. }
  162. }
  163. catch {
  164. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  165. }
  166. }
  167. const buttonDisabled = useMemo(() => {
  168. if (isAppsFull)
  169. return true
  170. if (currentTab === CreateFromDSLModalTab.FROM_FILE)
  171. return !currentFile
  172. if (currentTab === CreateFromDSLModalTab.FROM_URL)
  173. return !dslUrlValue
  174. return false
  175. }, [isAppsFull, currentTab, currentFile, dslUrlValue])
  176. return (
  177. <>
  178. <Modal
  179. className='w-[520px] rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg p-0 shadow-xl'
  180. isShow={show}
  181. onClose={noop}
  182. >
  183. <Header onClose={onClose} />
  184. <Tab
  185. currentTab={currentTab}
  186. setCurrentTab={setCurrentTab}
  187. />
  188. <div className='px-6 py-4'>
  189. {
  190. currentTab === CreateFromDSLModalTab.FROM_FILE && (
  191. <Uploader
  192. className='mt-0'
  193. file={currentFile}
  194. updateFile={handleFile}
  195. />
  196. )
  197. }
  198. {
  199. currentTab === CreateFromDSLModalTab.FROM_URL && (
  200. <div>
  201. <div className='system-md-semibold leading6 mb-1'>DSL URL</div>
  202. <Input
  203. placeholder={t('app.importFromDSLUrlPlaceholder') || ''}
  204. value={dslUrlValue}
  205. onChange={e => setDslUrlValue(e.target.value)}
  206. />
  207. </div>
  208. )
  209. }
  210. </div>
  211. {isAppsFull && (
  212. <div className='px-6'>
  213. <AppsFull className='mt-0' loc='app-create-dsl' />
  214. </div>
  215. )}
  216. <div className='flex justify-end gap-x-2 p-6 pt-5'>
  217. <Button onClick={onClose}>
  218. {t('app.newApp.Cancel')}
  219. </Button>
  220. <Button
  221. disabled={buttonDisabled}
  222. variant='primary'
  223. onClick={handleCreateApp}
  224. className='gap-1'
  225. >
  226. <span>{t('app.newApp.import')}</span>
  227. </Button>
  228. </div>
  229. </Modal>
  230. <Modal
  231. isShow={showErrorModal}
  232. onClose={() => setShowErrorModal(false)}
  233. className='w-[480px]'
  234. >
  235. <div className='flex flex-col items-start gap-2 self-stretch pb-4'>
  236. <div className='title-2xl-semi-bold text-text-primary'>{t('app.newApp.appCreateDSLErrorTitle')}</div>
  237. <div className='system-md-regular flex grow flex-col text-text-secondary'>
  238. <div>{t('app.newApp.appCreateDSLErrorPart1')}</div>
  239. <div>{t('app.newApp.appCreateDSLErrorPart2')}</div>
  240. <br />
  241. <div>{t('app.newApp.appCreateDSLErrorPart3')}<span className='system-md-medium'>{versions?.importedVersion}</span></div>
  242. <div>{t('app.newApp.appCreateDSLErrorPart4')}<span className='system-md-medium'>{versions?.systemVersion}</span></div>
  243. </div>
  244. </div>
  245. <div className='flex items-start justify-end gap-2 self-stretch pt-6'>
  246. <Button variant='secondary' onClick={() => setShowErrorModal(false)}>{t('app.newApp.Cancel')}</Button>
  247. <Button variant='primary' destructive onClick={onDSLConfirm}>{t('app.newApp.Confirm')}</Button>
  248. </div>
  249. </Modal>
  250. </>
  251. )
  252. }
  253. export default CreateFromDSLModal