node.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useEffect,
  5. } from 'react'
  6. import {
  7. Background,
  8. useNodesInitialized,
  9. useViewport,
  10. } from 'reactflow'
  11. import { useTranslation } from 'react-i18next'
  12. import { IterationStartNodeDumb } from '../iteration-start'
  13. import { useNodeIterationInteractions } from './use-interactions'
  14. import type { IterationNodeType } from './types'
  15. import AddBlock from './add-block'
  16. import cn from '@/utils/classnames'
  17. import type { NodeProps } from '@/app/components/workflow/types'
  18. import Toast from '@/app/components/base/toast'
  19. const i18nPrefix = 'workflow.nodes.iteration'
  20. const Node: FC<NodeProps<IterationNodeType>> = ({
  21. id,
  22. data,
  23. }) => {
  24. const { zoom } = useViewport()
  25. const nodesInitialized = useNodesInitialized()
  26. const { handleNodeIterationRerender } = useNodeIterationInteractions()
  27. const { t } = useTranslation()
  28. useEffect(() => {
  29. if (nodesInitialized)
  30. handleNodeIterationRerender(id)
  31. if (data.is_parallel && data._isShowTips) {
  32. Toast.notify({
  33. type: 'warning',
  34. message: t(`${i18nPrefix}.answerNodeWarningDesc`),
  35. duration: 5000,
  36. })
  37. data._isShowTips = false
  38. }
  39. }, [nodesInitialized, id, handleNodeIterationRerender, data, t])
  40. return (
  41. <div className={cn(
  42. 'relative min-w-[240px] min-h-[90px] w-full h-full rounded-2xl bg-[#F0F2F7]/90',
  43. )}>
  44. <Background
  45. id={`iteration-background-${id}`}
  46. className='rounded-2xl !z-0'
  47. gap={[14 / zoom, 14 / zoom]}
  48. size={2 / zoom}
  49. color='#E4E5E7'
  50. />
  51. {
  52. data._isCandidate && (
  53. <IterationStartNodeDumb />
  54. )
  55. }
  56. {
  57. data._children!.length === 1 && (
  58. <AddBlock
  59. iterationNodeId={id}
  60. iterationNodeData={data}
  61. />
  62. )
  63. }
  64. </div>
  65. )
  66. }
  67. export default memo(Node)