loop-result-panel.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. RiArrowLeftLine,
  7. RiArrowRightSLine,
  8. RiErrorWarningLine,
  9. RiLoader2Line,
  10. } from '@remixicon/react'
  11. import { NodeRunningStatus } from '@/app/components/workflow/types'
  12. import TracingPanel from '@/app/components/workflow/run/tracing-panel'
  13. import { Loop } from '@/app/components/base/icons/src/vender/workflow'
  14. import cn from '@/utils/classnames'
  15. import type { LoopDurationMap, NodeTracing } from '@/types/workflow'
  16. const i18nPrefix = 'workflow.singleRun'
  17. type Props = {
  18. list: NodeTracing[][]
  19. onBack: () => void
  20. loopDurationMap?: LoopDurationMap
  21. }
  22. const LoopResultPanel: FC<Props> = ({
  23. list,
  24. onBack,
  25. loopDurationMap,
  26. }) => {
  27. const { t } = useTranslation()
  28. const [expandedLoops, setExpandedLoops] = useState<Record<number, boolean>>({})
  29. const toggleLoop = useCallback((index: number) => {
  30. setExpandedLoops(prev => ({
  31. ...prev,
  32. [index]: !prev[index],
  33. }))
  34. }, [])
  35. const countLoopDuration = (loop: NodeTracing[], loopDurationMap: LoopDurationMap): string => {
  36. const loopRunIndex = loop[0]?.execution_metadata?.loop_index as number
  37. const loopRunId = loop[0]?.execution_metadata?.parallel_mode_run_id
  38. const loopItem = loopDurationMap[loopRunId || loopRunIndex]
  39. const duration = loopItem
  40. return `${(duration && duration > 0.01) ? duration.toFixed(2) : 0.01}s`
  41. }
  42. const loopStatusShow = (index: number, loop: NodeTracing[], loopDurationMap?: LoopDurationMap) => {
  43. const hasFailed = loop.some(item => item.status === NodeRunningStatus.Failed)
  44. const isRunning = loop.some(item => item.status === NodeRunningStatus.Running)
  45. const hasDurationMap = loopDurationMap && Object.keys(loopDurationMap).length !== 0
  46. if (hasFailed)
  47. return <RiErrorWarningLine className='h-4 w-4 text-text-destructive' />
  48. if (isRunning)
  49. return <RiLoader2Line className='h-3.5 w-3.5 animate-spin text-primary-600' />
  50. return (
  51. <>
  52. {hasDurationMap && (
  53. <div className='system-xs-regular text-text-tertiary'>
  54. {countLoopDuration(loop, loopDurationMap)}
  55. </div>
  56. )}
  57. <RiArrowRightSLine
  58. className={cn(
  59. 'h-4 w-4 shrink-0 text-text-tertiary transition-transform duration-200',
  60. expandedLoops[index] && 'rotate-90',
  61. )}
  62. />
  63. </>
  64. )
  65. }
  66. return (
  67. <div className='bg-components-panel-bg'>
  68. <div
  69. className='flex h-8 cursor-pointer items-center border-b-[0.5px] border-b-divider-regular px-4 text-text-accent-secondary'
  70. onClick={(e) => {
  71. e.stopPropagation()
  72. e.nativeEvent.stopImmediatePropagation()
  73. onBack()
  74. }}
  75. >
  76. <RiArrowLeftLine className='mr-1 h-4 w-4' />
  77. <div className='system-sm-medium'>{t(`${i18nPrefix}.back`)}</div>
  78. </div>
  79. {/* List */}
  80. <div className='bg-components-panel-bg p-2'>
  81. {list.map((loop, index) => (
  82. <div key={index} className={cn('mb-1 overflow-hidden rounded-xl border-none bg-background-section-burn')}>
  83. <div
  84. className={cn(
  85. 'flex w-full cursor-pointer items-center justify-between px-3',
  86. expandedLoops[index] ? 'pb-2 pt-3' : 'py-3',
  87. 'rounded-xl text-left',
  88. )}
  89. onClick={() => toggleLoop(index)}
  90. >
  91. <div className={cn('flex grow items-center gap-2')}>
  92. <div className='flex h-4 w-4 shrink-0 items-center justify-center rounded-[5px] border-divider-subtle bg-util-colors-cyan-cyan-500'>
  93. <Loop className='h-3 w-3 text-text-primary-on-surface' />
  94. </div>
  95. <span className='system-sm-semibold-uppercase grow text-text-primary'>
  96. {t(`${i18nPrefix}.loop`)} {index + 1}
  97. </span>
  98. {loopStatusShow(index, loop, loopDurationMap)}
  99. </div>
  100. </div>
  101. {expandedLoops[index] && <div
  102. className="h-px grow bg-divider-subtle"
  103. ></div>}
  104. <div className={cn(
  105. 'overflow-hidden transition-all duration-200',
  106. expandedLoops[index] ? 'max-h-[1000px] opacity-100' : 'max-h-0 opacity-0',
  107. )}>
  108. <TracingPanel
  109. list={loop}
  110. className='bg-background-section-burn'
  111. />
  112. </div>
  113. </div>
  114. ))}
  115. </div>
  116. </div>
  117. )
  118. }
  119. export default React.memo(LoopResultPanel)