2022-01-03 17:39:59 +01:00
|
|
|
import { Flex, FlexProps, useEventListener } from '@chakra-ui/react'
|
2022-01-28 09:42:31 +01:00
|
|
|
import React, { useRef, useMemo, useEffect } from 'react'
|
2021-12-16 10:43:49 +01:00
|
|
|
import { blockWidth, useGraph } from 'contexts/GraphContext'
|
2022-02-02 08:05:02 +01:00
|
|
|
import { BlockNode } from './Nodes/BlockNode/BlockNode'
|
2022-02-04 19:00:08 +01:00
|
|
|
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'
|
2021-12-22 14:59:07 +01:00
|
|
|
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
|
2022-02-11 18:06:59 +01:00
|
|
|
import { Block, DraggableStepType, PublicTypebot, Typebot } from 'models'
|
2022-02-02 08:05:02 +01:00
|
|
|
import { generate } from 'short-uuid'
|
|
|
|
import { AnswersCount } from 'services/analytics'
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-02-02 08:05:02 +01:00
|
|
|
export const Graph = ({
|
2022-02-11 18:06:59 +01:00
|
|
|
typebot,
|
2022-02-02 08:05:02 +01:00
|
|
|
answersCounts,
|
2022-02-13 06:53:48 +01:00
|
|
|
onUnlockProPlanClick,
|
2022-02-02 08:05:02 +01:00
|
|
|
...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 } =
|
2022-01-28 09:42:31 +01:00
|
|
|
useStepDnd()
|
2021-12-16 10:43:49 +01:00
|
|
|
const graphContainerRef = useRef<HTMLDivElement | null>(null)
|
2022-01-28 09:42:31 +01:00
|
|
|
const editorContainerRef = useRef<HTMLDivElement | null>(null)
|
2022-02-11 18:06:59 +01:00
|
|
|
const { createBlock } = useTypebot()
|
2022-02-02 08:05:02 +01:00
|
|
|
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]
|
|
|
|
)
|
|
|
|
|
2022-01-28 09:42:31 +01:00
|
|
|
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) => {
|
2022-02-04 19:00:08 +01:00
|
|
|
if (!typebot) return
|
2021-12-16 10:43:49 +01:00
|
|
|
if (!draggedStep && !draggedStepType) return
|
2022-02-02 08:05:02 +01:00
|
|
|
const coordinates = {
|
2021-12-17 07:54:12 +01:00
|
|
|
x: e.clientX - graphPosition.x - blockWidth / 3,
|
2021-12-22 14:59:07 +01:00
|
|
|
y: e.clientY - graphPosition.y - 20 - headerHeight,
|
2022-02-02 08:05:02 +01:00
|
|
|
}
|
|
|
|
const id = generate()
|
|
|
|
updateBlockCoordinates(id, coordinates)
|
|
|
|
createBlock({
|
|
|
|
id,
|
|
|
|
...coordinates,
|
2022-01-08 07:40:55 +01:00
|
|
|
step: draggedStep ?? (draggedStepType as DraggableStepType),
|
2022-02-04 19:00:08 +01:00
|
|
|
indices: { blockIndex: typebot.blocks.length, stepIndex: 0 },
|
2021-12-16 10:43:49 +01:00
|
|
|
})
|
|
|
|
setDraggedStep(undefined)
|
|
|
|
setDraggedStepType(undefined)
|
|
|
|
}
|
|
|
|
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
|
|
|
|
|
2021-12-22 14:59:07 +01:00
|
|
|
const handleMouseDown = (e: MouseEvent) => {
|
|
|
|
const isRightClick = e.button === 2
|
|
|
|
if (isRightClick) e.stopPropagation()
|
|
|
|
}
|
|
|
|
useEventListener('mousedown', handleMouseDown, undefined, { capture: true })
|
|
|
|
|
2022-01-28 09:42:31 +01:00
|
|
|
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'}
|
2021-12-22 14:59:07 +01:00
|
|
|
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}
|
|
|
|
/>
|
2022-02-04 19:00:08 +01:00
|
|
|
{typebot?.blocks.map((block, idx) => (
|
2022-02-11 18:06:59 +01:00
|
|
|
<BlockNode block={block as Block} blockIndex={idx} key={block.id} />
|
2022-02-02 08:05:02 +01:00
|
|
|
))}
|
2021-12-16 10:43:49 +01:00
|
|
|
</Flex>
|
|
|
|
</Flex>
|
|
|
|
)
|
|
|
|
}
|