2022-01-29 11:22:22 +01:00
|
|
|
import {
|
|
|
|
Stack,
|
|
|
|
Text,
|
|
|
|
SimpleGrid,
|
|
|
|
useEventListener,
|
|
|
|
Portal,
|
|
|
|
Flex,
|
|
|
|
IconButton,
|
|
|
|
Tooltip,
|
|
|
|
Fade,
|
|
|
|
} from '@chakra-ui/react'
|
|
|
|
import {
|
2022-06-11 07:27:38 +02:00
|
|
|
BubbleBlockType,
|
|
|
|
DraggableBlockType,
|
|
|
|
InputBlockType,
|
|
|
|
IntegrationBlockType,
|
|
|
|
LogicBlockType,
|
2022-01-29 11:22:22 +01:00
|
|
|
} from 'models'
|
2022-06-11 07:27:38 +02:00
|
|
|
import { useBlockDnd } from 'contexts/GraphDndContext'
|
2022-01-29 11:22:22 +01:00
|
|
|
import React, { useState } from 'react'
|
2022-06-11 07:27:38 +02:00
|
|
|
import { BlockCard, BlockCardOverlay } from './BlockCard'
|
2022-01-29 11:22:22 +01:00
|
|
|
import { LockedIcon, UnlockedIcon } from 'assets/icons'
|
|
|
|
import { headerHeight } from 'components/shared/TypebotHeader'
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const BlocksSideBar = () => {
|
|
|
|
const { setDraggedBlockType, draggedBlockType } = useBlockDnd()
|
2022-01-29 11:22:22 +01:00
|
|
|
const [position, setPosition] = useState({
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
})
|
|
|
|
const [relativeCoordinates, setRelativeCoordinates] = useState({ x: 0, y: 0 })
|
|
|
|
const [isLocked, setIsLocked] = useState(true)
|
|
|
|
const [isExtended, setIsExtended] = useState(true)
|
|
|
|
|
|
|
|
const handleMouseMove = (event: MouseEvent) => {
|
2022-06-11 07:27:38 +02:00
|
|
|
if (!draggedBlockType) return
|
2022-01-29 11:22:22 +01:00
|
|
|
const { clientX, clientY } = event
|
|
|
|
setPosition({
|
|
|
|
...position,
|
|
|
|
x: clientX - relativeCoordinates.x,
|
|
|
|
y: clientY - relativeCoordinates.y,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
useEventListener('mousemove', handleMouseMove)
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const handleMouseDown = (e: React.MouseEvent, type: DraggableBlockType) => {
|
2022-01-29 11:22:22 +01:00
|
|
|
const element = e.currentTarget as HTMLDivElement
|
|
|
|
const rect = element.getBoundingClientRect()
|
|
|
|
setPosition({ x: rect.left, y: rect.top })
|
|
|
|
const x = e.clientX - rect.left
|
|
|
|
const y = e.clientY - rect.top
|
|
|
|
setRelativeCoordinates({ x, y })
|
2022-06-11 07:27:38 +02:00
|
|
|
setDraggedBlockType(type)
|
2022-01-29 11:22:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleMouseUp = () => {
|
2022-06-11 07:27:38 +02:00
|
|
|
if (!draggedBlockType) return
|
|
|
|
setDraggedBlockType(undefined)
|
2022-01-29 11:22:22 +01:00
|
|
|
setPosition({
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
useEventListener('mouseup', handleMouseUp)
|
|
|
|
|
|
|
|
const handleLockClick = () => setIsLocked(!isLocked)
|
|
|
|
|
|
|
|
const handleDockBarEnter = () => setIsExtended(true)
|
|
|
|
|
|
|
|
const handleMouseLeave = () => {
|
|
|
|
if (isLocked) return
|
|
|
|
setIsExtended(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex
|
2022-03-31 09:49:23 +02:00
|
|
|
w="360px"
|
2022-01-29 11:22:22 +01:00
|
|
|
pos="absolute"
|
|
|
|
left="0"
|
|
|
|
h={`calc(100vh - ${headerHeight}px)`}
|
|
|
|
zIndex="2"
|
|
|
|
pl="4"
|
|
|
|
py="4"
|
|
|
|
onMouseLeave={handleMouseLeave}
|
2022-03-31 09:49:23 +02:00
|
|
|
transform={isExtended ? 'translateX(0)' : 'translateX(-350px)'}
|
2022-01-29 11:22:22 +01:00
|
|
|
transition="transform 350ms cubic-bezier(0.075, 0.82, 0.165, 1) 0s"
|
|
|
|
>
|
|
|
|
<Stack
|
|
|
|
w="full"
|
|
|
|
rounded="lg"
|
|
|
|
shadow="xl"
|
|
|
|
borderWidth="1px"
|
|
|
|
pt="2"
|
2022-02-17 08:01:57 +01:00
|
|
|
pb="10"
|
2022-03-31 09:49:23 +02:00
|
|
|
px="4"
|
|
|
|
bgColor="white"
|
2022-01-29 11:22:22 +01:00
|
|
|
spacing={6}
|
|
|
|
userSelect="none"
|
2022-02-17 08:01:57 +01:00
|
|
|
overflowY="scroll"
|
2022-02-28 14:22:43 +01:00
|
|
|
className="hide-scrollbar"
|
2022-01-29 11:22:22 +01:00
|
|
|
>
|
2022-02-02 08:05:02 +01:00
|
|
|
<Flex justifyContent="flex-end">
|
|
|
|
<Tooltip label={isLocked ? 'Unlock sidebar' : 'Lock sidebar'}>
|
|
|
|
<IconButton
|
|
|
|
icon={isLocked ? <LockedIcon /> : <UnlockedIcon />}
|
|
|
|
aria-label={isLocked ? 'Unlock' : 'Lock'}
|
|
|
|
size="sm"
|
|
|
|
variant="outline"
|
|
|
|
onClick={handleLockClick}
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
</Flex>
|
2022-01-29 11:22:22 +01:00
|
|
|
|
|
|
|
<Stack>
|
|
|
|
<Text fontSize="sm" fontWeight="semibold" color="gray.600">
|
|
|
|
Bubbles
|
|
|
|
</Text>
|
2022-03-31 09:49:23 +02:00
|
|
|
<SimpleGrid columns={2} spacing="3">
|
2022-06-11 07:27:38 +02:00
|
|
|
{Object.values(BubbleBlockType).map((type) => (
|
|
|
|
<BlockCard key={type} type={type} onMouseDown={handleMouseDown} />
|
2022-01-29 11:22:22 +01:00
|
|
|
))}
|
|
|
|
</SimpleGrid>
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
<Stack>
|
|
|
|
<Text fontSize="sm" fontWeight="semibold" color="gray.600">
|
|
|
|
Inputs
|
|
|
|
</Text>
|
2022-03-31 09:49:23 +02:00
|
|
|
<SimpleGrid columns={2} spacing="3">
|
2022-06-11 07:27:38 +02:00
|
|
|
{Object.values(InputBlockType).map((type) => (
|
|
|
|
<BlockCard key={type} type={type} onMouseDown={handleMouseDown} />
|
2022-01-29 11:22:22 +01:00
|
|
|
))}
|
|
|
|
</SimpleGrid>
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
<Stack>
|
|
|
|
<Text fontSize="sm" fontWeight="semibold" color="gray.600">
|
|
|
|
Logic
|
|
|
|
</Text>
|
2022-03-31 09:49:23 +02:00
|
|
|
<SimpleGrid columns={2} spacing="3">
|
2022-06-11 07:27:38 +02:00
|
|
|
{Object.values(LogicBlockType).map((type) => (
|
|
|
|
<BlockCard key={type} type={type} onMouseDown={handleMouseDown} />
|
2022-01-29 11:22:22 +01:00
|
|
|
))}
|
|
|
|
</SimpleGrid>
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
<Stack>
|
|
|
|
<Text fontSize="sm" fontWeight="semibold" color="gray.600">
|
|
|
|
Integrations
|
|
|
|
</Text>
|
2022-03-31 09:49:23 +02:00
|
|
|
<SimpleGrid columns={2} spacing="3">
|
2022-06-11 07:27:38 +02:00
|
|
|
{Object.values(IntegrationBlockType).map((type) => (
|
|
|
|
<BlockCard
|
2022-04-29 19:13:34 -07:00
|
|
|
key={type}
|
|
|
|
type={type}
|
|
|
|
onMouseDown={handleMouseDown}
|
2022-06-11 07:27:38 +02:00
|
|
|
isDisabled={type === IntegrationBlockType.MAKE_COM}
|
2022-04-29 19:13:34 -07:00
|
|
|
/>
|
2022-01-29 11:22:22 +01:00
|
|
|
))}
|
|
|
|
</SimpleGrid>
|
|
|
|
</Stack>
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
{draggedBlockType && (
|
2022-01-29 11:22:22 +01:00
|
|
|
<Portal>
|
2022-06-11 07:27:38 +02:00
|
|
|
<BlockCardOverlay
|
|
|
|
type={draggedBlockType}
|
2022-01-29 11:22:22 +01:00
|
|
|
onMouseUp={handleMouseUp}
|
|
|
|
pos="fixed"
|
|
|
|
top="0"
|
|
|
|
left="0"
|
|
|
|
style={{
|
|
|
|
transform: `translate(${position.x}px, ${position.y}px) rotate(-2deg)`,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Portal>
|
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
<Fade in={!isLocked} unmountOnExit>
|
|
|
|
<Flex
|
|
|
|
pos="absolute"
|
|
|
|
h="100%"
|
|
|
|
right="-50px"
|
|
|
|
w="50px"
|
|
|
|
top="0"
|
|
|
|
justify="center"
|
|
|
|
align="center"
|
|
|
|
onMouseEnter={handleDockBarEnter}
|
|
|
|
>
|
|
|
|
<Flex w="5px" h="20px" bgColor="gray.400" rounded="md" />
|
|
|
|
</Flex>
|
|
|
|
</Fade>
|
|
|
|
</Flex>
|
|
|
|
)
|
|
|
|
}
|