You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-import-dsl.ts 4.2KB

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