panel-contextmenu.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. memo,
  3. useRef,
  4. } from 'react'
  5. import cn from 'classnames'
  6. import { useTranslation } from 'react-i18next'
  7. import { useClickAway } from 'ahooks'
  8. import ShortcutsName from './shortcuts-name'
  9. import { useStore } from './store'
  10. import {
  11. useNodesInteractions,
  12. usePanelInteractions,
  13. useWorkflowStartRun,
  14. } from './hooks'
  15. import AddBlock from './operator/add-block'
  16. import { exportAppConfig } from '@/service/apps'
  17. import { useToastContext } from '@/app/components/base/toast'
  18. import { useStore as useAppStore } from '@/app/components/app/store'
  19. const PanelContextmenu = () => {
  20. const { t } = useTranslation()
  21. const { notify } = useToastContext()
  22. const ref = useRef(null)
  23. const panelMenu = useStore(s => s.panelMenu)
  24. const clipboardElements = useStore(s => s.clipboardElements)
  25. const appDetail = useAppStore(s => s.appDetail)
  26. const { handleNodesPaste } = useNodesInteractions()
  27. const { handlePaneContextmenuCancel } = usePanelInteractions()
  28. const { handleStartWorkflowRun } = useWorkflowStartRun()
  29. useClickAway(() => {
  30. handlePaneContextmenuCancel()
  31. }, ref)
  32. const onExport = async () => {
  33. if (!appDetail)
  34. return
  35. try {
  36. const { data } = await exportAppConfig(appDetail.id)
  37. const a = document.createElement('a')
  38. const file = new Blob([data], { type: 'application/yaml' })
  39. a.href = URL.createObjectURL(file)
  40. a.download = `${appDetail.name}.yml`
  41. a.click()
  42. }
  43. catch (e) {
  44. notify({ type: 'error', message: t('app.exportFailed') })
  45. }
  46. }
  47. const renderTrigger = () => {
  48. return (
  49. <div
  50. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  51. >
  52. {t('workflow.common.addBlock')}
  53. </div>
  54. )
  55. }
  56. if (!panelMenu)
  57. return null
  58. return (
  59. <div
  60. className='absolute w-[200px] rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xl z-[9]'
  61. style={{
  62. left: panelMenu.left,
  63. top: panelMenu.top,
  64. }}
  65. ref={ref}
  66. >
  67. <div className='p-1'>
  68. <AddBlock
  69. renderTrigger={renderTrigger}
  70. offset={{
  71. mainAxis: -36,
  72. crossAxis: -4,
  73. }}
  74. />
  75. <div
  76. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  77. onClick={() => {
  78. handleStartWorkflowRun()
  79. handlePaneContextmenuCancel()
  80. }}
  81. >
  82. {t('workflow.common.run')}
  83. <ShortcutsName keys={['alt', 'r']} />
  84. </div>
  85. </div>
  86. <div className='h-[1px] bg-gray-100'></div>
  87. <div className='p-1'>
  88. <div
  89. className={cn(
  90. 'flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer',
  91. !clipboardElements.length ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-50',
  92. )}
  93. onClick={() => {
  94. if (clipboardElements.length) {
  95. handleNodesPaste()
  96. handlePaneContextmenuCancel()
  97. }
  98. }}
  99. >
  100. {t('workflow.common.pasteHere')}
  101. <ShortcutsName keys={['ctrl', 'v']} />
  102. </div>
  103. </div>
  104. <div className='h-[1px] bg-gray-100'></div>
  105. <div className='p-1'>
  106. <div
  107. className='flex items-center justify-between px-3 h-8 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  108. onClick={() => onExport()}
  109. >
  110. {t('app.export')}
  111. </div>
  112. </div>
  113. </div>
  114. )
  115. }
  116. export default memo(PanelContextmenu)