2
0
Files
bot/apps/builder/components/board/graph/BlockNode/StepNode/StepNode.tsx

249 lines
7.6 KiB
TypeScript
Raw Normal View History

2022-01-06 16:54:23 +01:00
import {
Box,
Flex,
HStack,
Popover,
PopoverTrigger,
useEventListener,
} from '@chakra-ui/react'
import React, { useEffect, useMemo, useState } from 'react'
import { Block, DraggableStep, Step } from 'models'
2021-12-16 10:43:49 +01:00
import { useGraph } from 'contexts/GraphContext'
import { StepIcon } from 'components/board/StepTypesList/StepIcon'
import {
isDefined,
isInputStep,
isLogicStep,
isTextBubbleStep,
isIntegrationStep,
} from 'utils'
import { Coordinates } from '@dnd-kit/core/dist/types'
import { TextEditor } from './TextEditor/TextEditor'
2022-01-12 09:10:59 +01:00
import { StepNodeContent } from './StepNodeContent'
import { useTypebot } from 'contexts/TypebotContext'
import { ContextMenu } from 'components/shared/ContextMenu'
2022-01-06 16:54:23 +01:00
import { SettingsPopoverContent } from './SettingsPopoverContent'
2022-01-12 09:10:59 +01:00
import { StepNodeContextMenu } from './StepNodeContextMenu'
import { SourceEndpoint } from './SourceEndpoint'
2022-01-15 17:30:20 +01:00
import { hasDefaultConnector } from 'services/typebots'
import { TargetEndpoint } from './TargetEndpoint'
import { useRouter } from 'next/router'
2021-12-16 10:43:49 +01:00
export const StepNode = ({
step,
isConnectable,
onMouseMoveBottomOfElement,
onMouseMoveTopOfElement,
onMouseDown,
}: {
2022-01-06 09:40:56 +01:00
step: Step
2021-12-16 10:43:49 +01:00
isConnectable: boolean
onMouseMoveBottomOfElement?: () => void
onMouseMoveTopOfElement?: () => void
onMouseDown?: (
stepNodePosition: { absolute: Coordinates; relative: Coordinates },
2022-01-08 07:40:55 +01:00
step: DraggableStep
) => void
2021-12-16 10:43:49 +01:00
}) => {
const { query } = useRouter()
const { setConnectingIds, connectingIds } = useGraph()
2022-01-12 09:10:59 +01:00
const { moveStep, typebot } = useTypebot()
2021-12-16 10:43:49 +01:00
const [isConnecting, setIsConnecting] = useState(false)
const [mouseDownEvent, setMouseDownEvent] =
useState<{ absolute: Coordinates; relative: Coordinates }>()
2022-01-07 07:33:01 +01:00
const [isEditing, setIsEditing] = useState<boolean>(
2022-01-08 07:40:55 +01:00
isTextBubbleStep(step) && step.content.plainText === ''
2022-01-07 07:33:01 +01:00
)
2021-12-16 10:43:49 +01:00
useEffect(() => {
setIsConnecting(
connectingIds?.target?.blockId === step.blockId &&
connectingIds?.target?.stepId === step.id
)
}, [connectingIds, step.blockId, step.id])
const handleMouseEnter = () => {
if (connectingIds?.target)
setConnectingIds({
...connectingIds,
target: { ...connectingIds.target, stepId: step.id },
})
}
const handleMouseLeave = () => {
if (connectingIds?.target)
setConnectingIds({
...connectingIds,
target: { ...connectingIds.target, stepId: undefined },
})
}
const handleMouseDown = (e: React.MouseEvent) => {
if (!onMouseDown) return
e.stopPropagation()
const element = e.currentTarget as HTMLDivElement
const rect = element.getBoundingClientRect()
const relativeX = e.clientX - rect.left
const relativeY = e.clientY - rect.top
setMouseDownEvent({
absolute: { x: e.clientX + relativeX, y: e.clientY + relativeY },
relative: { x: relativeX, y: relativeY },
})
}
const handleGlobalMouseUp = () => {
setMouseDownEvent(undefined)
}
useEventListener('mouseup', handleGlobalMouseUp)
const handleMouseUp = () => {
if (mouseDownEvent) {
setIsEditing(true)
}
2021-12-16 10:43:49 +01:00
}
const handleMouseMove = (event: React.MouseEvent<HTMLDivElement>) => {
if (!onMouseMoveBottomOfElement || !onMouseMoveTopOfElement) return
const isMovingAndIsMouseDown =
mouseDownEvent &&
onMouseDown &&
(event.movementX > 0 || event.movementY > 0)
2022-01-08 07:40:55 +01:00
if (isMovingAndIsMouseDown && step.type !== 'start') {
onMouseDown(mouseDownEvent, step)
2022-01-12 09:10:59 +01:00
moveStep(step.id)
setMouseDownEvent(undefined)
}
2021-12-16 10:43:49 +01:00
const element = event.currentTarget as HTMLDivElement
const rect = element.getBoundingClientRect()
const y = event.clientY - rect.top
if (y > rect.height / 2) onMouseMoveBottomOfElement()
else onMouseMoveTopOfElement()
}
const handleCloseEditor = () => {
setIsEditing(false)
}
2021-12-16 10:43:49 +01:00
const connectedStubPosition: 'right' | 'left' | undefined = useMemo(() => {
2022-01-06 09:40:56 +01:00
if (!typebot) return
const currentBlock = typebot.blocks?.byId[step.blockId]
2021-12-16 10:43:49 +01:00
const isDragginConnectorFromCurrentBlock =
2022-01-06 09:40:56 +01:00
connectingIds?.source.blockId === currentBlock?.id &&
2021-12-16 10:43:49 +01:00
connectingIds?.target?.blockId
const targetBlockId = isDragginConnectorFromCurrentBlock
? connectingIds.target?.blockId
: step.target?.blockId
2022-01-06 09:40:56 +01:00
const targetedBlock = targetBlockId && typebot.blocks.byId[targetBlockId]
2021-12-16 10:43:49 +01:00
return targetedBlock
? targetedBlock.graphCoordinates.x <
(currentBlock as Block).graphCoordinates.x
? 'left'
: 'right'
: undefined
}, [
2022-01-06 09:40:56 +01:00
typebot,
2021-12-16 10:43:49 +01:00
step.blockId,
step.target?.blockId,
2022-01-06 09:40:56 +01:00
connectingIds?.source.blockId,
connectingIds?.target?.blockId,
2021-12-16 10:43:49 +01:00
])
2022-01-08 07:40:55 +01:00
return isEditing && isTextBubbleStep(step) ? (
<TextEditor
2022-01-06 09:40:56 +01:00
stepId={step.id}
initialValue={step.content.richText}
onClose={handleCloseEditor}
/>
) : (
<ContextMenu<HTMLDivElement>
2022-01-06 09:40:56 +01:00
renderMenu={() => <StepNodeContextMenu stepId={step.id} />}
>
{(ref, isOpened) => (
<Popover
placement="left"
isLazy
defaultIsOpen={query.stepId?.toString() === step.id}
>
2022-01-06 16:54:23 +01:00
<PopoverTrigger>
<Flex
pos="relative"
ref={ref}
onMouseMove={handleMouseMove}
onMouseDown={handleMouseDown}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onMouseUp={handleMouseUp}
data-testid={`step-${step.id}`}
2022-01-12 09:10:59 +01:00
w="full"
2022-01-06 16:54:23 +01:00
>
{connectedStubPosition === 'left' && (
<Box
h="2px"
pos="absolute"
left="-18px"
top="25px"
w="18px"
bgColor="blue.500"
/>
)}
<HStack
flex="1"
userSelect="none"
p="3"
borderWidth="2px"
borderColor={isConnecting || isOpened ? 'blue.400' : 'gray.400'}
rounded="lg"
cursor={'pointer'}
bgColor="white"
2022-01-12 09:10:59 +01:00
align="flex-start"
2022-01-06 16:54:23 +01:00
>
2022-01-12 09:10:59 +01:00
<StepIcon type={step.type} mt="1" />
<StepNodeContent step={step} />
2022-01-15 17:30:20 +01:00
<TargetEndpoint
pos="absolute"
left="-32px"
top="19px"
stepId={step.id}
/>
{isConnectable && hasDefaultConnector(step) && (
2022-01-06 16:54:23 +01:00
<SourceEndpoint
2022-01-12 09:10:59 +01:00
source={{
blockId: step.blockId,
stepId: step.id,
}}
2022-01-06 16:54:23 +01:00
pos="absolute"
2022-01-12 09:10:59 +01:00
right="15px"
top="19px"
2022-01-06 16:54:23 +01:00
/>
)}
</HStack>
2021-12-16 10:43:49 +01:00
2022-01-15 17:30:20 +01:00
{isDefined(connectedStubPosition) &&
hasDefaultConnector(step) &&
isConnectable && (
<Box
h="2px"
pos="absolute"
right={
connectedStubPosition === 'left' ? undefined : '-18px'
}
left={
connectedStubPosition === 'left' ? '-18px' : undefined
}
top="25px"
w="18px"
bgColor="gray.500"
/>
)}
2022-01-06 16:54:23 +01:00
</Flex>
</PopoverTrigger>
{hasPopover(step) && <SettingsPopoverContent step={step} />}
2022-01-06 16:54:23 +01:00
</Popover>
2021-12-16 10:43:49 +01:00
)}
</ContextMenu>
2021-12-16 10:43:49 +01:00
)
}
const hasPopover = (step: Step) =>
isInputStep(step) || isLogicStep(step) || isIntegrationStep(step)