Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

use-import-dsl.ts 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {
  2. useCallback,
  3. useRef,
  4. useState,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { useRouter } from 'next/navigation'
  8. import type {
  9. DSLImportMode,
  10. DSLImportResponse,
  11. } from '@/models/app'
  12. import { DSLImportStatus } from '@/models/app'
  13. import {
  14. importDSL,
  15. importDSLConfirm,
  16. } from '@/service/apps'
  17. import type { AppIconType } from '@/types/app'
  18. import { useToastContext } from '@/app/components/base/toast'
  19. import { usePluginDependencies } from '@/app/components/workflow/plugin-dependency/hooks'
  20. import { getRedirection } from '@/utils/app-redirection'
  21. import { useSelector } from '@/context/app-context'
  22. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  23. type DSLPayload = {
  24. mode: DSLImportMode
  25. yaml_content?: string
  26. yaml_url?: string
  27. name?: string
  28. icon_type?: AppIconType
  29. icon?: string
  30. icon_background?: string
  31. description?: string
  32. }
  33. type ResponseCallback = {
  34. onSuccess?: () => void
  35. onPending?: (payload: DSLImportResponse) => void
  36. onFailed?: () => void
  37. }
  38. export const useImportDSL = () => {
  39. const { t } = useTranslation()
  40. const { notify } = useToastContext()
  41. const [isFetching, setIsFetching] = useState(false)
  42. const { handleCheckPluginDependencies } = usePluginDependencies()
  43. const isCurrentWorkspaceEditor = useSelector(s => s.isCurrentWorkspaceEditor)
  44. const { push } = useRouter()
  45. const [versions, setVersions] = useState<{ importedVersion: string; systemVersion: string }>()
  46. const importIdRef = useRef<string>('')
  47. const handleImportDSL = useCallback(async (
  48. payload: DSLPayload,
  49. {
  50. onSuccess,
  51. onPending,
  52. onFailed,
  53. }: ResponseCallback,
  54. ) => {
  55. if (isFetching)
  56. return
  57. setIsFetching(true)
  58. try {
  59. const response = await importDSL(payload)
  60. if (!response)
  61. return
  62. const {
  63. id,
  64. status,
  65. app_id,
  66. app_mode,
  67. imported_dsl_version,
  68. current_dsl_version,
  69. } = response
  70. if (status === DSLImportStatus.COMPLETED || status === DSLImportStatus.COMPLETED_WITH_WARNINGS) {
  71. if (!app_id)
  72. return
  73. notify({
  74. type: status === DSLImportStatus.COMPLETED ? 'success' : 'warning',
  75. message: t(status === DSLImportStatus.COMPLETED ? 'app.newApp.appCreated' : 'app.newApp.caution'),
  76. children: status === DSLImportStatus.COMPLETED_WITH_WARNINGS && t('app.newApp.appCreateDSLWarning'),
  77. })
  78. onSuccess?.()
  79. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  80. await handleCheckPluginDependencies(app_id)
  81. getRedirection(isCurrentWorkspaceEditor, { id: app_id, mode: app_mode }, push)
  82. }
  83. else if (status === DSLImportStatus.PENDING) {
  84. setVersions({
  85. importedVersion: imported_dsl_version ?? '',
  86. systemVersion: current_dsl_version ?? '',
  87. })
  88. importIdRef.current = id
  89. onPending?.(response)
  90. }
  91. else {
  92. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  93. onFailed?.()
  94. }
  95. }
  96. catch {
  97. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  98. onFailed?.()
  99. }
  100. finally {
  101. setIsFetching(false)
  102. }
  103. }, [t, notify, handleCheckPluginDependencies, isCurrentWorkspaceEditor, push, isFetching])
  104. const handleImportDSLConfirm = useCallback(async (
  105. {
  106. onSuccess,
  107. onFailed,
  108. }: Pick<ResponseCallback, 'onSuccess' | 'onFailed'>,
  109. ) => {
  110. if (isFetching)
  111. return
  112. setIsFetching(true)
  113. if (!importIdRef.current)
  114. return
  115. try {
  116. const response = await importDSLConfirm({
  117. import_id: importIdRef.current,
  118. })
  119. const { status, app_id, app_mode } = response
  120. if (!app_id)
  121. return
  122. if (status === DSLImportStatus.COMPLETED) {
  123. onSuccess?.()
  124. notify({
  125. type: 'success',
  126. message: t('app.newApp.appCreated'),
  127. })
  128. await handleCheckPluginDependencies(app_id)
  129. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  130. getRedirection(isCurrentWorkspaceEditor, { id: app_id!, mode: app_mode }, push)
  131. }
  132. else if (status === DSLImportStatus.FAILED) {
  133. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  134. onFailed?.()
  135. }
  136. }
  137. catch {
  138. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  139. onFailed?.()
  140. }
  141. finally {
  142. setIsFetching(false)
  143. }
  144. }, [t, notify, handleCheckPluginDependencies, isCurrentWorkspaceEditor, push, isFetching])
  145. return {
  146. handleImportDSL,
  147. handleImportDSLConfirm,
  148. versions,
  149. isFetching,
  150. }
  151. }