2022-12-20 16:55:43 +01:00
|
|
|
import { Flex, useColorModeValue } from '@chakra-ui/react'
|
2022-11-15 09:35:48 +01:00
|
|
|
import {
|
|
|
|
Coordinates,
|
|
|
|
useGraph,
|
|
|
|
NodePosition,
|
|
|
|
useDragDistance,
|
|
|
|
} from '../../../providers'
|
|
|
|
import { useTypebot } from '@/features/editor'
|
2022-11-16 14:56:09 +01:00
|
|
|
import { ChoiceInputBlock, Item, ItemIndices } from 'models'
|
2022-02-04 19:00:08 +01:00
|
|
|
import React, { useRef, useState } from 'react'
|
|
|
|
import { SourceEndpoint } from '../../Endpoints/SourceEndpoint'
|
|
|
|
import { ItemNodeContent } from './ItemNodeContent'
|
|
|
|
import { ItemNodeContextMenu } from './ItemNodeContextMenu'
|
2022-11-15 09:35:48 +01:00
|
|
|
import { ContextMenu } from '@/components/ContextMenu'
|
|
|
|
import { setMultipleRefs } from '@/utils/helpers'
|
2022-02-04 19:00:08 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
item: Item
|
|
|
|
indices: ItemIndices
|
|
|
|
onMouseDown?: (
|
2022-06-11 07:27:38 +02:00
|
|
|
blockNodePosition: { absolute: Coordinates; relative: Coordinates },
|
2022-11-16 14:56:09 +01:00
|
|
|
item: Item
|
2022-02-04 19:00:08 +01:00
|
|
|
) => void
|
2022-11-16 14:56:09 +01:00
|
|
|
connectionDisabled?: boolean
|
2022-02-04 19:00:08 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:56:09 +01:00
|
|
|
export const ItemNode = ({
|
|
|
|
item,
|
|
|
|
indices,
|
|
|
|
onMouseDown,
|
|
|
|
connectionDisabled,
|
|
|
|
}: Props) => {
|
2022-12-20 16:55:43 +01:00
|
|
|
const previewingBorderColor = useColorModeValue('blue.400', 'blue.300')
|
|
|
|
const borderColor = useColorModeValue('gray.200', 'gray.700')
|
|
|
|
const bg = useColorModeValue('white', undefined)
|
2022-02-04 19:00:08 +01:00
|
|
|
const { typebot } = useTypebot()
|
2022-04-02 12:03:21 +02:00
|
|
|
const { previewingEdge } = useGraph()
|
2022-02-04 19:00:08 +01:00
|
|
|
const [isMouseOver, setIsMouseOver] = useState(false)
|
|
|
|
const itemRef = useRef<HTMLDivElement | null>(null)
|
2022-04-02 12:03:21 +02:00
|
|
|
const isPreviewing = previewingEdge?.from.itemId === item.id
|
2022-11-16 14:56:09 +01:00
|
|
|
const isConnectable =
|
|
|
|
!connectionDisabled &&
|
|
|
|
!(
|
|
|
|
typebot?.groups[indices.groupIndex].blocks[
|
|
|
|
indices.blockIndex
|
|
|
|
] as ChoiceInputBlock
|
|
|
|
)?.options?.isMultipleChoice
|
2022-02-04 19:00:08 +01:00
|
|
|
const onDrag = (position: NodePosition) => {
|
2022-11-16 14:56:09 +01:00
|
|
|
if (!onMouseDown) return
|
2022-02-04 19:00:08 +01:00
|
|
|
onMouseDown(position, item)
|
|
|
|
}
|
|
|
|
useDragDistance({
|
|
|
|
ref: itemRef,
|
|
|
|
onDrag,
|
2022-11-16 14:56:09 +01:00
|
|
|
isDisabled: !onMouseDown,
|
2022-02-04 19:00:08 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const handleMouseEnter = () => setIsMouseOver(true)
|
|
|
|
const handleMouseLeave = () => setIsMouseOver(false)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ContextMenu<HTMLDivElement>
|
|
|
|
renderMenu={() => <ItemNodeContextMenu indices={indices} />}
|
|
|
|
>
|
2022-12-20 16:55:43 +01:00
|
|
|
{(ref, isContextMenuOpened) => (
|
2022-02-04 19:00:08 +01:00
|
|
|
<Flex
|
|
|
|
data-testid="item"
|
|
|
|
pos="relative"
|
2022-04-02 12:03:21 +02:00
|
|
|
ref={setMultipleRefs([ref, itemRef])}
|
2022-11-16 14:56:09 +01:00
|
|
|
w="full"
|
2022-02-04 19:00:08 +01:00
|
|
|
>
|
2022-04-02 12:03:21 +02:00
|
|
|
<Flex
|
|
|
|
align="center"
|
|
|
|
onMouseEnter={handleMouseEnter}
|
|
|
|
onMouseLeave={handleMouseLeave}
|
|
|
|
shadow="sm"
|
2022-11-16 14:56:09 +01:00
|
|
|
_hover={{ shadow: 'md' }}
|
2022-04-02 12:03:21 +02:00
|
|
|
transition="box-shadow 200ms, border-color 200ms"
|
|
|
|
rounded="md"
|
2022-12-20 16:55:43 +01:00
|
|
|
bg={bg}
|
|
|
|
borderWidth={isContextMenuOpened || isPreviewing ? '2px' : '1px'}
|
|
|
|
borderColor={
|
|
|
|
isContextMenuOpened || isPreviewing
|
|
|
|
? previewingBorderColor
|
|
|
|
: borderColor
|
|
|
|
}
|
|
|
|
margin={isContextMenuOpened || isPreviewing ? '-1px' : 0}
|
2022-04-02 12:03:21 +02:00
|
|
|
w="full"
|
|
|
|
>
|
|
|
|
<ItemNodeContent
|
|
|
|
item={item}
|
|
|
|
isMouseOver={isMouseOver}
|
|
|
|
indices={indices}
|
2022-02-04 19:00:08 +01:00
|
|
|
/>
|
2022-04-02 12:03:21 +02:00
|
|
|
{typebot && isConnectable && (
|
|
|
|
<SourceEndpoint
|
|
|
|
source={{
|
2022-06-11 07:27:38 +02:00
|
|
|
groupId: typebot.groups[indices.groupIndex].id,
|
|
|
|
blockId: item.blockId,
|
2022-04-02 12:03:21 +02:00
|
|
|
itemId: item.id,
|
|
|
|
}}
|
|
|
|
pos="absolute"
|
|
|
|
right="-49px"
|
|
|
|
pointerEvents="all"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Flex>
|
2022-02-04 19:00:08 +01:00
|
|
|
</Flex>
|
|
|
|
)}
|
|
|
|
</ContextMenu>
|
|
|
|
)
|
|
|
|
}
|