2022-01-03 17:39:59 +01:00
|
|
|
import { Flex, FlexProps, useEventListener } from '@chakra-ui/react'
|
2022-11-09 15:08:42 +01:00
|
|
|
import React, { useRef, useMemo, useEffect, useState } from 'react'
|
2022-03-02 12:21:32 +01:00
|
|
|
import {
|
|
|
|
|
blockWidth,
|
2022-04-08 14:30:46 -05:00
|
|
|
Coordinates,
|
2022-03-02 12:21:32 +01:00
|
|
|
graphPositionDefaultValue,
|
|
|
|
|
useGraph,
|
2022-06-26 16:12:28 +02:00
|
|
|
useGroupsCoordinates,
|
2022-11-15 09:35:48 +01:00
|
|
|
useBlockDnd,
|
|
|
|
|
} from '../providers'
|
|
|
|
|
import { useTypebot } from '@/features/editor'
|
2022-06-11 07:27:38 +02:00
|
|
|
import { DraggableBlockType, PublicTypebot, Typebot } from 'models'
|
2022-02-14 18:56:29 +01:00
|
|
|
import { useDebounce } from 'use-debounce'
|
2022-06-26 16:12:28 +02:00
|
|
|
import GraphElements from './GraphElements'
|
2022-03-18 12:30:42 +01:00
|
|
|
import cuid from 'cuid'
|
2022-11-15 09:35:48 +01:00
|
|
|
import { useUser } from '@/features/account'
|
2022-04-08 14:30:46 -05:00
|
|
|
import { ZoomButtons } from './ZoomButtons'
|
2022-11-15 09:35:48 +01:00
|
|
|
import { AnswersCount } from '@/features/analytics'
|
|
|
|
|
import { headerHeight } from '@/features/editor'
|
2022-11-18 07:58:43 +01:00
|
|
|
import { useGesture } from '@use-gesture/react'
|
|
|
|
|
import { GraphNavigation } from 'db'
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-11-18 07:58:43 +01:00
|
|
|
const maxScale = 2
|
|
|
|
|
const minScale = 0.3
|
2022-06-11 07:27:38 +02:00
|
|
|
const zoomButtonsScaleBlock = 0.2
|
2022-02-15 06:25:03 +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
|
|
|
}: {
|
2022-06-26 16:12:28 +02:00
|
|
|
typebot: Typebot | PublicTypebot
|
2022-02-11 18:06:59 +01:00
|
|
|
answersCounts?: AnswersCount[]
|
2022-02-13 06:53:48 +01:00
|
|
|
onUnlockProPlanClick?: () => void
|
2022-02-11 18:06:59 +01:00
|
|
|
} & FlexProps) => {
|
2022-03-25 13:31:14 +01:00
|
|
|
const {
|
2022-06-11 07:27:38 +02:00
|
|
|
draggedBlockType,
|
|
|
|
|
setDraggedBlockType,
|
|
|
|
|
draggedBlock,
|
|
|
|
|
setDraggedBlock,
|
2022-03-25 13:31:14 +01:00
|
|
|
draggedItem,
|
|
|
|
|
setDraggedItem,
|
2022-06-11 07:27:38 +02:00
|
|
|
} = useBlockDnd()
|
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-06-11 07:27:38 +02:00
|
|
|
const { createGroup } = useTypebot()
|
2022-02-02 08:05:02 +01:00
|
|
|
const {
|
2022-03-02 12:21:32 +01:00
|
|
|
setGraphPosition: setGlobalGraphPosition,
|
2022-06-11 07:27:38 +02:00
|
|
|
setOpenedBlockId,
|
2022-11-16 14:56:09 +01:00
|
|
|
setOpenedItemId,
|
2022-03-24 11:44:34 +01:00
|
|
|
setPreviewingEdge,
|
2022-04-11 13:31:29 -05:00
|
|
|
connectingIds,
|
2022-02-02 08:05:02 +01:00
|
|
|
} = useGraph()
|
2022-06-26 16:12:28 +02:00
|
|
|
const { updateGroupCoordinates } = useGroupsCoordinates()
|
2022-12-16 08:47:02 +01:00
|
|
|
const [graphPosition, setGraphPosition] = useState(
|
|
|
|
|
graphPositionDefaultValue(typebot.groups[0].graphCoordinates)
|
|
|
|
|
)
|
2022-02-14 18:56:29 +01:00
|
|
|
const [debouncedGraphPosition] = useDebounce(graphPosition, 200)
|
2021-12-16 10:43:49 +01:00
|
|
|
const transform = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
`translate(${graphPosition.x}px, ${graphPosition.y}px) scale(${graphPosition.scale})`,
|
|
|
|
|
[graphPosition]
|
|
|
|
|
)
|
2022-04-08 14:30:46 -05:00
|
|
|
const { user } = useUser()
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-04-11 13:31:29 -05:00
|
|
|
const [autoMoveDirection, setAutoMoveDirection] = useState<
|
|
|
|
|
'top' | 'right' | 'bottom' | 'left' | undefined
|
|
|
|
|
>()
|
|
|
|
|
useAutoMoveBoard(autoMoveDirection, setGraphPosition)
|
|
|
|
|
|
2022-01-28 09:42:31 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
editorContainerRef.current = document.getElementById(
|
|
|
|
|
'editor-container'
|
|
|
|
|
) as HTMLDivElement
|
2022-02-14 18:56:29 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-01-28 09:42:31 +01:00
|
|
|
}, [])
|
|
|
|
|
|
2022-02-14 18:56:29 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!graphContainerRef.current) return
|
2022-03-02 12:21:32 +01:00
|
|
|
const { top, left } = graphContainerRef.current.getBoundingClientRect()
|
|
|
|
|
setGlobalGraphPosition({
|
|
|
|
|
x: left + debouncedGraphPosition.x,
|
|
|
|
|
y: top + debouncedGraphPosition.y,
|
2022-04-08 14:30:46 -05:00
|
|
|
scale: debouncedGraphPosition.scale,
|
2022-03-02 12:21:32 +01:00
|
|
|
})
|
2022-02-14 18:56:29 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [debouncedGraphPosition])
|
|
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
const handleMouseUp = (e: MouseEvent) => {
|
2022-02-04 19:00:08 +01:00
|
|
|
if (!typebot) return
|
2022-03-25 13:31:14 +01:00
|
|
|
if (draggedItem) setDraggedItem(undefined)
|
2022-06-11 07:27:38 +02:00
|
|
|
if (!draggedBlock && !draggedBlockType) return
|
2022-04-08 14:30:46 -05:00
|
|
|
|
|
|
|
|
const coordinates = projectMouse(
|
|
|
|
|
{ x: e.clientX, y: e.clientY },
|
|
|
|
|
graphPosition
|
|
|
|
|
)
|
2022-03-18 12:30:42 +01:00
|
|
|
const id = cuid()
|
2022-06-11 07:27:38 +02:00
|
|
|
updateGroupCoordinates(id, coordinates)
|
|
|
|
|
createGroup({
|
2022-02-02 08:05:02 +01:00
|
|
|
id,
|
|
|
|
|
...coordinates,
|
2022-06-11 07:27:38 +02:00
|
|
|
block: draggedBlock ?? (draggedBlockType as DraggableBlockType),
|
|
|
|
|
indices: { groupIndex: typebot.groups.length, blockIndex: 0 },
|
2021-12-16 10:43:49 +01:00
|
|
|
})
|
2022-06-11 07:27:38 +02:00
|
|
|
setDraggedBlock(undefined)
|
|
|
|
|
setDraggedBlockType(undefined)
|
2021-12-16 10:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-14 06:23:39 +01:00
|
|
|
const handleCaptureMouseDown = (e: MouseEvent) => {
|
2021-12-22 14:59:07 +01:00
|
|
|
const isRightClick = e.button === 2
|
|
|
|
|
if (isRightClick) e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 11:44:34 +01:00
|
|
|
const handleClick = () => {
|
2022-06-11 07:27:38 +02:00
|
|
|
setOpenedBlockId(undefined)
|
2022-11-16 14:56:09 +01:00
|
|
|
setOpenedItemId(undefined)
|
2022-03-24 11:44:34 +01:00
|
|
|
setPreviewingEdge(undefined)
|
|
|
|
|
}
|
2022-01-28 09:42:31 +01:00
|
|
|
|
2022-11-18 07:58:43 +01:00
|
|
|
useGesture(
|
|
|
|
|
{
|
|
|
|
|
onDrag: ({ delta: [dx, dy] }) => {
|
|
|
|
|
setGraphPosition({
|
|
|
|
|
...graphPosition,
|
|
|
|
|
x: graphPosition.x + dx,
|
|
|
|
|
y: graphPosition.y + dy,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
onWheel: ({ delta: [dx, dy], pinching }) => {
|
|
|
|
|
if (pinching) return
|
|
|
|
|
|
|
|
|
|
setGraphPosition({
|
|
|
|
|
...graphPosition,
|
|
|
|
|
x: graphPosition.x - dx,
|
|
|
|
|
y: graphPosition.y - dy,
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
onPinch: ({ origin: [x, y], offset: [scale] }) => {
|
|
|
|
|
zoom({ scale, mousePosition: { x, y } })
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
target: graphContainerRef,
|
|
|
|
|
pinch: {
|
|
|
|
|
scaleBounds: { min: minScale, max: maxScale },
|
|
|
|
|
modifierKey:
|
|
|
|
|
user?.graphNavigation === GraphNavigation.MOUSE ? null : 'ctrlKey',
|
|
|
|
|
},
|
|
|
|
|
drag: { pointer: { keys: false } },
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const getCenterOfGraph = (): Coordinates => {
|
|
|
|
|
const graphWidth = graphContainerRef.current?.clientWidth ?? 0
|
|
|
|
|
const graphHeight = graphContainerRef.current?.clientHeight ?? 0
|
|
|
|
|
return {
|
|
|
|
|
x: graphWidth / 2,
|
|
|
|
|
y: graphHeight / 2,
|
|
|
|
|
}
|
2022-04-08 14:30:46 -05:00
|
|
|
}
|
|
|
|
|
|
2022-11-18 07:58:43 +01:00
|
|
|
const zoom = ({
|
|
|
|
|
scale,
|
|
|
|
|
mousePosition,
|
|
|
|
|
delta,
|
|
|
|
|
}: {
|
|
|
|
|
scale?: number
|
|
|
|
|
delta?: number
|
|
|
|
|
mousePosition?: Coordinates
|
|
|
|
|
}) => {
|
|
|
|
|
const { x: mouseX, y } = mousePosition ?? getCenterOfGraph()
|
2022-04-08 14:30:46 -05:00
|
|
|
const mouseY = y - headerHeight
|
2022-11-18 07:58:43 +01:00
|
|
|
let newScale = scale ?? graphPosition.scale + (delta ?? 0)
|
2022-04-08 14:30:46 -05:00
|
|
|
if (
|
2022-11-18 07:58:43 +01:00
|
|
|
(newScale >= maxScale && graphPosition.scale === maxScale) ||
|
|
|
|
|
(newScale <= minScale && graphPosition.scale === minScale)
|
2022-04-08 14:30:46 -05:00
|
|
|
)
|
|
|
|
|
return
|
2022-11-18 07:58:43 +01:00
|
|
|
newScale =
|
|
|
|
|
newScale >= maxScale
|
|
|
|
|
? maxScale
|
|
|
|
|
: newScale <= minScale
|
|
|
|
|
? minScale
|
|
|
|
|
: newScale
|
2022-04-08 14:30:46 -05:00
|
|
|
|
|
|
|
|
const xs = (mouseX - graphPosition.x) / graphPosition.scale
|
|
|
|
|
const ys = (mouseY - graphPosition.y) / graphPosition.scale
|
|
|
|
|
setGraphPosition({
|
|
|
|
|
...graphPosition,
|
2022-11-18 07:58:43 +01:00
|
|
|
x: mouseX - xs * newScale,
|
|
|
|
|
y: mouseY - ys * newScale,
|
|
|
|
|
scale: newScale,
|
2022-02-19 18:09:09 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 13:31:29 -05:00
|
|
|
const handleMouseMove = (e: MouseEvent) => {
|
|
|
|
|
if (!connectingIds)
|
|
|
|
|
return autoMoveDirection ? setAutoMoveDirection(undefined) : undefined
|
|
|
|
|
if (e.clientX <= 50) return setAutoMoveDirection('left')
|
|
|
|
|
if (e.clientY <= 50 + headerHeight) return setAutoMoveDirection('top')
|
|
|
|
|
if (e.clientX >= window.innerWidth - 50)
|
|
|
|
|
return setAutoMoveDirection('right')
|
|
|
|
|
if (e.clientY >= window.innerHeight - 50)
|
|
|
|
|
return setAutoMoveDirection('bottom')
|
|
|
|
|
setAutoMoveDirection(undefined)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEventListener('mousedown', handleCaptureMouseDown, undefined, {
|
|
|
|
|
capture: true,
|
|
|
|
|
})
|
|
|
|
|
useEventListener('mouseup', handleMouseUp, graphContainerRef.current)
|
|
|
|
|
useEventListener('click', handleClick, editorContainerRef.current)
|
|
|
|
|
useEventListener('mousemove', handleMouseMove)
|
2022-06-26 16:12:28 +02:00
|
|
|
|
2022-11-18 07:58:43 +01:00
|
|
|
// Make sure pinch doesn't interfere with native Safari zoom
|
|
|
|
|
// More info: https://use-gesture.netlify.app/docs/gestures/
|
|
|
|
|
useEventListener('gesturestart', (e) => e.preventDefault())
|
|
|
|
|
useEventListener('gesturechange', (e) => e.preventDefault())
|
2022-11-06 10:21:20 +01:00
|
|
|
|
2022-11-18 07:58:43 +01:00
|
|
|
const zoomIn = () => zoom({ delta: zoomButtonsScaleBlock })
|
|
|
|
|
const zoomOut = () => zoom({ delta: -zoomButtonsScaleBlock })
|
2022-06-26 16:12:28 +02:00
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
return (
|
2022-11-18 07:58:43 +01:00
|
|
|
<Flex ref={graphContainerRef} position="relative" {...props}>
|
|
|
|
|
<ZoomButtons onZoomInClick={zoomIn} onZoomOutClick={zoomOut} />
|
|
|
|
|
<Flex
|
|
|
|
|
flex="1"
|
|
|
|
|
w="full"
|
|
|
|
|
h="full"
|
|
|
|
|
position="absolute"
|
|
|
|
|
data-testid="graph"
|
|
|
|
|
style={{
|
|
|
|
|
transform,
|
|
|
|
|
}}
|
|
|
|
|
willChange="transform"
|
|
|
|
|
transformOrigin="0px 0px 0px"
|
|
|
|
|
>
|
|
|
|
|
<GraphElements
|
|
|
|
|
edges={typebot.edges}
|
|
|
|
|
groups={typebot.groups}
|
|
|
|
|
answersCounts={answersCounts}
|
|
|
|
|
onUnlockProPlanClick={onUnlockProPlanClick}
|
|
|
|
|
/>
|
2021-12-16 10:43:49 +01:00
|
|
|
</Flex>
|
2022-11-18 07:58:43 +01:00
|
|
|
</Flex>
|
2021-12-16 10:43:49 +01:00
|
|
|
)
|
|
|
|
|
}
|
2022-04-08 14:30:46 -05:00
|
|
|
|
|
|
|
|
const projectMouse = (
|
|
|
|
|
mouseCoordinates: Coordinates,
|
|
|
|
|
graphPosition: Coordinates & { scale: number }
|
|
|
|
|
) => {
|
|
|
|
|
return {
|
|
|
|
|
x:
|
|
|
|
|
(mouseCoordinates.x -
|
|
|
|
|
graphPosition.x -
|
|
|
|
|
blockWidth / (3 / graphPosition.scale)) /
|
|
|
|
|
graphPosition.scale,
|
|
|
|
|
y:
|
|
|
|
|
(mouseCoordinates.y -
|
|
|
|
|
graphPosition.y -
|
|
|
|
|
(headerHeight + 20 * graphPosition.scale)) /
|
|
|
|
|
graphPosition.scale,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-11 13:31:29 -05:00
|
|
|
|
|
|
|
|
const useAutoMoveBoard = (
|
|
|
|
|
autoMoveDirection: 'top' | 'right' | 'bottom' | 'left' | undefined,
|
|
|
|
|
setGraphPosition: React.Dispatch<
|
|
|
|
|
React.SetStateAction<{
|
|
|
|
|
x: number
|
|
|
|
|
y: number
|
|
|
|
|
scale: number
|
|
|
|
|
}>
|
|
|
|
|
>
|
|
|
|
|
) =>
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!autoMoveDirection) return
|
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
|
setGraphPosition((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
x:
|
|
|
|
|
autoMoveDirection === 'right'
|
|
|
|
|
? prev.x - 5
|
|
|
|
|
: autoMoveDirection === 'left'
|
|
|
|
|
? prev.x + 5
|
|
|
|
|
: prev.x,
|
|
|
|
|
y:
|
|
|
|
|
autoMoveDirection === 'bottom'
|
|
|
|
|
? prev.y - 5
|
|
|
|
|
: autoMoveDirection === 'top'
|
|
|
|
|
? prev.y + 5
|
|
|
|
|
: prev.y,
|
|
|
|
|
}))
|
|
|
|
|
}, 5)
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
clearInterval(interval)
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [autoMoveDirection])
|