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

128 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-12-16 10:43:49 +01:00
import {
Editable,
EditableInput,
EditablePreview,
Stack,
useEventListener,
} from '@chakra-ui/react'
import React, { useEffect, useMemo, useState } from 'react'
2022-01-06 09:40:56 +01:00
import { Block } from 'models'
2021-12-16 10:43:49 +01:00
import { useGraph } from 'contexts/GraphContext'
import { useStepDnd } from 'contexts/StepDndContext'
2021-12-16 10:43:49 +01:00
import { StepsList } from './StepsList'
2022-01-06 09:40:56 +01:00
import { filterTable, isDefined } from 'utils'
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
import { ContextMenu } from 'components/shared/ContextMenu'
import { BlockNodeContextMenu } from './BlockNodeContextMenu'
2021-12-16 10:43:49 +01:00
2022-01-03 17:39:59 +01:00
type Props = {
block: Block
}
export const BlockNode = ({ block }: Props) => {
const { connectingIds, setConnectingIds, previewingEdgeId } = useGraph()
2022-01-12 09:10:59 +01:00
const { typebot, updateBlock } = useTypebot()
const { setMouseOverBlockId } = useStepDnd()
const { draggedStep, draggedStepType } = useStepDnd()
2021-12-16 10:43:49 +01:00
const [isMouseDown, setIsMouseDown] = useState(false)
const [isConnecting, setIsConnecting] = useState(false)
const isPreviewing = useMemo(() => {
if (!previewingEdgeId) return
const edge = typebot?.edges.byId[previewingEdgeId]
return edge?.to.blockId === block.id || edge?.from.blockId === block.id
}, [block.id, previewingEdgeId, typebot?.edges.byId])
2021-12-16 10:43:49 +01:00
useEffect(() => {
setIsConnecting(
connectingIds?.target?.blockId === block.id &&
2021-12-27 15:59:32 +01:00
!isDefined(connectingIds.target?.stepId)
2021-12-16 10:43:49 +01:00
)
}, [block.id, connectingIds])
2022-01-19 09:44:21 +01:00
const handleTitleSubmit = (title: string) => updateBlock(block.id, { title })
2021-12-16 10:43:49 +01:00
const handleMouseDown = () => {
setIsMouseDown(true)
}
const handleMouseUp = () => {
setIsMouseDown(false)
}
2022-01-19 09:44:21 +01:00
useEventListener('mouseup', handleMouseUp)
2021-12-16 10:43:49 +01:00
const handleMouseMove = (event: MouseEvent) => {
if (!isMouseDown) return
const { movementX, movementY } = event
2022-01-06 09:40:56 +01:00
updateBlock(block.id, {
graphCoordinates: {
x: block.graphCoordinates.x + movementX,
y: block.graphCoordinates.y + movementY,
},
2021-12-16 10:43:49 +01:00
})
}
useEventListener('mousemove', handleMouseMove)
const handleMouseEnter = () => {
2022-01-12 09:10:59 +01:00
if (draggedStepType || draggedStep) setMouseOverBlockId(block.id)
2021-12-16 10:43:49 +01:00
if (connectingIds)
setConnectingIds({ ...connectingIds, target: { blockId: block.id } })
}
const handleMouseLeave = () => {
2022-01-12 09:10:59 +01:00
setMouseOverBlockId(undefined)
2021-12-16 10:43:49 +01:00
if (connectingIds) setConnectingIds({ ...connectingIds, target: undefined })
}
return (
<ContextMenu<HTMLDivElement>
renderMenu={() => <BlockNodeContextMenu blockId={block.id} />}
2021-12-16 10:43:49 +01:00
>
{(ref, isOpened) => (
<Stack
ref={ref}
p="4"
rounded="lg"
bgColor="blue.50"
2022-01-19 09:44:21 +01:00
backgroundImage="linear-gradient(rgb(235, 239, 244), rgb(231, 234, 241))"
borderWidth="2px"
borderColor={
2022-01-19 09:44:21 +01:00
isConnecting || isOpened || isPreviewing ? 'blue.400' : 'white'
}
2022-01-22 18:24:57 +01:00
w="300px"
2022-01-19 09:44:21 +01:00
transition="border 300ms, box-shadow 200ms"
pos="absolute"
style={{
transform: `translate(${block.graphCoordinates.x}px, ${block.graphCoordinates.y}px)`,
}}
onMouseDown={handleMouseDown}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
2022-01-19 09:44:21 +01:00
cursor={isMouseDown ? 'grabbing' : 'pointer'}
boxShadow="0px 0px 0px 1px #e9edf3;"
_hover={{ shadow: 'lg' }}
>
2022-01-19 09:44:21 +01:00
<Editable
defaultValue={block.title}
onSubmit={handleTitleSubmit}
fontWeight="semibold"
>
<EditablePreview
2022-01-19 09:44:21 +01:00
_hover={{ bgColor: 'gray.300' }}
px="1"
userSelect={'none'}
/>
<EditableInput minW="0" px="1" />
</Editable>
2022-01-06 09:40:56 +01:00
{typebot && (
<StepsList
blockId={block.id}
steps={filterTable(block.stepIds, typebot?.steps)}
/>
)}
</Stack>
)}
</ContextMenu>
2021-12-16 10:43:49 +01:00
)
}