2
0
Files
bot/apps/builder/components/shared/Graph/Graph.tsx

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-01-03 17:39:59 +01:00
import { Flex, FlexProps, useEventListener } from '@chakra-ui/react'
import React, { useRef, useMemo, useEffect } from 'react'
2021-12-16 10:43:49 +01:00
import { blockWidth, useGraph } from 'contexts/GraphContext'
import { BlockNode } from './Nodes/BlockNode/BlockNode'
import { useStepDnd } from 'contexts/GraphDndContext'
2021-12-16 10:43:49 +01:00
import { Edges } from './Edges'
2022-01-06 09:40:56 +01:00
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
2022-02-11 18:06:59 +01:00
import { Block, DraggableStepType, PublicTypebot, Typebot } from 'models'
import { generate } from 'short-uuid'
import { AnswersCount } from 'services/analytics'
2021-12-16 10:43:49 +01:00
export const Graph = ({
2022-02-11 18:06:59 +01:00
typebot,
answersCounts,
2022-02-13 06:53:48 +01:00
onUnlockProPlanClick,
...props
2022-02-11 18:06:59 +01:00
}: {
typebot?: Typebot | PublicTypebot
answersCounts?: AnswersCount[]
2022-02-13 06:53:48 +01:00
onUnlockProPlanClick?: () => void
2022-02-11 18:06:59 +01:00
} & FlexProps) => {
2021-12-16 10:43:49 +01:00
const { draggedStepType, setDraggedStepType, draggedStep, setDraggedStep } =
useStepDnd()
2021-12-16 10:43:49 +01:00
const graphContainerRef = useRef<HTMLDivElement | null>(null)
const editorContainerRef = useRef<HTMLDivElement | null>(null)
2022-02-11 18:06:59 +01:00
const { createBlock } = useTypebot()
const {
graphPosition,
setGraphPosition,
setOpenedStepId,
updateBlockCoordinates,
} = useGraph()
2021-12-16 10:43:49 +01:00
const transform = useMemo(
() =>
`translate(${graphPosition.x}px, ${graphPosition.y}px) scale(${graphPosition.scale})`,
[graphPosition]
)
useEffect(() => {
editorContainerRef.current = document.getElementById(
'editor-container'
) as HTMLDivElement
}, [])
2021-12-16 10:43:49 +01:00
const handleMouseWheel = (e: WheelEvent) => {
e.preventDefault()
const isPinchingTrackpad = e.ctrlKey
if (isPinchingTrackpad) {
const scale = graphPosition.scale - e.deltaY * 0.01
if (scale <= 0.2 || scale >= 1) return
setGraphPosition({
...graphPosition,
scale,
})
} else
setGraphPosition({
...graphPosition,
x: graphPosition.x - e.deltaX,
y: graphPosition.y - e.deltaY,
})
}
useEventListener('wheel', handleMouseWheel, graphContainerRef.current)
const handleMouseUp = (e: MouseEvent) => {
if (!typebot) return
2021-12-16 10:43:49 +01:00
if (!draggedStep && !draggedStepType) return
const coordinates = {
2021-12-17 07:54:12 +01:00
x: e.clientX - graphPosition.x - blockWidth / 3,
y: e.clientY - graphPosition.y - 20 - headerHeight,
}
const id = generate()
updateBlockCoordinates(id, coordinates)
createBlock({
id,
...coordinates,
2022-01-08 07:40:55 +01:00
step: draggedStep ?? (draggedStepType as DraggableStepType),
indices: { blockIndex: typebot.blocks.length, stepIndex: 0 },
2021-12-16 10:43:49 +01:00
})
setDraggedStep(undefined)
setDraggedStepType(undefined)
}
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
const handleMouseDown = (e: MouseEvent) => {
const isRightClick = e.button === 2
if (isRightClick) e.stopPropagation()
}
useEventListener('mousedown', handleMouseDown, undefined, { capture: true })
const handleClick = () => setOpenedStepId(undefined)
useEventListener('click', handleClick, editorContainerRef.current)
2021-12-16 10:43:49 +01:00
return (
2022-01-03 17:39:59 +01:00
<Flex ref={graphContainerRef} {...props}>
2021-12-16 10:43:49 +01:00
<Flex
flex="1"
2021-12-17 07:54:12 +01:00
boxSize={'200px'}
maxW={'200px'}
2021-12-16 10:43:49 +01:00
style={{
transform,
}}
>
2022-02-13 06:53:48 +01:00
<Edges
edges={typebot?.edges ?? []}
answersCounts={answersCounts}
onUnlockProPlanClick={onUnlockProPlanClick}
/>
{typebot?.blocks.map((block, idx) => (
2022-02-11 18:06:59 +01:00
<BlockNode block={block as Block} blockIndex={idx} key={block.id} />
))}
2021-12-16 10:43:49 +01:00
</Flex>
</Flex>
)
}