workflow-preview.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 OutputPanel from '../run/output-panel'
  10. import ResultPanel from '../run/result-panel'
  11. import TracingPanel from '../run/tracing-panel'
  12. import {
  13. useWorkflowRun,
  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 { handleRunSetting } = useWorkflowRun()
  25. const showInputsPanel = useStore(s => s.showInputsPanel)
  26. const workflowRunningData = useStore(s => s.workflowRunningData)
  27. const [currentTab, setCurrentTab] = useState<string>(showInputsPanel ? 'INPUT' : 'TRACING')
  28. const switchTab = async (tab: string) => {
  29. setCurrentTab(tab)
  30. }
  31. const [height, setHieght] = useState(0)
  32. const ref = useRef<HTMLDivElement>(null)
  33. const adjustResultHeight = () => {
  34. if (ref.current)
  35. setHieght(ref.current?.clientHeight - 16 - 16 - 2 - 1)
  36. }
  37. useEffect(() => {
  38. adjustResultHeight()
  39. }, [])
  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. {showInputsPanel && workflowRunningData?.result?.status !== WorkflowRunningStatus.Running && (
  47. <div className='p-1 cursor-pointer' onClick={() => handleRunSetting(true)}>
  48. <XClose className='w-4 h-4 text-gray-500' />
  49. </div>
  50. )}
  51. </div>
  52. <div className='grow relative flex flex-col'>
  53. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  54. {showInputsPanel && (
  55. <div
  56. className={cn(
  57. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  58. currentTab === 'INPUT' && '!border-[rgb(21,94,239)] text-gray-700',
  59. )}
  60. onClick={() => switchTab('INPUT')}
  61. >{t('runLog.input')}</div>
  62. )}
  63. <div
  64. className={cn(
  65. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  66. currentTab === 'RESULT' && '!border-[rgb(21,94,239)] text-gray-700',
  67. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  68. )}
  69. onClick={() => {
  70. if (!workflowRunningData)
  71. return
  72. switchTab('RESULT')
  73. }}
  74. >{t('runLog.result')}</div>
  75. <div
  76. className={cn(
  77. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  78. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  79. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  80. )}
  81. onClick={() => {
  82. if (!workflowRunningData)
  83. return
  84. switchTab('DETAIL')
  85. }}
  86. >{t('runLog.detail')}</div>
  87. <div
  88. className={cn(
  89. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  90. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  91. !workflowRunningData && 'opacity-30 !cursor-not-allowed',
  92. )}
  93. onClick={() => {
  94. if (!workflowRunningData)
  95. return
  96. switchTab('TRACING')
  97. }}
  98. >{t('runLog.tracing')}</div>
  99. </div>
  100. <div ref={ref} className={cn(
  101. 'grow bg-white h-0 overflow-y-auto rounded-b-2xl',
  102. (currentTab === 'RESULT' || currentTab === 'TRACING') && '!bg-gray-50',
  103. )}>
  104. {currentTab === 'INPUT' && (
  105. <InputsPanel onRun={() => switchTab('RESULT')} />
  106. )}
  107. {currentTab === 'RESULT' && (
  108. <OutputPanel
  109. isRunning={workflowRunningData?.result?.status === WorkflowRunningStatus.Running || !workflowRunningData?.result}
  110. outputs={workflowRunningData?.result?.outputs}
  111. error={workflowRunningData?.result?.error}
  112. height={height}
  113. />
  114. )}
  115. {currentTab === 'DETAIL' && (
  116. <ResultPanel
  117. inputs={workflowRunningData?.result?.inputs}
  118. outputs={workflowRunningData?.result?.outputs}
  119. status={workflowRunningData?.result?.status || ''}
  120. error={workflowRunningData?.result?.error}
  121. elapsed_time={workflowRunningData?.result?.elapsed_time}
  122. total_tokens={workflowRunningData?.result?.total_tokens}
  123. created_at={workflowRunningData?.result?.created_at}
  124. created_by={(workflowRunningData?.result?.created_by as any)?.name}
  125. steps={workflowRunningData?.result?.total_steps}
  126. />
  127. )}
  128. {currentTab === 'DETAIL' && !workflowRunningData?.result && (
  129. <div className='flex h-full items-center justify-center bg-white'>
  130. <Loading />
  131. </div>
  132. )}
  133. {currentTab === 'TRACING' && (
  134. <TracingPanel
  135. list={workflowRunningData?.tracing || []}
  136. />
  137. )}
  138. {currentTab === 'TRACING' && !workflowRunningData?.tracing?.length && (
  139. <div className='flex h-full items-center justify-center bg-gray-50'>
  140. <Loading />
  141. </div>
  142. )}
  143. </div>
  144. </div>
  145. </div>
  146. )
  147. }
  148. export default memo(WorkflowPreview)