Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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) => {
  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. })
  34. const a = document.createElement('a')
  35. const file = new Blob([data], { type: 'application/yaml' })
  36. a.href = URL.createObjectURL(file)
  37. a.download = `${appDetail.name}.yml`
  38. a.click()
  39. }
  40. catch {
  41. notify({ type: 'error', message: t('app.exportFailed') })
  42. }
  43. finally {
  44. setExporting(false)
  45. }
  46. }, [appDetail, notify, t, doSyncWorkflowDraft, exporting])
  47. const exportCheck = useCallback(async () => {
  48. if (!appDetail)
  49. return
  50. try {
  51. const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail?.id}/workflows/draft`)
  52. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  53. if (list.length === 0) {
  54. handleExportDSL()
  55. return
  56. }
  57. eventEmitter?.emit({
  58. type: DSL_EXPORT_CHECK,
  59. payload: {
  60. data: list,
  61. },
  62. } as any)
  63. }
  64. catch {
  65. notify({ type: 'error', message: t('app.exportFailed') })
  66. }
  67. }, [appDetail, eventEmitter, handleExportDSL, notify, t])
  68. return {
  69. exportCheck,
  70. handleExportDSL,
  71. }
  72. }