2021-12-16 10:43:49 +01:00
|
|
|
import { useEventListener } from '@chakra-ui/hooks'
|
2021-12-22 14:59:07 +01:00
|
|
|
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
|
2022-01-12 09:10:59 +01:00
|
|
|
import { useGraph, ConnectingIds } from 'contexts/GraphContext'
|
2022-01-06 09:40:56 +01:00
|
|
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
2022-01-15 17:30:20 +01:00
|
|
|
import { Step, Target } from 'models'
|
2021-12-16 10:43:49 +01:00
|
|
|
import React, { useMemo, useState } from 'react'
|
|
|
|
import {
|
2022-01-15 17:30:20 +01:00
|
|
|
computeConnectingEdgePath,
|
|
|
|
computeEdgePathToMouse,
|
|
|
|
getEndpointTopOffset,
|
2021-12-16 10:43:49 +01:00
|
|
|
} from 'services/graph'
|
|
|
|
|
|
|
|
export const DrawingEdge = () => {
|
2022-01-15 17:30:20 +01:00
|
|
|
const {
|
|
|
|
graphPosition,
|
|
|
|
setConnectingIds,
|
|
|
|
connectingIds,
|
|
|
|
sourceEndpoints,
|
|
|
|
targetEndpoints,
|
|
|
|
} = useGraph()
|
2022-01-12 09:10:59 +01:00
|
|
|
const { typebot, updateStep, updateChoiceItem } = useTypebot()
|
2021-12-16 10:43:49 +01:00
|
|
|
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 })
|
|
|
|
|
|
|
|
const sourceBlock = useMemo(
|
2022-01-06 09:40:56 +01:00
|
|
|
() => connectingIds && typebot?.blocks.byId[connectingIds.source.blockId],
|
2021-12-16 10:43:49 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
[connectingIds]
|
|
|
|
)
|
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
const sourceTop = useMemo(() => {
|
|
|
|
if (!sourceBlock || !connectingIds) return 0
|
|
|
|
return getEndpointTopOffset(
|
|
|
|
graphPosition,
|
|
|
|
sourceEndpoints,
|
|
|
|
connectingIds.source.choiceItemId ??
|
|
|
|
connectingIds.source.stepId + (connectingIds.source.conditionType ?? '')
|
|
|
|
)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [graphPosition, sourceEndpoints, connectingIds])
|
|
|
|
|
|
|
|
const targetTop = useMemo(() => {
|
|
|
|
if (!sourceBlock || !connectingIds) return 0
|
|
|
|
return getEndpointTopOffset(
|
|
|
|
graphPosition,
|
|
|
|
targetEndpoints,
|
|
|
|
connectingIds.target?.stepId
|
|
|
|
)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [graphPosition, targetEndpoints, connectingIds])
|
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
const path = useMemo(() => {
|
2022-01-12 09:10:59 +01:00
|
|
|
if (!sourceBlock || !typebot || !connectingIds) return ``
|
|
|
|
|
|
|
|
return connectingIds?.target
|
2022-01-15 17:30:20 +01:00
|
|
|
? computeConnectingEdgePath(
|
2022-01-12 09:10:59 +01:00
|
|
|
connectingIds as Omit<ConnectingIds, 'target'> & { target: Target },
|
|
|
|
sourceBlock,
|
2022-01-15 17:30:20 +01:00
|
|
|
sourceTop,
|
|
|
|
targetTop,
|
2022-01-12 09:10:59 +01:00
|
|
|
typebot
|
|
|
|
)
|
2022-01-15 17:30:20 +01:00
|
|
|
: computeEdgePathToMouse({
|
|
|
|
blockPosition: sourceBlock.graphCoordinates,
|
2022-01-12 09:10:59 +01:00
|
|
|
mousePosition,
|
2022-01-15 17:30:20 +01:00
|
|
|
sourceTop,
|
|
|
|
})
|
|
|
|
}, [sourceBlock, typebot, connectingIds, sourceTop, targetTop, mousePosition])
|
2021-12-16 10:43:49 +01:00
|
|
|
|
|
|
|
const handleMouseMove = (e: MouseEvent) => {
|
|
|
|
setMousePosition({
|
|
|
|
x: e.clientX - graphPosition.x,
|
2021-12-22 14:59:07 +01:00
|
|
|
y: e.clientY - graphPosition.y - headerHeight,
|
2021-12-16 10:43:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
useEventListener('mousemove', handleMouseMove)
|
|
|
|
useEventListener('mouseup', () => {
|
2022-01-12 09:10:59 +01:00
|
|
|
if (connectingIds?.target) createNewEdge(connectingIds)
|
2021-12-16 10:43:49 +01:00
|
|
|
setConnectingIds(null)
|
|
|
|
})
|
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
const createNewEdge = (connectingIds: ConnectingIds) => {
|
|
|
|
if (connectingIds.source.choiceItemId) {
|
|
|
|
updateChoiceItem(connectingIds.source.choiceItemId, {
|
|
|
|
target: connectingIds.target,
|
|
|
|
})
|
|
|
|
} else if (connectingIds.source.conditionType === 'true') {
|
|
|
|
updateStep(connectingIds.source.stepId, {
|
|
|
|
trueTarget: connectingIds.target,
|
|
|
|
} as Step)
|
|
|
|
} else if (connectingIds.source.conditionType === 'false') {
|
|
|
|
updateStep(connectingIds.source.stepId, {
|
|
|
|
falseTarget: connectingIds.target,
|
|
|
|
} as Step)
|
|
|
|
} else {
|
|
|
|
updateStep(connectingIds.source.stepId, {
|
|
|
|
target: connectingIds.target,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 09:10:59 +01:00
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
if ((mousePosition.x === 0 && mousePosition.y === 0) || !connectingIds)
|
|
|
|
return <></>
|
|
|
|
return (
|
|
|
|
<path
|
|
|
|
d={path}
|
|
|
|
stroke="#718096"
|
|
|
|
strokeWidth="2px"
|
|
|
|
markerEnd="url(#arrow)"
|
|
|
|
fill="none"
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|