workflow-preview.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. memo,
  3. useEffect,
  4. // useRef,
  5. useState,
  6. } from 'react'
  7. import cn from 'classnames'
  8. import { useTranslation } from 'react-i18next'
  9. import ResultText from '../run/result-text'
  10. import ResultPanel from '../run/result-panel'
  11. import TracingPanel from '../run/tracing-panel'
  12. import {
  13. useWorkflowInteractions,
  14. } from '../hooks'
  15. import { useStore } from '../store'
  16. import {
  17. WorkflowRunningStatus,
  18. } from '../types'
  19. import InputsPanel from './inputs-panel'
  20. import Loading from '@/app/components/base/loading'
  21. import { XClose } from '@/app/components/base/icons/src/vender/line/general'
  22. const WorkflowPreview = () => {
  23. const { t } = useTranslation()
  24. const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions()
  25. const workflowRunningData = useStore(s => s.workflowRunningData)
  26. const showInputsPanel = useStore(s => s.showInputsPanel)
  27. const showDebugAndPreviewPanel = useStore(s => s.showDebugAndPreviewPanel)
  28. const [currentTab, setCurrentTab] = useState<string>(showInputsPanel ? 'INPUT' : 'TRACING')
  29. const switchTab = async (tab: string) => {
  30. setCurrentTab(tab)
  31. }
  32. useEffect(() => {
  33. if (showDebugAndPreviewPanel && showInputsPanel)
  34. setCurrentTab('INPUT')
  35. }, [showDebugAndPreviewPanel, showInputsPanel])
  36. useEffect(() => {
  37. if ((workflowRunningData?.result.status === WorkflowRunningStatus.Succeeded || workflowRunningData?.result.status === WorkflowRunningStatus.Failed) && !workflowRunningData.resultText)
  38. switchTab('DETAIL')
  39. }, [workflowRunningData])
  40. return (
  41. <div className={`
  42. flex flex-col w-[420px] h-full rounded-l-2xl border-[0.5px] border-gray-200 shadow-xl bg-white
  43. `}>
  44. <div className='flex items-center justify-between p-4 pb-1 text-base font-semibold text-gray-900'>
  45. {`Test Run${!workflowRunningData?.result.sequence_number ? '' : `#${workflowRunningData?.result.sequence_number}`}`}
  46. <div className='p-1 cursor-pointer' onClick={() => handleCancelDebugAndPreviewPanel()}>
  47. <XClose className='w-4 h-4 text-gray-500' />
  48. </div>
  49. </div>
  50. <div className='grow relative flex flex-col'>
  51. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  52. {showInputsPanel && (
  53. <div
  54. className={cn(
  55. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  56. currentTab === 'INPUT' && '!border-[rgb(21,94,239)] text-gray-700',
  57. )}
  58. onClick={() => switchTab('INPUT')}
  59. >{t('runLog.input')}</div>
  60. )}
  61. <div
  62. className={cn(
  63. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  64. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  65. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  66. )}
  67. onClick={() => {
  68. if (!workflowRunningData)
  69. return
  70. switchTab('RESULT')
  71. }}
  72. >{t('runLog.result')}</div>
  73. <div
  74. className={cn(
  75. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  76. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  77. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  78. )}
  79. onClick={() => {
  80. if (!workflowRunningData)
  81. return
  82. switchTab('DETAIL')
  83. }}
  84. >{t('runLog.detail')}</div>
  85. <div
  86. className={cn(
  87. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  88. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  89. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  90. )}
  91. onClick={() => {
  92. if (!workflowRunningData)
  93. return
  94. switchTab('TRACING')
  95. }}
  96. >{t('runLog.tracing')}</div>
  97. </div>
  98. <div className={cn(
  99. 'grow bg-white h-0 overflow-y-auto rounded-b-2xl',
  100. (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-gray-50',
  101. )}>
  102. {currentTab === 'INPUT' && showInputsPanel && (
  103. <InputsPanel onRun={() => switchTab('RESULT')} />
  104. )}
  105. {currentTab === 'RESULT' && (
  106. <ResultText
  107. isRunning={workflowRunningData?.result?.status === WorkflowRunningStatus.Running || !workflowRunningData?.result}
  108. outputs={workflowRunningData?.resultText}
  109. error={workflowRunningData?.result?.error}
  110. onClick={() => switchTab('DETAIL')}
  111. />
  112. )}
  113. {currentTab === 'DETAIL' && (
  114. <ResultPanel
  115. inputs={workflowRunningData?.result?.inputs}
  116. outputs={workflowRunningData?.result?.outputs}
  117. status={workflowRunningData?.result?.status || ''}
  118. error={workflowRunningData?.result?.error}
  119. elapsed_time={workflowRunningData?.result?.elapsed_time}
  120. total_tokens={workflowRunningData?.result?.total_tokens}
  121. created_at={workflowRunningData?.result?.created_at}
  122. created_by={(workflowRunningData?.result?.created_by as any)?.name}
  123. steps={workflowRunningData?.result?.total_steps}
  124. />
  125. )}
  126. {currentTab === 'DETAIL' && !workflowRunningData?.result && (
  127. <div className='flex h-full items-center justify-center bg-white'>
  128. <Loading />
  129. </div>
  130. )}
  131. {currentTab === 'TRACING' && (
  132. <TracingPanel
  133. list={workflowRunningData?.tracing || []}
  134. />
  135. )}
  136. {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && (
  137. <div className='flex h-full items-center justify-center bg-gray-50'>
  138. <Loading />
  139. </div>
  140. )}
  141. </div>
  142. </div>
  143. </div>
  144. )
  145. }
  146. export default memo(WorkflowPreview)