run-and-history.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 workflowRunningData = useStore(s => s.workflowRunningData)
  41. const isRunning = workflowRunningData?.result.status === WorkflowRunningStatus.Running
  42. const handleClick = useCallback(async () => {
  43. const {
  44. workflowRunningData,
  45. } = workflowStore.getState()
  46. if (workflowRunningData?.result.status === WorkflowRunningStatus.Running)
  47. return
  48. const { getNodes } = store.getState()
  49. const nodes = getNodes()
  50. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  51. const startVariables = startNode?.data.variables || []
  52. const fileSettings = featuresStore!.getState().features.file
  53. const {
  54. setShowDebugAndPreviewPanel,
  55. setShowInputsPanel,
  56. } = workflowStore.getState()
  57. if (!startVariables.length && !fileSettings?.image?.enabled) {
  58. await doSyncWorkflowDraft()
  59. handleRun({ inputs: {}, files: [] })
  60. setShowDebugAndPreviewPanel(true)
  61. setShowInputsPanel(false)
  62. }
  63. else {
  64. setShowDebugAndPreviewPanel(true)
  65. setShowInputsPanel(true)
  66. }
  67. }, [
  68. workflowStore,
  69. handleRun,
  70. doSyncWorkflowDraft,
  71. store,
  72. featuresStore,
  73. ])
  74. return (
  75. <>
  76. <div
  77. className={cn(
  78. 'flex items-center px-1.5 h-7 rounded-md text-[13px] font-medium text-primary-600',
  79. 'hover:bg-primary-50 cursor-pointer',
  80. isRunning && 'bg-primary-50 !cursor-not-allowed',
  81. )}
  82. onClick={handleClick}
  83. >
  84. {
  85. isRunning
  86. ? (
  87. <>
  88. <Loading02 className='mr-1 w-4 h-4 animate-spin' />
  89. {t('workflow.common.running')}
  90. </>
  91. )
  92. : (
  93. <>
  94. <Play className='mr-1 w-4 h-4' />
  95. {t('workflow.common.run')}
  96. </>
  97. )
  98. }
  99. </div>
  100. {
  101. isRunning && (
  102. <div
  103. className='flex items-center justify-center ml-0.5 w-7 h-7 cursor-pointer hover:bg-black/5 rounded-md'
  104. onClick={() => handleStopRun(workflowRunningData?.task_id || '')}
  105. >
  106. <StopCircle className='w-4 h-4 text-gray-500' />
  107. </div>
  108. )
  109. }
  110. </>
  111. )
  112. })
  113. RunMode.displayName = 'RunMode'
  114. const PreviewMode = memo(() => {
  115. const { t } = useTranslation()
  116. const workflowStore = useWorkflowStore()
  117. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  118. const handleClick = () => {
  119. const {
  120. showDebugAndPreviewPanel,
  121. setShowDebugAndPreviewPanel,
  122. setHistoryWorkflowData,
  123. } = workflowStore.getState()
  124. if (showDebugAndPreviewPanel)
  125. handleCancelDebugAndPreviewPanel()
  126. else
  127. setShowDebugAndPreviewPanel(true)
  128. setHistoryWorkflowData(undefined)
  129. }
  130. return (
  131. <div
  132. className={cn(
  133. 'flex items-center px-1.5 h-7 rounded-md text-[13px] font-medium text-primary-600',
  134. 'hover:bg-primary-50 cursor-pointer',
  135. )}
  136. onClick={() => handleClick()}
  137. >
  138. <MessagePlay className='mr-1 w-4 h-4' />
  139. {t('workflow.common.debugAndPreview')}
  140. </div>
  141. )
  142. })
  143. PreviewMode.displayName = 'PreviewMode'
  144. const RunAndHistory: FC = () => {
  145. const isChatMode = useIsChatMode()
  146. return (
  147. <div className='flex items-center px-0.5 h-8 rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xs'>
  148. {
  149. !isChatMode && <RunMode />
  150. }
  151. {
  152. isChatMode && <PreviewMode />
  153. }
  154. <div className='mx-0.5 w-[0.5px] h-8 bg-gray-200'></div>
  155. <ViewHistory />
  156. </div>
  157. )
  158. }
  159. export default memo(RunAndHistory)