workflow-process.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. useState,
  6. } from 'react'
  7. import cn from 'classnames'
  8. import { useTranslation } from 'react-i18next'
  9. import type { ChatItem, WorkflowProcess } from '../../types'
  10. import { CheckCircle } from '@/app/components/base/icons/src/vender/solid/general'
  11. import { AlertCircle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback'
  12. import { Loading02 } from '@/app/components/base/icons/src/vender/line/general'
  13. import { ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
  14. import { WorkflowRunningStatus } from '@/app/components/workflow/types'
  15. import NodePanel from '@/app/components/workflow/run/node'
  16. import { useStore as useAppStore } from '@/app/components/app/store'
  17. type WorkflowProcessProps = {
  18. data: WorkflowProcess
  19. item?: ChatItem
  20. grayBg?: boolean
  21. expand?: boolean
  22. hideInfo?: boolean
  23. hideProcessDetail?: boolean
  24. }
  25. const WorkflowProcessItem = ({
  26. data,
  27. item,
  28. grayBg,
  29. expand = false,
  30. hideInfo = false,
  31. hideProcessDetail = false,
  32. }: WorkflowProcessProps) => {
  33. const { t } = useTranslation()
  34. const [collapse, setCollapse] = useState(!expand)
  35. const running = data.status === WorkflowRunningStatus.Running
  36. const succeeded = data.status === WorkflowRunningStatus.Succeeded
  37. const failed = data.status === WorkflowRunningStatus.Failed || data.status === WorkflowRunningStatus.Stopped
  38. const background = useMemo(() => {
  39. if (running && !collapse)
  40. return 'linear-gradient(180deg, #E1E4EA 0%, #EAECF0 100%)'
  41. if (succeeded && !collapse)
  42. return 'linear-gradient(180deg, #ECFDF3 0%, #F6FEF9 100%)'
  43. if (failed && !collapse)
  44. return 'linear-gradient(180deg, #FEE4E2 0%, #FEF3F2 100%)'
  45. }, [running, succeeded, failed, collapse])
  46. useEffect(() => {
  47. setCollapse(!expand)
  48. }, [expand])
  49. const setCurrentLogItem = useAppStore(s => s.setCurrentLogItem)
  50. const setShowMessageLogModal = useAppStore(s => s.setShowMessageLogModal)
  51. const setCurrentLogModalActiveTab = useAppStore(s => s.setCurrentLogModalActiveTab)
  52. const showIterationDetail = useCallback(() => {
  53. setCurrentLogItem(item)
  54. setCurrentLogModalActiveTab('TRACING')
  55. setShowMessageLogModal(true)
  56. }, [item, setCurrentLogItem, setCurrentLogModalActiveTab, setShowMessageLogModal])
  57. return (
  58. <div
  59. className={cn(
  60. 'mb-2 rounded-xl border-[0.5px] border-black/[0.08]',
  61. collapse ? 'py-[7px]' : hideInfo ? 'pt-2 pb-1' : 'py-2',
  62. collapse && (!grayBg ? 'bg-white' : 'bg-gray-50'),
  63. hideInfo ? 'mx-[-8px] px-1' : 'w-full px-3',
  64. )}
  65. style={{
  66. background,
  67. }}
  68. >
  69. <div
  70. className={cn(
  71. 'flex items-center h-[18px] cursor-pointer',
  72. hideInfo && 'px-[6px]',
  73. )}
  74. onClick={() => setCollapse(!collapse)}
  75. >
  76. {
  77. running && (
  78. <Loading02 className='shrink-0 mr-1 w-3 h-3 text-[#667085] animate-spin' />
  79. )
  80. }
  81. {
  82. succeeded && (
  83. <CheckCircle className='shrink-0 mr-1 w-3 h-3 text-[#12B76A]' />
  84. )
  85. }
  86. {
  87. failed && (
  88. <AlertCircle className='shrink-0 mr-1 w-3 h-3 text-[#F04438]' />
  89. )
  90. }
  91. <div className='grow text-xs font-medium text-gray-700'>
  92. {t('workflow.common.workflowProcess')}
  93. </div>
  94. <ChevronRight className={`'ml-1 w-3 h-3 text-gray-500' ${collapse ? '' : 'rotate-90'}`} />
  95. </div>
  96. {
  97. !collapse && (
  98. <div className='mt-1.5'>
  99. {
  100. data.tracing.map(node => (
  101. <div key={node.id} className='mb-1 last-of-type:mb-0'>
  102. <NodePanel
  103. nodeInfo={node}
  104. hideInfo={hideInfo}
  105. hideProcessDetail={hideProcessDetail}
  106. onShowIterationDetail={showIterationDetail}
  107. />
  108. </div>
  109. ))
  110. }
  111. </div>
  112. )
  113. }
  114. </div>
  115. )
  116. }
  117. export default WorkflowProcessItem