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

81 lines
2.4 KiB
TypeScript
Raw Normal View History

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'
2021-12-16 10:43:49 +01:00
import React, { useMemo } from 'react'
import {
getAnchorsPosition,
computeFlowChartConnectorPath,
2022-01-12 09:10:59 +01:00
getSourceChoiceItemIndex,
2021-12-16 10:43:49 +01:00
} from 'services/graph'
2022-01-12 09:10:59 +01:00
import { isChoiceInput } from 'utils'
2021-12-16 10:43:49 +01:00
export type AnchorsPositionProps = {
sourcePosition: Coordinates
targetPosition: Coordinates
sourceType: 'right' | 'left'
totalSegments: number
}
2022-01-12 09:10:59 +01:00
export const Edge = ({
stepId,
item,
}: {
stepId: string
item?: ChoiceItem
}) => {
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]
const sourceBlock = typebot.blocks.byId[step.blockId]
2022-01-12 09:10:59 +01:00
const targetBlockId = item?.target?.blockId ?? step.target?.blockId
assert(isDefined(targetBlockId))
const targetBlock = typebot.blocks.byId[targetBlockId]
const targetStepId = item?.target?.stepId ?? step.target?.stepId
const targetStepIndex = targetStepId
? targetBlock.stepIds.indexOf(targetStepId)
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-12 09:10:59 +01:00
}, [item?.target?.blockId, item?.target?.stepId, stepId, 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 sourceChoiceItemIndex = isChoiceInput(step)
? getSourceChoiceItemIndex(step, item?.id)
: undefined
const anchorsPosition = getAnchorsPosition({
2021-12-16 10:43:49 +01:00
sourceBlock,
targetBlock,
2022-01-12 09:10:59 +01:00
sourceStepIndex: sourceBlock.stepIds.indexOf(stepId),
targetStepIndex,
sourceChoiceItemIndex,
})
2021-12-16 10:43:49 +01:00
return computeFlowChartConnectorPath(anchorsPosition)
2022-01-12 09:10:59 +01:00
}, [item, sourceBlock, step, 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"
/>
)
}