run-and-history.tsx 4.7 KB

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