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-DSL.ts 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {
  2. useCallback,
  3. useState,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. DSL_EXPORT_CHECK,
  8. } from '@/app/components/workflow/constants'
  9. import { useNodesSyncDraft } from './use-nodes-sync-draft'
  10. import { useEventEmitterContextContext } from '@/context/event-emitter'
  11. import { fetchWorkflowDraft } from '@/service/workflow'
  12. import { exportAppConfig } from '@/service/apps'
  13. import { useToastContext } from '@/app/components/base/toast'
  14. import { useStore as useAppStore } from '@/app/components/app/store'
  15. export const useDSL = () => {
  16. const { t } = useTranslation()
  17. const { notify } = useToastContext()
  18. const { eventEmitter } = useEventEmitterContextContext()
  19. const [exporting, setExporting] = useState(false)
  20. const { doSyncWorkflowDraft } = useNodesSyncDraft()
  21. const appDetail = useAppStore(s => s.appDetail)
  22. const handleExportDSL = useCallback(async (include = false, workflowId?: string) => {
  23. if (!appDetail)
  24. return
  25. if (exporting)
  26. return
  27. try {
  28. setExporting(true)
  29. await doSyncWorkflowDraft()
  30. const { data } = await exportAppConfig({
  31. appID: appDetail.id,
  32. include,
  33. workflowID: workflowId,
  34. })
  35. const a = document.createElement('a')
  36. const file = new Blob([data], { type: 'application/yaml' })
  37. const url = URL.createObjectURL(file)
  38. a.href = url
  39. a.download = `${appDetail.name}.yml`
  40. a.click()
  41. URL.revokeObjectURL(url)
  42. }
  43. catch {
  44. notify({ type: 'error', message: t('app.exportFailed') })
  45. }
  46. finally {
  47. setExporting(false)
  48. }
  49. }, [appDetail, notify, t, doSyncWorkflowDraft, exporting])
  50. const exportCheck = useCallback(async () => {
  51. if (!appDetail)
  52. return
  53. try {
  54. const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail?.id}/workflows/draft`)
  55. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  56. if (list.length === 0) {
  57. handleExportDSL()
  58. return
  59. }
  60. eventEmitter?.emit({
  61. type: DSL_EXPORT_CHECK,
  62. payload: {
  63. data: list,
  64. },
  65. } as any)
  66. }
  67. catch {
  68. notify({ type: 'error', message: t('app.exportFailed') })
  69. }
  70. }, [appDetail, eventEmitter, handleExportDSL, notify, t])
  71. return {
  72. exportCheck,
  73. handleExportDSL,
  74. }
  75. }