2
0
Files
bot/apps/builder/components/board/graph/BlockNode/StepNode/SourceEndpoint.tsx

42 lines
988 B
TypeScript
Raw Normal View History

2021-12-16 10:43:49 +01:00
import { Box, BoxProps } from '@chakra-ui/react'
2022-01-12 09:10:59 +01:00
import { ConnectingSourceIds, useGraph } from 'contexts/GraphContext'
2022-01-15 17:30:20 +01:00
import React, { MouseEvent, useEffect, useRef } 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 & {
2022-01-12 09:10:59 +01:00
source: ConnectingSourceIds
2021-12-16 10:43:49 +01:00
}) => {
2022-01-15 17:30:20 +01:00
const { setConnectingIds, addSourceEndpoint: addEndpoint } = useGraph()
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 (!ref.current) return
const id =
source.choiceItemId ?? source.stepId + (source.conditionType ?? '')
addEndpoint({
id,
ref,
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ref])
2021-12-16 10:43:49 +01:00
return (
<Box
2022-01-15 17:30:20 +01:00
ref={ref}
2021-12-16 10:43:49 +01:00
boxSize="15px"
rounded="full"
bgColor="gray.500"
onMouseDown={handleMouseDown}
cursor="pointer"
{...props}
/>
)
}