run-and-history.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import type { FC } from 'react'
  2. import { memo, useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useStoreApi } from 'reactflow'
  5. import cn from 'classnames'
  6. import {
  7. useStore,
  8. useWorkflowStore,
  9. } from '../store'
  10. import {
  11. useIsChatMode,
  12. useNodesSyncDraft,
  13. useWorkflowInteractions,
  14. useWorkflowRun,
  15. } from '../hooks'
  16. import {
  17. BlockEnum,
  18. WorkflowRunningStatus,
  19. } from '../types'
  20. import ViewHistory from './view-history'
  21. import {
  22. Play,
  23. StopCircle,
  24. } from '@/app/components/base/icons/src/vender/line/mediaAndDevices'
  25. import { Loading02 } from '@/app/components/base/icons/src/vender/line/general'
  26. import { useFeaturesStore } from '@/app/components/base/features/hooks'
  27. import { MessagePlay } from '@/app/components/base/icons/src/vender/line/communication'
  28. const RunMode = memo(() => {
  29. const { t } = useTranslation()
  30. const store = useStoreApi()
  31. const workflowStore = useWorkflowStore()
  32. const featuresStore = useFeaturesStore()
  33. const {
  34. handleStopRun,
  35. handleRun,
  36. } = useWorkflowRun()
  37. const {
  38. doSyncWorkflowDraft,
  39. } = useNodesSyncDraft()
  40. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  41. const workflowRunningData = useStore(s => s.workflowRunningData)
  42. const isRunning = workflowRunningData?.result.status === WorkflowRunningStatus.Running
  43. const handleClick = useCallback(async () => {
  44. const {
  45. workflowRunningData,
  46. } = workflowStore.getState()
  47. if (workflowRunningData?.result.status === WorkflowRunningStatus.Running)
  48. return
  49. const { getNodes } = store.getState()
  50. const nodes = getNodes()
  51. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  52. const startVariables = startNode?.data.variables || []
  53. const fileSettings = featuresStore!.getState().features.file
  54. const {
  55. showDebugAndPreviewPanel,
  56. setShowDebugAndPreviewPanel,
  57. setShowInputsPanel,
  58. } = workflowStore.getState()
  59. if (showDebugAndPreviewPanel) {
  60. handleCancelDebugAndPreviewPanel()
  61. return
  62. }
  63. if (!startVariables.length && !fileSettings?.image?.enabled) {
  64. await doSyncWorkflowDraft()
  65. handleRun({ inputs: {}, files: [] })
  66. setShowDebugAndPreviewPanel(true)
  67. setShowInputsPanel(false)
  68. }
  69. else {
  70. setShowDebugAndPreviewPanel(true)
  71. setShowInputsPanel(true)
  72. }
  73. }, [
  74. workflowStore,
  75. handleRun,
  76. doSyncWorkflowDraft,
  77. store,
  78. featuresStore,
  79. handleCancelDebugAndPreviewPanel,
  80. ])
  81. return (
  82. <>
  83. <div
  84. className={cn(
  85. 'flex items-center px-1.5 h-7 rounded-md text-[13px] font-medium text-primary-600',
  86. 'hover:bg-primary-50 cursor-pointer',
  87. isRunning && 'bg-primary-50 !cursor-not-allowed',
  88. )}
  89. onClick={handleClick}
  90. >
  91. {
  92. isRunning
  93. ? (
  94. <>
  95. <Loading02 className='mr-1 w-4 h-4 animate-spin' />
  96. {t('workflow.common.running')}
  97. </>
  98. )
  99. : (
  100. <>
  101. <Play className='mr-1 w-4 h-4' />
  102. {t('workflow.common.run')}
  103. </>
  104. )
  105. }
  106. </div>
  107. {
  108. isRunning && (
  109. <div
  110. className='flex items-center justify-center ml-0.5 w-7 h-7 cursor-pointer hover:bg-black/5 rounded-md'
  111. onClick={() => handleStopRun(workflowRunningData?.task_id || '')}
  112. >
  113. <StopCircle className='w-4 h-4 text-gray-500' />
  114. </div>
  115. )
  116. }
  117. </>
  118. )
  119. })
  120. RunMode.displayName = 'RunMode'
  121. const PreviewMode = memo(() => {
  122. const { t } = useTranslation()
  123. const workflowStore = useWorkflowStore()
  124. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  125. const handleClick = () => {
  126. const {
  127. showDebugAndPreviewPanel,
  128. setShowDebugAndPreviewPanel,
  129. setHistoryWorkflowData,
  130. } = workflowStore.getState()
  131. if (showDebugAndPreviewPanel)
  132. handleCancelDebugAndPreviewPanel()
  133. else
  134. setShowDebugAndPreviewPanel(true)
  135. setHistoryWorkflowData(undefined)
  136. }
  137. return (
  138. <div
  139. className={cn(
  140. 'flex items-center px-1.5 h-7 rounded-md text-[13px] font-medium text-primary-600',
  141. 'hover:bg-primary-50 cursor-pointer',
  142. )}
  143. onClick={() => handleClick()}
  144. >
  145. <MessagePlay className='mr-1 w-4 h-4' />
  146. {t('workflow.common.debugAndPreview')}
  147. </div>
  148. )
  149. })
  150. PreviewMode.displayName = 'PreviewMode'
  151. const RunAndHistory: FC = () => {
  152. const isChatMode = useIsChatMode()
  153. return (
  154. <div className='flex items-center px-0.5 h-8 rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xs'>
  155. {
  156. !isChatMode && <RunMode />
  157. }
  158. {
  159. isChatMode && <PreviewMode />
  160. }
  161. <div className='mx-0.5 w-[0.5px] h-8 bg-gray-200'></div>
  162. <ViewHistory />
  163. </div>
  164. )
  165. }
  166. export default memo(RunAndHistory)