2022-01-12 09:10:59 +01:00
|
|
|
import { isDefined } from '@udecode/plate-core'
|
|
|
|
import assert from 'assert'
|
2021-12-16 10:43:49 +01:00
|
|
|
import { Coordinates, useGraph } from 'contexts/GraphContext'
|
2022-01-06 09:40:56 +01:00
|
|
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
2022-01-12 09:10:59 +01:00
|
|
|
import { ChoiceItem } from 'models'
|
2022-01-15 17:30:20 +01:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react'
|
2021-12-16 10:43:49 +01:00
|
|
|
import {
|
|
|
|
getAnchorsPosition,
|
2022-01-15 17:30:20 +01:00
|
|
|
computeEdgePath,
|
|
|
|
getEndpointTopOffset,
|
|
|
|
getTarget,
|
2021-12-16 10:43:49 +01:00
|
|
|
} from 'services/graph'
|
|
|
|
|
|
|
|
export type AnchorsPositionProps = {
|
|
|
|
sourcePosition: Coordinates
|
|
|
|
targetPosition: Coordinates
|
|
|
|
sourceType: 'right' | 'left'
|
|
|
|
totalSegments: number
|
|
|
|
}
|
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
export enum EdgeType {
|
|
|
|
STEP,
|
|
|
|
CHOICE_ITEM,
|
|
|
|
CONDITION_TRUE,
|
|
|
|
CONDITION_FALSE,
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:10:59 +01:00
|
|
|
export const Edge = ({
|
2022-01-15 17:30:20 +01:00
|
|
|
type,
|
2022-01-12 09:10:59 +01:00
|
|
|
stepId,
|
|
|
|
item,
|
|
|
|
}: {
|
2022-01-15 17:30:20 +01:00
|
|
|
type: EdgeType
|
2022-01-12 09:10:59 +01:00
|
|
|
stepId: string
|
|
|
|
item?: ChoiceItem
|
|
|
|
}) => {
|
2021-12-22 14:59:07 +01:00
|
|
|
const { typebot } = useTypebot()
|
2022-01-15 17:30:20 +01:00
|
|
|
const { previewingIds, sourceEndpoints, targetEndpoints, graphPosition } =
|
|
|
|
useGraph()
|
2022-01-06 09:40:56 +01:00
|
|
|
const step = typebot?.steps.byId[stepId]
|
2021-12-22 14:59:07 +01:00
|
|
|
const isPreviewing = useMemo(
|
|
|
|
() =>
|
2022-01-06 09:40:56 +01:00
|
|
|
previewingIds.sourceId === step?.blockId &&
|
|
|
|
previewingIds.targetId === step?.target?.blockId,
|
|
|
|
[previewingIds.sourceId, previewingIds.targetId, step]
|
2021-12-22 14:59:07 +01:00
|
|
|
)
|
2022-01-15 17:30:20 +01:00
|
|
|
const [sourceTop, setSourceTop] = useState(
|
|
|
|
getEndpointTopOffset(graphPosition, sourceEndpoints, item?.id ?? step?.id)
|
|
|
|
)
|
|
|
|
const [targetTop, setTargetTop] = useState(
|
|
|
|
getEndpointTopOffset(graphPosition, targetEndpoints, step?.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const newSourceTop = getEndpointTopOffset(
|
|
|
|
graphPosition,
|
|
|
|
sourceEndpoints,
|
|
|
|
getSourceEndpointId()
|
|
|
|
)
|
|
|
|
const sensibilityThreshold = 10
|
|
|
|
const newSourceTopIsTooClose =
|
|
|
|
sourceTop < newSourceTop + sensibilityThreshold &&
|
|
|
|
sourceTop > newSourceTop - sensibilityThreshold
|
|
|
|
if (newSourceTopIsTooClose) return
|
|
|
|
setSourceTop(newSourceTop)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [typebot?.blocks, typebot?.steps, graphPosition, sourceEndpoints])
|
|
|
|
|
|
|
|
const getSourceEndpointId = () => {
|
|
|
|
switch (type) {
|
|
|
|
case EdgeType.STEP:
|
|
|
|
return step?.id
|
|
|
|
case EdgeType.CHOICE_ITEM:
|
|
|
|
return item?.id
|
|
|
|
case EdgeType.CONDITION_TRUE:
|
|
|
|
return step?.id + 'true'
|
|
|
|
case EdgeType.CONDITION_FALSE:
|
|
|
|
return step?.id + 'false'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!step) return
|
|
|
|
const target = getTarget(step, type)
|
|
|
|
const newTargetTop = getEndpointTopOffset(
|
|
|
|
graphPosition,
|
|
|
|
targetEndpoints,
|
|
|
|
target?.blockId ?? target?.stepId
|
|
|
|
)
|
|
|
|
const sensibilityThreshold = 10
|
|
|
|
const newSourceTopIsTooClose =
|
|
|
|
targetTop < newTargetTop + sensibilityThreshold &&
|
|
|
|
targetTop > newTargetTop - sensibilityThreshold
|
|
|
|
if (newSourceTopIsTooClose) return
|
|
|
|
setTargetTop(newTargetTop)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [typebot?.blocks, typebot?.steps, graphPosition, targetEndpoints])
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
const { sourceBlock, targetBlock } = useMemo(() => {
|
2022-01-06 09:40:56 +01:00
|
|
|
if (!typebot) return {}
|
|
|
|
const step = typebot.steps.byId[stepId]
|
|
|
|
const sourceBlock = typebot.blocks.byId[step.blockId]
|
2022-01-15 17:30:20 +01:00
|
|
|
const targetBlockId = getTarget(step, type)?.blockId
|
2022-01-12 09:10:59 +01:00
|
|
|
assert(isDefined(targetBlockId))
|
|
|
|
const targetBlock = typebot.blocks.byId[targetBlockId]
|
2021-12-16 10:43:49 +01:00
|
|
|
return {
|
2022-01-06 09:40:56 +01:00
|
|
|
sourceBlock,
|
2021-12-16 10:43:49 +01:00
|
|
|
targetBlock,
|
|
|
|
}
|
2022-01-15 17:30:20 +01:00
|
|
|
}, [stepId, type, typebot])
|
2021-12-16 10:43:49 +01:00
|
|
|
|
|
|
|
const path = useMemo(() => {
|
2022-01-12 09:10:59 +01:00
|
|
|
if (!sourceBlock || !targetBlock || !step) return ``
|
|
|
|
const anchorsPosition = getAnchorsPosition({
|
2021-12-16 10:43:49 +01:00
|
|
|
sourceBlock,
|
|
|
|
targetBlock,
|
2022-01-15 17:30:20 +01:00
|
|
|
sourceTop,
|
|
|
|
targetTop,
|
2022-01-12 09:10:59 +01:00
|
|
|
})
|
2022-01-15 17:30:20 +01:00
|
|
|
return computeEdgePath(anchorsPosition)
|
|
|
|
}, [sourceBlock, sourceTop, step, targetBlock, targetTop])
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
if (sourceTop === 0) return <></>
|
2021-12-16 10:43:49 +01:00
|
|
|
return (
|
|
|
|
<path
|
|
|
|
d={path}
|
2021-12-22 14:59:07 +01:00
|
|
|
stroke={isPreviewing ? '#1a5fff' : '#718096'}
|
2021-12-16 10:43:49 +01:00
|
|
|
strokeWidth="2px"
|
2022-01-19 09:44:21 +01:00
|
|
|
markerEnd={isPreviewing ? 'url(#blue-arrow)' : 'url(#arrow)'}
|
2021-12-16 10:43:49 +01:00
|
|
|
fill="none"
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|