2
0
Files
bot/apps/builder/components/shared/Graph/Endpoints/SourceEndpoint.tsx

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-01-19 09:44:21 +01:00
import { Box, BoxProps, Flex } from '@chakra-ui/react'
import { useGraph } from 'contexts/GraphContext'
import { Source } from 'models'
import React, { MouseEvent, useEffect, useRef, useState } from 'react'
2021-12-16 10:43:49 +01:00
export const SourceEndpoint = ({
2022-01-12 09:10:59 +01:00
source,
2021-12-16 10:43:49 +01:00
...props
}: BoxProps & {
source: Source
2021-12-16 10:43:49 +01:00
}) => {
const [ranOnce, setRanOnce] = useState(false)
const { setConnectingIds, addSourceEndpoint, blocksCoordinates } = useGraph()
2022-01-15 17:30:20 +01:00
const ref = useRef<HTMLDivElement | null>(null)
2022-01-12 09:10:59 +01:00
2021-12-16 10:43:49 +01:00
const handleMouseDown = (e: MouseEvent<HTMLDivElement>) => {
e.stopPropagation()
2022-01-12 09:10:59 +01:00
setConnectingIds({ source })
2021-12-16 10:43:49 +01:00
}
2022-01-15 17:30:20 +01:00
useEffect(() => {
if (ranOnce || !ref.current || Object.keys(blocksCoordinates).length === 0)
return
const id = source.itemId ?? source.stepId
addSourceEndpoint({
2022-01-15 17:30:20 +01:00
id,
ref,
})
setRanOnce(true)
2022-01-15 17:30:20 +01:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ref.current, blocksCoordinates])
2022-01-15 17:30:20 +01:00
if (!blocksCoordinates) return <></>
2021-12-16 10:43:49 +01:00
return (
2022-01-19 09:44:21 +01:00
<Flex
2022-01-15 17:30:20 +01:00
ref={ref}
data-testid="endpoint"
2022-01-19 09:44:21 +01:00
boxSize="18px"
2021-12-16 10:43:49 +01:00
rounded="full"
onMouseDownCapture={handleMouseDown}
2022-01-19 09:44:21 +01:00
cursor="copy"
borderWidth="1px"
borderColor="gray.400"
bgColor="white"
justify="center"
align="center"
pointerEvents="all"
2021-12-16 10:43:49 +01:00
{...props}
2022-01-19 09:44:21 +01:00
>
<Box bgColor="gray.400" rounded="full" boxSize="6px" />
2022-01-19 09:44:21 +01:00
</Flex>
2021-12-16 10:43:49 +01:00
)
}