2
0
Files
bot/apps/builder/components/board/graph/Edges/Edge.tsx

64 lines
1.8 KiB
TypeScript
Raw Normal View History

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'
2021-12-16 10:43:49 +01:00
import React, { useMemo } from 'react'
import {
getAnchorsPosition,
computeFlowChartConnectorPath,
} from 'services/graph'
export type AnchorsPositionProps = {
sourcePosition: Coordinates
targetPosition: Coordinates
sourceType: 'right' | 'left'
totalSegments: number
}
2022-01-06 09:40:56 +01:00
export const Edge = ({ stepId }: { stepId: string }) => {
const { typebot } = useTypebot()
const { previewingIds } = useGraph()
2022-01-06 09:40:56 +01:00
const step = typebot?.steps.byId[stepId]
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-16 10:43:49 +01:00
const { sourceBlock, targetBlock, targetStepIndex } = useMemo(() => {
2022-01-06 09:40:56 +01:00
if (!typebot) return {}
const step = typebot.steps.byId[stepId]
if (!step.target) return {}
const sourceBlock = typebot.blocks.byId[step.blockId]
const targetBlock = typebot.blocks.byId[step.target.blockId]
2021-12-16 10:43:49 +01:00
const targetStepIndex = step.target.stepId
2022-01-06 09:40:56 +01:00
? targetBlock.stepIds.indexOf(step.target.stepId)
2021-12-16 10:43:49 +01:00
: undefined
return {
2022-01-06 09:40:56 +01:00
sourceBlock,
2021-12-16 10:43:49 +01:00
targetBlock,
targetStepIndex,
}
2022-01-06 09:40:56 +01:00
}, [stepId, typebot])
2021-12-16 10:43:49 +01:00
const path = useMemo(() => {
if (!sourceBlock || !targetBlock) return ``
const anchorsPosition = getAnchorsPosition(
sourceBlock,
targetBlock,
2022-01-06 09:40:56 +01:00
sourceBlock.stepIds.indexOf(stepId),
2021-12-16 10:43:49 +01:00
targetStepIndex
)
return computeFlowChartConnectorPath(anchorsPosition)
2022-01-06 09:40:56 +01:00
}, [sourceBlock, stepId, targetBlock, targetStepIndex])
2021-12-16 10:43:49 +01:00
return (
<path
d={path}
stroke={isPreviewing ? '#1a5fff' : '#718096'}
2021-12-16 10:43:49 +01:00
strokeWidth="2px"
markerEnd="url(#arrow)"
fill="none"
/>
)
}