loop-log-trigger.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { useTranslation } from 'react-i18next'
  2. import { RiArrowRightSLine } from '@remixicon/react'
  3. import Button from '@/app/components/base/button'
  4. import type {
  5. LoopDurationMap,
  6. NodeTracing,
  7. } from '@/types/workflow'
  8. import { Loop } from '@/app/components/base/icons/src/vender/workflow'
  9. type LoopLogTriggerProps = {
  10. nodeInfo: NodeTracing
  11. onShowLoopResultList: (loopResultList: NodeTracing[][], loopResultDurationMap: LoopDurationMap) => void
  12. }
  13. const LoopLogTrigger = ({
  14. nodeInfo,
  15. onShowLoopResultList,
  16. }: LoopLogTriggerProps) => {
  17. const { t } = useTranslation()
  18. const getErrorCount = (details: NodeTracing[][] | undefined) => {
  19. if (!details || details.length === 0)
  20. return 0
  21. return details.reduce((acc, loop) => {
  22. if (loop.some(item => item.status === 'failed'))
  23. acc++
  24. return acc
  25. }, 0)
  26. }
  27. const getCount = (loop_curr_length: number | undefined, loop_length: number) => {
  28. if ((loop_curr_length && loop_curr_length < loop_length) || !loop_length)
  29. return loop_curr_length
  30. return loop_length
  31. }
  32. const handleOnShowLoopDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
  33. e.stopPropagation()
  34. e.nativeEvent.stopImmediatePropagation()
  35. onShowLoopResultList(nodeInfo.details || [], nodeInfo?.loopDurationMap || nodeInfo.execution_metadata?.loop_duration_map || {})
  36. }
  37. return (
  38. <Button
  39. className='flex w-full cursor-pointer items-center gap-2 self-stretch rounded-lg border-none bg-components-button-tertiary-bg-hover px-3 py-2 hover:bg-components-button-tertiary-bg-hover'
  40. onClick={handleOnShowLoopDetail}
  41. >
  42. <Loop className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
  43. <div className='system-sm-medium flex-1 text-left text-components-button-tertiary-text'>{t('workflow.nodes.loop.loop', { count: getCount(nodeInfo.details?.length, nodeInfo.metadata?.loop_length) })}{getErrorCount(nodeInfo.details) > 0 && (
  44. <>
  45. {t('workflow.nodes.loop.comma')}
  46. {t('workflow.nodes.loop.error', { count: getErrorCount(nodeInfo.details) })}
  47. </>
  48. )}</div>
  49. <RiArrowRightSLine className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
  50. </Button>
  51. )
  52. }
  53. export default LoopLogTrigger