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

75 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-03-31 09:49:23 +02:00
import { 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)
2022-03-31 09:49:23 +02:00
const {
setConnectingIds,
addSourceEndpoint,
blocksCoordinates,
previewingEdge,
} = 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-03-31 09:49:23 +02:00
boxSize="32px"
2021-12-16 10:43:49 +01:00
rounded="full"
onMouseDownCapture={handleMouseDown}
2022-01-19 09:44:21 +01:00
cursor="copy"
justify="center"
align="center"
pointerEvents="all"
2021-12-16 10:43:49 +01:00
{...props}
2022-01-19 09:44:21 +01:00
>
2022-03-31 09:49:23 +02:00
<Flex
boxSize="20px"
justify="center"
align="center"
bgColor="gray.100"
rounded="full"
>
<Flex
boxSize="13px"
rounded="full"
borderWidth="3.5px"
shadow={`sm`}
borderColor={
previewingEdge?.from.stepId === source.stepId &&
previewingEdge.from.itemId === source.itemId
? 'blue.300'
: 'blue.200'
}
/>
</Flex>
2022-01-19 09:44:21 +01:00
</Flex>
2021-12-16 10:43:49 +01:00
)
}