♻️ Normalize data
This commit is contained in:
@ -6,7 +6,7 @@ import {
|
|||||||
StatLabel,
|
StatLabel,
|
||||||
StatNumber,
|
StatNumber,
|
||||||
} from '@chakra-ui/react'
|
} from '@chakra-ui/react'
|
||||||
import { Stats } from 'bot-engine'
|
import { Stats } from 'models'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
export const StatsCards = ({
|
export const StatsCards = ({
|
||||||
|
@ -3,7 +3,6 @@ import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
|||||||
import React, { useMemo, useRef } from 'react'
|
import React, { useMemo, useRef } from 'react'
|
||||||
import { AnswersCount } from 'services/analytics'
|
import { AnswersCount } from 'services/analytics'
|
||||||
import { BlockNode } from './blocks/BlockNode'
|
import { BlockNode } from './blocks/BlockNode'
|
||||||
import { StartBlockNode } from './blocks/StartBlockNode'
|
|
||||||
import { Edges } from './Edges'
|
import { Edges } from './Edges'
|
||||||
|
|
||||||
const AnalyticsGraph = ({
|
const AnalyticsGraph = ({
|
||||||
@ -49,9 +48,8 @@ const AnalyticsGraph = ({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Edges answersCounts={answersCounts} />
|
<Edges answersCounts={answersCounts} />
|
||||||
{typebot.startBlock && <StartBlockNode block={typebot.startBlock} />}
|
{typebot.blocks.allIds.map((blockId) => (
|
||||||
{(typebot.blocks ?? []).map((block) => (
|
<BlockNode block={typebot.blocks.byId[blockId]} key={blockId} />
|
||||||
<BlockNode block={block} key={block.id} />
|
|
||||||
))}
|
))}
|
||||||
</Flex>
|
</Flex>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
@ -7,11 +7,12 @@ type Props = {
|
|||||||
}
|
}
|
||||||
export const DropOffEdge = ({ blockId }: Props) => {
|
export const DropOffEdge = ({ blockId }: Props) => {
|
||||||
const { typebot } = useAnalyticsGraph()
|
const { typebot } = useAnalyticsGraph()
|
||||||
|
|
||||||
const path = useMemo(() => {
|
const path = useMemo(() => {
|
||||||
if (!typebot) return
|
if (!typebot) return
|
||||||
const block = (typebot?.blocks ?? []).find((b) => b.id === blockId)
|
const block = typebot.blocks.byId[blockId]
|
||||||
if (!block) return ''
|
if (!block) return ''
|
||||||
return computeDropOffPath(block.graphCoordinates, block.steps.length - 1)
|
return computeDropOffPath(block.graphCoordinates, block.stepIds.length - 1)
|
||||||
}, [blockId, typebot])
|
}, [blockId, typebot])
|
||||||
|
|
||||||
return <path d={path} stroke={'#E53E3E'} strokeWidth="2px" fill="none" />
|
return <path d={path} stroke={'#E53E3E'} strokeWidth="2px" fill="none" />
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import { Block } from 'bot-engine'
|
|
||||||
import { StepWithTarget } from 'components/board/graph/Edges/Edge'
|
|
||||||
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import {
|
import {
|
||||||
@ -7,42 +5,37 @@ import {
|
|||||||
computeFlowChartConnectorPath,
|
computeFlowChartConnectorPath,
|
||||||
} from 'services/graph'
|
} from 'services/graph'
|
||||||
|
|
||||||
export const Edge = ({ step }: { step: StepWithTarget }) => {
|
type Props = { stepId: string }
|
||||||
|
|
||||||
|
export const Edge = ({ stepId }: Props) => {
|
||||||
const { typebot } = useAnalyticsGraph()
|
const { typebot } = useAnalyticsGraph()
|
||||||
const { blocks, startBlock } = typebot ?? {}
|
|
||||||
|
|
||||||
const { sourceBlock, targetBlock, targetStepIndex } = useMemo(() => {
|
const { sourceBlock, targetBlock, targetStepIndex } = useMemo(() => {
|
||||||
const targetBlock = blocks?.find(
|
if (!typebot) return {}
|
||||||
(b) => b?.id === step.target.blockId
|
const step = typebot.steps.byId[stepId]
|
||||||
) as Block
|
if (!step.target) return {}
|
||||||
|
const targetBlock = typebot.blocks.byId[step.target.blockId]
|
||||||
const targetStepIndex = step.target.stepId
|
const targetStepIndex = step.target.stepId
|
||||||
? targetBlock.steps.findIndex((s) => s.id === step.target.stepId)
|
? targetBlock.stepIds.indexOf(step.target.stepId)
|
||||||
: undefined
|
: undefined
|
||||||
|
const sourceBlock = typebot.blocks.byId[step.blockId]
|
||||||
return {
|
return {
|
||||||
sourceBlock: [startBlock, ...(blocks ?? [])].find(
|
sourceBlock,
|
||||||
(b) => b?.id === step.blockId
|
|
||||||
),
|
|
||||||
targetBlock,
|
targetBlock,
|
||||||
targetStepIndex,
|
targetStepIndex,
|
||||||
}
|
}
|
||||||
}, [
|
}, [stepId, typebot])
|
||||||
blocks,
|
|
||||||
startBlock,
|
|
||||||
step.blockId,
|
|
||||||
step.target.blockId,
|
|
||||||
step.target.stepId,
|
|
||||||
])
|
|
||||||
|
|
||||||
const path = useMemo(() => {
|
const path = useMemo(() => {
|
||||||
if (!sourceBlock || !targetBlock) return ``
|
if (!sourceBlock || !targetBlock) return ``
|
||||||
const anchorsPosition = getAnchorsPosition(
|
const anchorsPosition = getAnchorsPosition(
|
||||||
sourceBlock,
|
sourceBlock,
|
||||||
targetBlock,
|
targetBlock,
|
||||||
sourceBlock.steps.findIndex((s) => s.id === step.id),
|
sourceBlock.stepIds.indexOf(stepId),
|
||||||
targetStepIndex
|
targetStepIndex
|
||||||
)
|
)
|
||||||
return computeFlowChartConnectorPath(anchorsPosition)
|
return computeFlowChartConnectorPath(anchorsPosition)
|
||||||
}, [sourceBlock, step.id, targetBlock, targetStepIndex])
|
}, [sourceBlock, stepId, targetBlock, targetStepIndex])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<path
|
<path
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { chakra } from '@chakra-ui/system'
|
import { chakra } from '@chakra-ui/system'
|
||||||
import { StepWithTarget } from 'components/board/graph/Edges/Edge'
|
|
||||||
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import { AnswersCount } from 'services/analytics'
|
import { AnswersCount } from 'services/analytics'
|
||||||
|
import { isDefined } from 'utils'
|
||||||
import { DropOffBlock } from '../blocks/DropOffBlock'
|
import { DropOffBlock } from '../blocks/DropOffBlock'
|
||||||
import { DropOffEdge } from './DropOffEdge'
|
import { DropOffEdge } from './DropOffEdge'
|
||||||
import { Edge } from './Edge'
|
import { Edge } from './Edge'
|
||||||
@ -11,16 +11,13 @@ type Props = { answersCounts?: AnswersCount[] }
|
|||||||
|
|
||||||
export const Edges = ({ answersCounts }: Props) => {
|
export const Edges = ({ answersCounts }: Props) => {
|
||||||
const { typebot } = useAnalyticsGraph()
|
const { typebot } = useAnalyticsGraph()
|
||||||
const { blocks, startBlock } = typebot ?? {}
|
|
||||||
const stepsWithTarget: StepWithTarget[] = useMemo(() => {
|
const stepIdsWithTarget: string[] = useMemo(() => {
|
||||||
if (!startBlock) return []
|
if (!typebot) return []
|
||||||
return [
|
return typebot.steps.allIds.filter((stepId) =>
|
||||||
...(startBlock.steps.filter((s) => s.target) as StepWithTarget[]),
|
isDefined(typebot.steps.byId[stepId].target)
|
||||||
...((blocks ?? [])
|
)
|
||||||
.flatMap((b) => b.steps)
|
}, [typebot])
|
||||||
.filter((s) => s.target) as StepWithTarget[]),
|
|
||||||
]
|
|
||||||
}, [blocks, startBlock])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -32,8 +29,8 @@ export const Edges = ({ answersCounts }: Props) => {
|
|||||||
left="0"
|
left="0"
|
||||||
top="0"
|
top="0"
|
||||||
>
|
>
|
||||||
{stepsWithTarget.map((step) => (
|
{stepIdsWithTarget.map((stepId) => (
|
||||||
<Edge key={step.id} step={step} />
|
<Edge key={stepId} stepId={stepId} />
|
||||||
))}
|
))}
|
||||||
<marker
|
<marker
|
||||||
id={'arrow'}
|
id={'arrow'}
|
||||||
|
@ -6,9 +6,9 @@ import {
|
|||||||
useEventListener,
|
useEventListener,
|
||||||
} from '@chakra-ui/react'
|
} from '@chakra-ui/react'
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { Block } from 'bot-engine'
|
|
||||||
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
||||||
import { StepsList } from './StepsList'
|
import { StepsList } from './StepsList'
|
||||||
|
import { Block } from 'models'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
block: Block
|
block: Block
|
||||||
@ -56,7 +56,7 @@ export const BlockNode = ({ block }: Props) => {
|
|||||||
<EditablePreview px="1" userSelect={'none'} />
|
<EditablePreview px="1" userSelect={'none'} />
|
||||||
<EditableInput minW="0" px="1" />
|
<EditableInput minW="0" px="1" />
|
||||||
</Editable>
|
</Editable>
|
||||||
<StepsList blockId={block.id} steps={block.steps} />
|
<StepsList stepIds={block.stepIds} />
|
||||||
</Stack>
|
</Stack>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Tag, Text, VStack } from '@chakra-ui/react'
|
import { Tag, Text, VStack } from '@chakra-ui/react'
|
||||||
import { Block } from 'bot-engine'
|
|
||||||
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import { AnswersCount } from 'services/analytics'
|
import { AnswersCount } from 'services/analytics'
|
||||||
@ -21,13 +20,13 @@ export const DropOffBlock = ({ answersCounts, blockId }: Props) => {
|
|||||||
const { totalDroppedUser, dropOffRate } = useMemo(() => {
|
const { totalDroppedUser, dropOffRate } = useMemo(() => {
|
||||||
if (!typebot || totalAnswers === undefined)
|
if (!typebot || totalAnswers === undefined)
|
||||||
return { previousTotal: undefined, dropOffRate: undefined }
|
return { previousTotal: undefined, dropOffRate: undefined }
|
||||||
const previousTotal = answersCounts
|
const previousBlockIds = typebot.blocks.allIds.filter(() =>
|
||||||
.filter(
|
typebot.steps.allIds.find(
|
||||||
(a) =>
|
(sId) => typebot.steps.byId[sId].target?.blockId === blockId
|
||||||
[typebot.startBlock, ...typebot.blocks].find((b) =>
|
|
||||||
(b as Block).steps.find((s) => s.target?.blockId === blockId)
|
|
||||||
)?.id === a.blockId
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
const previousTotal = answersCounts
|
||||||
|
.filter((a) => previousBlockIds.includes(a.blockId))
|
||||||
.reduce((prev, acc) => acc.totalAnswers + prev, 0)
|
.reduce((prev, acc) => acc.totalAnswers + prev, 0)
|
||||||
if (previousTotal === 0)
|
if (previousTotal === 0)
|
||||||
return { previousTotal: undefined, dropOffRate: undefined }
|
return { previousTotal: undefined, dropOffRate: undefined }
|
||||||
@ -41,11 +40,11 @@ export const DropOffBlock = ({ answersCounts, blockId }: Props) => {
|
|||||||
|
|
||||||
const labelCoordinates = useMemo(() => {
|
const labelCoordinates = useMemo(() => {
|
||||||
if (!typebot) return { x: 0, y: 0 }
|
if (!typebot) return { x: 0, y: 0 }
|
||||||
const sourceBlock = typebot?.blocks.find((b) => b.id === blockId)
|
const sourceBlock = typebot?.blocks.byId[blockId]
|
||||||
if (!sourceBlock) return
|
if (!sourceBlock) return
|
||||||
return computeSourceCoordinates(
|
return computeSourceCoordinates(
|
||||||
sourceBlock?.graphCoordinates,
|
sourceBlock?.graphCoordinates,
|
||||||
sourceBlock?.steps.length - 1
|
sourceBlock?.stepIds.length - 1
|
||||||
)
|
)
|
||||||
}, [blockId, typebot])
|
}, [blockId, typebot])
|
||||||
|
|
||||||
|
@ -1,62 +0,0 @@
|
|||||||
import {
|
|
||||||
Editable,
|
|
||||||
EditableInput,
|
|
||||||
EditablePreview,
|
|
||||||
Stack,
|
|
||||||
useEventListener,
|
|
||||||
} from '@chakra-ui/react'
|
|
||||||
import React, { useState } from 'react'
|
|
||||||
import { StartBlock } from 'bot-engine'
|
|
||||||
import { useAnalyticsGraph } from 'contexts/AnalyticsGraphProvider'
|
|
||||||
import { StepsList } from './StepsList'
|
|
||||||
|
|
||||||
type Props = {
|
|
||||||
block: StartBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
export const StartBlockNode = ({ block }: Props) => {
|
|
||||||
const { updateBlockPosition } = useAnalyticsGraph()
|
|
||||||
const [isMouseDown, setIsMouseDown] = useState(false)
|
|
||||||
|
|
||||||
const handleMouseDown = () => {
|
|
||||||
setIsMouseDown(true)
|
|
||||||
}
|
|
||||||
const handleMouseUp = () => {
|
|
||||||
setIsMouseDown(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleMouseMove = (event: MouseEvent) => {
|
|
||||||
if (!isMouseDown) return
|
|
||||||
const { movementX, movementY } = event
|
|
||||||
|
|
||||||
updateBlockPosition(block.id, {
|
|
||||||
x: block.graphCoordinates.x + movementX,
|
|
||||||
y: block.graphCoordinates.y + movementY,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
useEventListener('mousemove', handleMouseMove)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Stack
|
|
||||||
p="4"
|
|
||||||
rounded="lg"
|
|
||||||
bgColor="blue.50"
|
|
||||||
borderWidth="2px"
|
|
||||||
minW="300px"
|
|
||||||
transition="border 300ms"
|
|
||||||
pos="absolute"
|
|
||||||
style={{
|
|
||||||
transform: `translate(${block.graphCoordinates.x}px, ${block.graphCoordinates.y}px)`,
|
|
||||||
}}
|
|
||||||
onMouseDown={handleMouseDown}
|
|
||||||
onMouseUp={handleMouseUp}
|
|
||||||
>
|
|
||||||
<Editable value={block.title} isDisabled>
|
|
||||||
<EditablePreview px="1" userSelect={'none'} />
|
|
||||||
<EditableInput minW="0" px="1" />
|
|
||||||
</Editable>
|
|
||||||
<StepsList blockId={block.id} steps={block.steps} />
|
|
||||||
</Stack>
|
|
||||||
)
|
|
||||||
}
|
|
@ -1,27 +1,24 @@
|
|||||||
import { Flex, Stack } from '@chakra-ui/react'
|
import { Flex, Stack } from '@chakra-ui/react'
|
||||||
import { StartStep, Step } from 'bot-engine'
|
|
||||||
import { StepNodeOverlay } from 'components/board/graph/BlockNode/StepNode'
|
import { StepNodeOverlay } from 'components/board/graph/BlockNode/StepNode'
|
||||||
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
|
|
||||||
export const StepsList = ({
|
export const StepsList = ({ stepIds }: { stepIds: string[] }) => {
|
||||||
steps,
|
const { typebot } = useTypebot()
|
||||||
}: {
|
|
||||||
blockId: string
|
|
||||||
steps: Step[] | [StartStep]
|
|
||||||
}) => {
|
|
||||||
return (
|
return (
|
||||||
<Stack spacing={1} transition="none">
|
<Stack spacing={1} transition="none">
|
||||||
<Flex h={'2px'} bgColor={'gray.400'} visibility={'hidden'} rounded="lg" />
|
<Flex h={'2px'} bgColor={'gray.400'} visibility={'hidden'} rounded="lg" />
|
||||||
{steps.map((step) => (
|
{typebot &&
|
||||||
<Stack key={step.id} spacing={1}>
|
stepIds.map((stepId) => (
|
||||||
<StepNodeOverlay key={step.id} step={step} />
|
<Stack key={stepId} spacing={1}>
|
||||||
<Flex
|
<StepNodeOverlay step={typebot?.steps.byId[stepId]} />
|
||||||
h={'2px'}
|
<Flex
|
||||||
bgColor={'gray.400'}
|
h={'2px'}
|
||||||
visibility={'hidden'}
|
bgColor={'gray.400'}
|
||||||
rounded="lg"
|
visibility={'hidden'}
|
||||||
/>
|
rounded="lg"
|
||||||
</Stack>
|
/>
|
||||||
))}
|
</Stack>
|
||||||
|
))}
|
||||||
</Stack>
|
</Stack>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Button, ButtonProps, Flex, HStack } from '@chakra-ui/react'
|
import { Button, ButtonProps, Flex, HStack } from '@chakra-ui/react'
|
||||||
import { StepType } from 'bot-engine'
|
import { StepType } from 'models'
|
||||||
import { useDnd } from 'contexts/DndContext'
|
import { useDnd } from 'contexts/DndContext'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { StepIcon } from './StepIcon'
|
import { StepIcon } from './StepIcon'
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ChatIcon, FlagIcon, TextIcon } from 'assets/icons'
|
import { ChatIcon, FlagIcon, TextIcon } from 'assets/icons'
|
||||||
import { StepType } from 'bot-engine'
|
import { StepType } from 'models'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
type StepIconProps = { type: StepType }
|
type StepIconProps = { type: StepType }
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Text } from '@chakra-ui/react'
|
import { Text } from '@chakra-ui/react'
|
||||||
import { StepType } from 'bot-engine'
|
import { StepType } from 'models'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
type Props = { type: StepType }
|
type Props = { type: StepType }
|
||||||
|
@ -5,7 +5,7 @@ import {
|
|||||||
SimpleGrid,
|
SimpleGrid,
|
||||||
useEventListener,
|
useEventListener,
|
||||||
} from '@chakra-ui/react'
|
} from '@chakra-ui/react'
|
||||||
import { StepType } from 'bot-engine'
|
import { StepType } from 'models'
|
||||||
import { useDnd } from 'contexts/DndContext'
|
import { useDnd } from 'contexts/DndContext'
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { StepCard, StepCardOverlay } from './StepCard'
|
import { StepCard, StepCardOverlay } from './StepCard'
|
||||||
|
@ -6,12 +6,12 @@ import {
|
|||||||
useEventListener,
|
useEventListener,
|
||||||
} from '@chakra-ui/react'
|
} from '@chakra-ui/react'
|
||||||
import React, { useEffect, useMemo, useState } from 'react'
|
import React, { useEffect, useMemo, useState } from 'react'
|
||||||
import { Block } from 'bot-engine'
|
import { Block } from 'models'
|
||||||
import { useGraph } from 'contexts/GraphContext'
|
import { useGraph } from 'contexts/GraphContext'
|
||||||
import { useDnd } from 'contexts/DndContext'
|
import { useDnd } from 'contexts/DndContext'
|
||||||
import { StepsList } from './StepsList'
|
import { StepsList } from './StepsList'
|
||||||
import { isDefined } from 'utils'
|
import { filterTable, isDefined } from 'utils'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { ContextMenu } from 'components/shared/ContextMenu'
|
import { ContextMenu } from 'components/shared/ContextMenu'
|
||||||
import { BlockNodeContextMenu } from './BlockNodeContextMenu'
|
import { BlockNodeContextMenu } from './BlockNodeContextMenu'
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ type Props = {
|
|||||||
|
|
||||||
export const BlockNode = ({ block }: Props) => {
|
export const BlockNode = ({ block }: Props) => {
|
||||||
const { connectingIds, setConnectingIds, previewingIds } = useGraph()
|
const { connectingIds, setConnectingIds, previewingIds } = useGraph()
|
||||||
const { updateBlockPosition, addStepToBlock } = useTypebot()
|
const { typebot, updateBlock, createStep } = useTypebot()
|
||||||
const { draggedStep, draggedStepType, setDraggedStepType, setDraggedStep } =
|
const { draggedStep, draggedStepType, setDraggedStepType, setDraggedStep } =
|
||||||
useDnd()
|
useDnd()
|
||||||
const [isMouseDown, setIsMouseDown] = useState(false)
|
const [isMouseDown, setIsMouseDown] = useState(false)
|
||||||
@ -55,9 +55,11 @@ export const BlockNode = ({ block }: Props) => {
|
|||||||
if (!isMouseDown) return
|
if (!isMouseDown) return
|
||||||
const { movementX, movementY } = event
|
const { movementX, movementY } = event
|
||||||
|
|
||||||
updateBlockPosition(block.id, {
|
updateBlock(block.id, {
|
||||||
x: block.graphCoordinates.x + movementX,
|
graphCoordinates: {
|
||||||
y: block.graphCoordinates.y + movementY,
|
x: block.graphCoordinates.x + movementX,
|
||||||
|
y: block.graphCoordinates.y + movementY,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,11 +79,11 @@ export const BlockNode = ({ block }: Props) => {
|
|||||||
const handleStepDrop = (index: number) => {
|
const handleStepDrop = (index: number) => {
|
||||||
setShowSortPlaceholders(false)
|
setShowSortPlaceholders(false)
|
||||||
if (draggedStepType) {
|
if (draggedStepType) {
|
||||||
addStepToBlock(block.id, draggedStepType, index)
|
createStep(block.id, draggedStepType, index)
|
||||||
setDraggedStepType(undefined)
|
setDraggedStepType(undefined)
|
||||||
}
|
}
|
||||||
if (draggedStep) {
|
if (draggedStep) {
|
||||||
addStepToBlock(block.id, draggedStep, index)
|
createStep(block.id, draggedStep, index)
|
||||||
setDraggedStep(undefined)
|
setDraggedStep(undefined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,12 +121,14 @@ export const BlockNode = ({ block }: Props) => {
|
|||||||
/>
|
/>
|
||||||
<EditableInput minW="0" px="1" />
|
<EditableInput minW="0" px="1" />
|
||||||
</Editable>
|
</Editable>
|
||||||
<StepsList
|
{typebot && (
|
||||||
blockId={block.id}
|
<StepsList
|
||||||
steps={block.steps}
|
blockId={block.id}
|
||||||
showSortPlaceholders={showSortPlaceholders}
|
steps={filterTable(block.stepIds, typebot?.steps)}
|
||||||
onMouseUp={handleStepDrop}
|
showSortPlaceholders={showSortPlaceholders}
|
||||||
/>
|
onMouseUp={handleStepDrop}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
)}
|
)}
|
||||||
</ContextMenu>
|
</ContextMenu>
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
import { MenuList, MenuItem } from '@chakra-ui/react'
|
import { MenuList, MenuItem } from '@chakra-ui/react'
|
||||||
import { TrashIcon } from 'assets/icons'
|
import { TrashIcon } from 'assets/icons'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
|
|
||||||
export const BlockNodeContextMenu = ({ blockId }: { blockId: string }) => {
|
export const BlockNodeContextMenu = ({ blockId }: { blockId: string }) => {
|
||||||
const { removeBlock } = useTypebot()
|
const { deleteBlock } = useTypebot()
|
||||||
|
|
||||||
|
const handleDeleteClick = () => deleteBlock(blockId)
|
||||||
|
|
||||||
const handleDeleteClick = () => {
|
|
||||||
removeBlock(blockId)
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<MenuList>
|
<MenuList>
|
||||||
<MenuItem icon={<TrashIcon />} onClick={handleDeleteClick}>
|
<MenuItem icon={<TrashIcon />} onClick={handleDeleteClick}>
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
import {
|
|
||||||
Editable,
|
|
||||||
EditableInput,
|
|
||||||
EditablePreview,
|
|
||||||
Stack,
|
|
||||||
useEventListener,
|
|
||||||
} from '@chakra-ui/react'
|
|
||||||
import React, { useMemo, useState } from 'react'
|
|
||||||
import { StartBlock } from 'bot-engine'
|
|
||||||
import { StepNode } from './StepNode'
|
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
|
||||||
import { useGraph } from 'contexts/GraphContext'
|
|
||||||
|
|
||||||
type Props = {
|
|
||||||
block: StartBlock
|
|
||||||
}
|
|
||||||
export const StartBlockNode = ({ block }: Props) => {
|
|
||||||
const { previewingIds } = useGraph()
|
|
||||||
const [isMouseDown, setIsMouseDown] = useState(false)
|
|
||||||
const [titleValue, setTitleValue] = useState(block.title)
|
|
||||||
const { updateBlockPosition } = useTypebot()
|
|
||||||
const isPreviewing = useMemo(
|
|
||||||
() => previewingIds.sourceId === block.id,
|
|
||||||
[block.id, previewingIds.sourceId]
|
|
||||||
)
|
|
||||||
|
|
||||||
const handleTitleChange = (title: string) => setTitleValue(title)
|
|
||||||
|
|
||||||
const handleMouseDown = () => {
|
|
||||||
setIsMouseDown(true)
|
|
||||||
}
|
|
||||||
const handleMouseUp = () => {
|
|
||||||
setIsMouseDown(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleMouseMove = (event: MouseEvent) => {
|
|
||||||
if (!isMouseDown) return
|
|
||||||
const { movementX, movementY } = event
|
|
||||||
|
|
||||||
updateBlockPosition(block.id, {
|
|
||||||
x: block.graphCoordinates.x + movementX,
|
|
||||||
y: block.graphCoordinates.y + movementY,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
useEventListener('mousemove', handleMouseMove)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Stack
|
|
||||||
p="4"
|
|
||||||
rounded="lg"
|
|
||||||
bgColor="blue.50"
|
|
||||||
borderWidth="2px"
|
|
||||||
borderColor={isPreviewing ? 'blue.400' : 'gray.400'}
|
|
||||||
minW="300px"
|
|
||||||
transition="border 300ms"
|
|
||||||
pos="absolute"
|
|
||||||
style={{
|
|
||||||
transform: `translate(${block.graphCoordinates.x}px, ${block.graphCoordinates.y}px)`,
|
|
||||||
}}
|
|
||||||
onMouseDown={handleMouseDown}
|
|
||||||
onMouseUp={handleMouseUp}
|
|
||||||
spacing="14px"
|
|
||||||
>
|
|
||||||
<Editable value={titleValue} onChange={handleTitleChange}>
|
|
||||||
<EditablePreview
|
|
||||||
_hover={{ bgColor: 'blue.100' }}
|
|
||||||
px="1"
|
|
||||||
userSelect={'none'}
|
|
||||||
/>
|
|
||||||
<EditableInput minW="0" px="1" />
|
|
||||||
</Editable>
|
|
||||||
<StepNode step={block.steps[0]} isConnectable={true} />
|
|
||||||
</Stack>
|
|
||||||
)
|
|
||||||
}
|
|
@ -1,19 +1,12 @@
|
|||||||
import { MenuList, MenuItem } from '@chakra-ui/react'
|
import { MenuList, MenuItem } from '@chakra-ui/react'
|
||||||
import { TrashIcon } from 'assets/icons'
|
import { TrashIcon } from 'assets/icons'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
|
|
||||||
export const StepNodeContextMenu = ({
|
export const StepNodeContextMenu = ({ stepId }: { stepId: string }) => {
|
||||||
blockId,
|
const { deleteStep } = useTypebot()
|
||||||
stepId,
|
|
||||||
}: {
|
const handleDeleteClick = () => deleteStep(stepId)
|
||||||
blockId: string
|
|
||||||
stepId: string
|
|
||||||
}) => {
|
|
||||||
const { removeStepFromBlock } = useTypebot()
|
|
||||||
|
|
||||||
const handleDeleteClick = () => {
|
|
||||||
removeStepFromBlock(blockId, stepId)
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<MenuList>
|
<MenuList>
|
||||||
<MenuItem icon={<TrashIcon />} onClick={handleDeleteClick}>
|
<MenuItem icon={<TrashIcon />} onClick={handleDeleteClick}>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Flex, Text } from '@chakra-ui/react'
|
import { Flex, Text } from '@chakra-ui/react'
|
||||||
import { Step, StartStep, StepType } from 'bot-engine'
|
import { Step, StartStep, StepType } from 'models'
|
||||||
|
|
||||||
export const StepContent = (props: Step | StartStep) => {
|
export const StepContent = (props: Step | StartStep) => {
|
||||||
switch (props.type) {
|
switch (props.type) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Box, Flex, HStack, useEventListener } from '@chakra-ui/react'
|
import { Box, Flex, HStack, useEventListener } from '@chakra-ui/react'
|
||||||
import React, { useEffect, useMemo, useState } from 'react'
|
import React, { useEffect, useMemo, useState } from 'react'
|
||||||
import { Block, StartStep, Step, StepType } from 'bot-engine'
|
import { Block, Step, StepType } from 'models'
|
||||||
import { SourceEndpoint } from './SourceEndpoint'
|
import { SourceEndpoint } from './SourceEndpoint'
|
||||||
import { useGraph } from 'contexts/GraphContext'
|
import { useGraph } from 'contexts/GraphContext'
|
||||||
import { StepIcon } from 'components/board/StepTypesList/StepIcon'
|
import { StepIcon } from 'components/board/StepTypesList/StepIcon'
|
||||||
@ -8,7 +8,7 @@ import { isDefined } from 'utils'
|
|||||||
import { Coordinates } from '@dnd-kit/core/dist/types'
|
import { Coordinates } from '@dnd-kit/core/dist/types'
|
||||||
import { TextEditor } from './TextEditor/TextEditor'
|
import { TextEditor } from './TextEditor/TextEditor'
|
||||||
import { StepContent } from './StepContent'
|
import { StepContent } from './StepContent'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { ContextMenu } from 'components/shared/ContextMenu'
|
import { ContextMenu } from 'components/shared/ContextMenu'
|
||||||
import { StepNodeContextMenu } from './RightClickMenu'
|
import { StepNodeContextMenu } from './RightClickMenu'
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ export const StepNode = ({
|
|||||||
onMouseMoveTopOfElement,
|
onMouseMoveTopOfElement,
|
||||||
onMouseDown,
|
onMouseDown,
|
||||||
}: {
|
}: {
|
||||||
step: Step | StartStep
|
step: Step
|
||||||
isConnectable: boolean
|
isConnectable: boolean
|
||||||
onMouseMoveBottomOfElement?: () => void
|
onMouseMoveBottomOfElement?: () => void
|
||||||
onMouseMoveTopOfElement?: () => void
|
onMouseMoveTopOfElement?: () => void
|
||||||
@ -29,8 +29,7 @@ export const StepNode = ({
|
|||||||
) => void
|
) => void
|
||||||
}) => {
|
}) => {
|
||||||
const { setConnectingIds, connectingIds } = useGraph()
|
const { setConnectingIds, connectingIds } = useGraph()
|
||||||
const { removeStepFromBlock, typebot } = useTypebot()
|
const { deleteStep, typebot } = useTypebot()
|
||||||
const { blocks, startBlock } = typebot ?? {}
|
|
||||||
const [isConnecting, setIsConnecting] = useState(false)
|
const [isConnecting, setIsConnecting] = useState(false)
|
||||||
const [mouseDownEvent, setMouseDownEvent] =
|
const [mouseDownEvent, setMouseDownEvent] =
|
||||||
useState<{ absolute: Coordinates; relative: Coordinates }>()
|
useState<{ absolute: Coordinates; relative: Coordinates }>()
|
||||||
@ -59,9 +58,8 @@ export const StepNode = ({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleConnectionDragStart = () => {
|
const handleConnectionDragStart = () =>
|
||||||
setConnectingIds({ blockId: step.blockId, stepId: step.id })
|
setConnectingIds({ source: { blockId: step.blockId, stepId: step.id } })
|
||||||
}
|
|
||||||
|
|
||||||
const handleMouseDown = (e: React.MouseEvent) => {
|
const handleMouseDown = (e: React.MouseEvent) => {
|
||||||
if (!onMouseDown) return
|
if (!onMouseDown) return
|
||||||
@ -95,7 +93,7 @@ export const StepNode = ({
|
|||||||
(event.movementX > 0 || event.movementY > 0)
|
(event.movementX > 0 || event.movementY > 0)
|
||||||
if (isMovingAndIsMouseDown) {
|
if (isMovingAndIsMouseDown) {
|
||||||
onMouseDown(mouseDownEvent, step as Step)
|
onMouseDown(mouseDownEvent, step as Step)
|
||||||
removeStepFromBlock(step.blockId, step.id)
|
deleteStep(step.id)
|
||||||
setMouseDownEvent(undefined)
|
setMouseDownEvent(undefined)
|
||||||
}
|
}
|
||||||
const element = event.currentTarget as HTMLDivElement
|
const element = event.currentTarget as HTMLDivElement
|
||||||
@ -110,18 +108,15 @@ export const StepNode = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const connectedStubPosition: 'right' | 'left' | undefined = useMemo(() => {
|
const connectedStubPosition: 'right' | 'left' | undefined = useMemo(() => {
|
||||||
const currentBlock = [startBlock, ...(blocks ?? [])].find(
|
if (!typebot) return
|
||||||
(b) => b?.id === step.blockId
|
const currentBlock = typebot.blocks?.byId[step.blockId]
|
||||||
)
|
|
||||||
const isDragginConnectorFromCurrentBlock =
|
const isDragginConnectorFromCurrentBlock =
|
||||||
connectingIds?.blockId === currentBlock?.id &&
|
connectingIds?.source.blockId === currentBlock?.id &&
|
||||||
connectingIds?.target?.blockId
|
connectingIds?.target?.blockId
|
||||||
const targetBlockId = isDragginConnectorFromCurrentBlock
|
const targetBlockId = isDragginConnectorFromCurrentBlock
|
||||||
? connectingIds.target?.blockId
|
? connectingIds.target?.blockId
|
||||||
: step.target?.blockId
|
: step.target?.blockId
|
||||||
const targetedBlock = targetBlockId
|
const targetedBlock = targetBlockId && typebot.blocks.byId[targetBlockId]
|
||||||
? (blocks ?? []).find((b) => b.id === targetBlockId)
|
|
||||||
: undefined
|
|
||||||
return targetedBlock
|
return targetedBlock
|
||||||
? targetedBlock.graphCoordinates.x <
|
? targetedBlock.graphCoordinates.x <
|
||||||
(currentBlock as Block).graphCoordinates.x
|
(currentBlock as Block).graphCoordinates.x
|
||||||
@ -129,27 +124,24 @@ export const StepNode = ({
|
|||||||
: 'right'
|
: 'right'
|
||||||
: undefined
|
: undefined
|
||||||
}, [
|
}, [
|
||||||
blocks,
|
typebot,
|
||||||
connectingIds?.blockId,
|
|
||||||
connectingIds?.target?.blockId,
|
|
||||||
step.blockId,
|
step.blockId,
|
||||||
step.target?.blockId,
|
step.target?.blockId,
|
||||||
startBlock,
|
connectingIds?.source.blockId,
|
||||||
|
connectingIds?.target?.blockId,
|
||||||
])
|
])
|
||||||
|
|
||||||
return step.type === StepType.TEXT &&
|
return step.type === StepType.TEXT &&
|
||||||
(isEditing ||
|
(isEditing ||
|
||||||
(isEditing === undefined && step.content.plainText === '')) ? (
|
(isEditing === undefined && step.content.plainText === '')) ? (
|
||||||
<TextEditor
|
<TextEditor
|
||||||
ids={{ stepId: step.id, blockId: step.blockId }}
|
stepId={step.id}
|
||||||
initialValue={step.content.richText}
|
initialValue={step.content.richText}
|
||||||
onClose={handleCloseEditor}
|
onClose={handleCloseEditor}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<ContextMenu<HTMLDivElement>
|
<ContextMenu<HTMLDivElement>
|
||||||
renderMenu={() => (
|
renderMenu={() => <StepNodeContextMenu stepId={step.id} />}
|
||||||
<StepNodeContextMenu blockId={step.blockId} stepId={step.id} />
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
{(ref, isOpened) => (
|
{(ref, isOpened) => (
|
||||||
<Flex
|
<Flex
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { StackProps, HStack } from '@chakra-ui/react'
|
import { StackProps, HStack } from '@chakra-ui/react'
|
||||||
import { StartStep, Step } from 'bot-engine'
|
import { StartStep, Step } from 'models'
|
||||||
import { StepIcon } from 'components/board/StepTypesList/StepIcon'
|
import { StepIcon } from 'components/board/StepTypesList/StepIcon'
|
||||||
import { StepContent } from './StepContent'
|
import { StepContent } from './StepContent'
|
||||||
|
|
||||||
|
@ -9,20 +9,25 @@ import {
|
|||||||
} from '@udecode/plate-core'
|
} from '@udecode/plate-core'
|
||||||
import { editorStyle, platePlugins } from 'libs/plate'
|
import { editorStyle, platePlugins } from 'libs/plate'
|
||||||
import { useDebounce } from 'use-debounce'
|
import { useDebounce } from 'use-debounce'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { createEditor } from 'slate'
|
import { createEditor } from 'slate'
|
||||||
import { ToolBar } from './ToolBar'
|
import { ToolBar } from './ToolBar'
|
||||||
import { parseHtmlStringToPlainText } from 'services/utils'
|
import { parseHtmlStringToPlainText } from 'services/utils'
|
||||||
|
import { TextStep } from 'models'
|
||||||
|
|
||||||
type TextEditorProps = {
|
type TextEditorProps = {
|
||||||
ids: { stepId: string; blockId: string }
|
stepId: string
|
||||||
initialValue: TDescendant[]
|
initialValue: TDescendant[]
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TextEditor = ({ initialValue, ids, onClose }: TextEditorProps) => {
|
export const TextEditor = ({
|
||||||
|
initialValue,
|
||||||
|
stepId,
|
||||||
|
onClose,
|
||||||
|
}: TextEditorProps) => {
|
||||||
const editor = useMemo(
|
const editor = useMemo(
|
||||||
() => withPlate(createEditor(), { id: ids.stepId, plugins: platePlugins }),
|
() => withPlate(createEditor(), { id: stepId, plugins: platePlugins }),
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
@ -48,13 +53,13 @@ export const TextEditor = ({ initialValue, ids, onClose }: TextEditorProps) => {
|
|||||||
const html = serializeHtml(editor, {
|
const html = serializeHtml(editor, {
|
||||||
nodes: value,
|
nodes: value,
|
||||||
})
|
})
|
||||||
updateStep(ids, {
|
updateStep(stepId, {
|
||||||
content: {
|
content: {
|
||||||
html,
|
html,
|
||||||
richText: value,
|
richText: value,
|
||||||
plainText: parseHtmlStringToPlainText(html),
|
plainText: parseHtmlStringToPlainText(html),
|
||||||
},
|
},
|
||||||
})
|
} as TextStep)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleMouseDown = (e: React.MouseEvent) => {
|
const handleMouseDown = (e: React.MouseEvent) => {
|
||||||
@ -72,7 +77,7 @@ export const TextEditor = ({ initialValue, ids, onClose }: TextEditorProps) => {
|
|||||||
>
|
>
|
||||||
<ToolBar />
|
<ToolBar />
|
||||||
<Plate
|
<Plate
|
||||||
id={ids.stepId}
|
id={stepId}
|
||||||
editableProps={{
|
editableProps={{
|
||||||
style: editorStyle,
|
style: editorStyle,
|
||||||
autoFocus: true,
|
autoFocus: true,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useEventListener, Stack, Flex, Portal } from '@chakra-ui/react'
|
import { useEventListener, Stack, Flex, Portal } from '@chakra-ui/react'
|
||||||
import { StartStep, Step } from 'bot-engine'
|
import { Step, Table } from 'models'
|
||||||
import { useDnd } from 'contexts/DndContext'
|
import { useDnd } from 'contexts/DndContext'
|
||||||
import { Coordinates } from 'contexts/GraphContext'
|
import { Coordinates } from 'contexts/GraphContext'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
@ -12,7 +12,7 @@ export const StepsList = ({
|
|||||||
onMouseUp,
|
onMouseUp,
|
||||||
}: {
|
}: {
|
||||||
blockId: string
|
blockId: string
|
||||||
steps: Step[] | [StartStep]
|
steps: Table<Step>
|
||||||
showSortPlaceholders: boolean
|
showSortPlaceholders: boolean
|
||||||
onMouseUp: (index: number) => void
|
onMouseUp: (index: number) => void
|
||||||
}) => {
|
}) => {
|
||||||
@ -88,12 +88,12 @@ export const StepsList = ({
|
|||||||
rounded="lg"
|
rounded="lg"
|
||||||
transition={showSortPlaceholders ? 'height 200ms' : 'none'}
|
transition={showSortPlaceholders ? 'height 200ms' : 'none'}
|
||||||
/>
|
/>
|
||||||
{steps.map((step, idx) => (
|
{steps.allIds.map((stepId, idx) => (
|
||||||
<Stack key={step.id} spacing={1}>
|
<Stack key={stepId} spacing={1}>
|
||||||
<StepNode
|
<StepNode
|
||||||
key={step.id}
|
key={stepId}
|
||||||
step={step}
|
step={steps.byId[stepId]}
|
||||||
isConnectable={steps.length - 1 === idx}
|
isConnectable={steps.allIds.length - 1 === idx}
|
||||||
onMouseMoveTopOfElement={() => handleMouseOnTopOfStep(idx)}
|
onMouseMoveTopOfElement={() => handleMouseOnTopOfStep(idx)}
|
||||||
onMouseMoveBottomOfElement={() => {
|
onMouseMoveBottomOfElement={() => {
|
||||||
handleMouseOnBottomOfStep(idx)
|
handleMouseOnBottomOfStep(idx)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { useEventListener } from '@chakra-ui/hooks'
|
import { useEventListener } from '@chakra-ui/hooks'
|
||||||
import { Coordinates } from '@dnd-kit/core/dist/types'
|
import { Coordinates } from '@dnd-kit/core/dist/types'
|
||||||
import { Block } from 'bot-engine'
|
|
||||||
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
|
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
|
||||||
import {
|
import {
|
||||||
blockWidth,
|
blockWidth,
|
||||||
@ -9,7 +8,7 @@ import {
|
|||||||
stubLength,
|
stubLength,
|
||||||
useGraph,
|
useGraph,
|
||||||
} from 'contexts/GraphContext'
|
} from 'contexts/GraphContext'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React, { useMemo, useState } from 'react'
|
import React, { useMemo, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
computeFlowChartConnectorPath,
|
computeFlowChartConnectorPath,
|
||||||
@ -19,34 +18,30 @@ import { roundCorners } from 'svg-round-corners'
|
|||||||
|
|
||||||
export const DrawingEdge = () => {
|
export const DrawingEdge = () => {
|
||||||
const { graphPosition, setConnectingIds, connectingIds } = useGraph()
|
const { graphPosition, setConnectingIds, connectingIds } = useGraph()
|
||||||
const { typebot, updateTarget } = useTypebot()
|
const { typebot, updateStep } = useTypebot()
|
||||||
const { startBlock, blocks } = typebot ?? {}
|
|
||||||
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 })
|
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 })
|
||||||
|
|
||||||
const sourceBlock = useMemo(
|
const sourceBlock = useMemo(
|
||||||
() =>
|
() => connectingIds && typebot?.blocks.byId[connectingIds.source.blockId],
|
||||||
[startBlock, ...(blocks ?? [])].find(
|
|
||||||
(b) => b?.id === connectingIds?.blockId
|
|
||||||
),
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
[connectingIds]
|
[connectingIds]
|
||||||
)
|
)
|
||||||
|
|
||||||
const path = useMemo(() => {
|
const path = useMemo(() => {
|
||||||
if (!sourceBlock) return ``
|
if (!sourceBlock || !typebot) return ``
|
||||||
if (connectingIds?.target) {
|
if (connectingIds?.target) {
|
||||||
const targetedBlock = blocks?.find(
|
const targetedBlock = typebot?.blocks.byId[connectingIds.target.blockId]
|
||||||
(b) => b.id === connectingIds.target?.blockId
|
|
||||||
) as Block
|
|
||||||
const targetedStepIndex = connectingIds.target.stepId
|
const targetedStepIndex = connectingIds.target.stepId
|
||||||
? targetedBlock.steps.findIndex(
|
? targetedBlock.stepIds.findIndex(
|
||||||
(s) => s.id === connectingIds.target?.stepId
|
(stepId) => stepId === connectingIds.target?.stepId
|
||||||
)
|
)
|
||||||
: undefined
|
: undefined
|
||||||
const anchorsPosition = getAnchorsPosition(
|
const anchorsPosition = getAnchorsPosition(
|
||||||
sourceBlock,
|
sourceBlock,
|
||||||
targetedBlock,
|
targetedBlock,
|
||||||
sourceBlock?.steps.findIndex((s) => s.id === connectingIds?.stepId),
|
sourceBlock?.stepIds.findIndex(
|
||||||
|
(stepId) => stepId === connectingIds?.source.stepId
|
||||||
|
),
|
||||||
targetedStepIndex
|
targetedStepIndex
|
||||||
)
|
)
|
||||||
return computeFlowChartConnectorPath(anchorsPosition)
|
return computeFlowChartConnectorPath(anchorsPosition)
|
||||||
@ -54,7 +49,9 @@ export const DrawingEdge = () => {
|
|||||||
return computeConnectingEdgePath(
|
return computeConnectingEdgePath(
|
||||||
sourceBlock?.graphCoordinates,
|
sourceBlock?.graphCoordinates,
|
||||||
mousePosition,
|
mousePosition,
|
||||||
sourceBlock.steps.findIndex((s) => s.id === connectingIds?.stepId)
|
sourceBlock.stepIds.findIndex(
|
||||||
|
(stepId) => stepId === connectingIds?.source.stepId
|
||||||
|
)
|
||||||
)
|
)
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [sourceBlock, mousePosition])
|
}, [sourceBlock, mousePosition])
|
||||||
@ -67,7 +64,8 @@ export const DrawingEdge = () => {
|
|||||||
}
|
}
|
||||||
useEventListener('mousemove', handleMouseMove)
|
useEventListener('mousemove', handleMouseMove)
|
||||||
useEventListener('mouseup', () => {
|
useEventListener('mouseup', () => {
|
||||||
if (connectingIds?.target) updateTarget(connectingIds)
|
if (connectingIds?.target)
|
||||||
|
updateStep(connectingIds.source.stepId, { target: connectingIds.target })
|
||||||
setConnectingIds(null)
|
setConnectingIds(null)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { Block, StartStep, Step, Target } from 'bot-engine'
|
|
||||||
import { Coordinates, useGraph } from 'contexts/GraphContext'
|
import { Coordinates, useGraph } from 'contexts/GraphContext'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import {
|
import {
|
||||||
getAnchorsPosition,
|
getAnchorsPosition,
|
||||||
@ -14,58 +13,43 @@ export type AnchorsPositionProps = {
|
|||||||
totalSegments: number
|
totalSegments: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type StepWithTarget = Omit<Step | StartStep, 'target'> & {
|
export const Edge = ({ stepId }: { stepId: string }) => {
|
||||||
target: Target
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Edge = ({ step }: { step: StepWithTarget }) => {
|
|
||||||
const { typebot } = useTypebot()
|
const { typebot } = useTypebot()
|
||||||
const { previewingIds } = useGraph()
|
const { previewingIds } = useGraph()
|
||||||
|
const step = typebot?.steps.byId[stepId]
|
||||||
const isPreviewing = useMemo(
|
const isPreviewing = useMemo(
|
||||||
() =>
|
() =>
|
||||||
previewingIds.sourceId === step.blockId &&
|
previewingIds.sourceId === step?.blockId &&
|
||||||
previewingIds.targetId === step.target.blockId,
|
previewingIds.targetId === step?.target?.blockId,
|
||||||
[
|
[previewingIds.sourceId, previewingIds.targetId, step]
|
||||||
previewingIds.sourceId,
|
|
||||||
previewingIds.targetId,
|
|
||||||
step.blockId,
|
|
||||||
step.target.blockId,
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
const { blocks, startBlock } = typebot ?? {}
|
|
||||||
|
|
||||||
const { sourceBlock, targetBlock, targetStepIndex } = useMemo(() => {
|
const { sourceBlock, targetBlock, targetStepIndex } = useMemo(() => {
|
||||||
const targetBlock = blocks?.find(
|
if (!typebot) return {}
|
||||||
(b) => b?.id === step.target.blockId
|
const step = typebot.steps.byId[stepId]
|
||||||
) as Block
|
if (!step.target) return {}
|
||||||
|
const sourceBlock = typebot.blocks.byId[step.blockId]
|
||||||
|
const targetBlock = typebot.blocks.byId[step.target.blockId]
|
||||||
const targetStepIndex = step.target.stepId
|
const targetStepIndex = step.target.stepId
|
||||||
? targetBlock.steps.findIndex((s) => s.id === step.target.stepId)
|
? targetBlock.stepIds.indexOf(step.target.stepId)
|
||||||
: undefined
|
: undefined
|
||||||
return {
|
return {
|
||||||
sourceBlock: [startBlock, ...(blocks ?? [])].find(
|
sourceBlock,
|
||||||
(b) => b?.id === step.blockId
|
|
||||||
),
|
|
||||||
targetBlock,
|
targetBlock,
|
||||||
targetStepIndex,
|
targetStepIndex,
|
||||||
}
|
}
|
||||||
}, [
|
}, [stepId, typebot])
|
||||||
blocks,
|
|
||||||
startBlock,
|
|
||||||
step.blockId,
|
|
||||||
step.target.blockId,
|
|
||||||
step.target.stepId,
|
|
||||||
])
|
|
||||||
|
|
||||||
const path = useMemo(() => {
|
const path = useMemo(() => {
|
||||||
if (!sourceBlock || !targetBlock) return ``
|
if (!sourceBlock || !targetBlock) return ``
|
||||||
const anchorsPosition = getAnchorsPosition(
|
const anchorsPosition = getAnchorsPosition(
|
||||||
sourceBlock,
|
sourceBlock,
|
||||||
targetBlock,
|
targetBlock,
|
||||||
sourceBlock.steps.findIndex((s) => s.id === step.id),
|
sourceBlock.stepIds.indexOf(stepId),
|
||||||
targetStepIndex
|
targetStepIndex
|
||||||
)
|
)
|
||||||
return computeFlowChartConnectorPath(anchorsPosition)
|
return computeFlowChartConnectorPath(anchorsPosition)
|
||||||
}, [sourceBlock, step.id, targetBlock, targetStepIndex])
|
}, [sourceBlock, stepId, targetBlock, targetStepIndex])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<path
|
<path
|
||||||
|
@ -1,21 +1,18 @@
|
|||||||
import { chakra } from '@chakra-ui/system'
|
import { chakra } from '@chakra-ui/system'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
|
import { isDefined } from 'utils'
|
||||||
import { DrawingEdge } from './DrawingEdge'
|
import { DrawingEdge } from './DrawingEdge'
|
||||||
import { Edge, StepWithTarget } from './Edge'
|
import { Edge } from './Edge'
|
||||||
|
|
||||||
export const Edges = () => {
|
export const Edges = () => {
|
||||||
const { typebot } = useTypebot()
|
const { typebot } = useTypebot()
|
||||||
const { blocks, startBlock } = typebot ?? {}
|
const stepIdsWithTarget: string[] = useMemo(() => {
|
||||||
const stepsWithTarget: StepWithTarget[] = useMemo(() => {
|
if (!typebot) return []
|
||||||
if (!startBlock) return []
|
return typebot.steps.allIds.filter((stepId) =>
|
||||||
return [
|
isDefined(typebot.steps.byId[stepId].target)
|
||||||
...(startBlock.steps.filter((s) => s.target) as StepWithTarget[]),
|
)
|
||||||
...((blocks ?? [])
|
}, [typebot])
|
||||||
.flatMap((b) => b.steps)
|
|
||||||
.filter((s) => s.target) as StepWithTarget[]),
|
|
||||||
]
|
|
||||||
}, [blocks, startBlock])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<chakra.svg
|
<chakra.svg
|
||||||
@ -27,8 +24,8 @@ export const Edges = () => {
|
|||||||
top="0"
|
top="0"
|
||||||
>
|
>
|
||||||
<DrawingEdge />
|
<DrawingEdge />
|
||||||
{stepsWithTarget.map((step) => (
|
{stepIdsWithTarget.map((stepId) => (
|
||||||
<Edge key={step.id} step={step} />
|
<Edge key={stepId} stepId={stepId} />
|
||||||
))}
|
))}
|
||||||
<marker
|
<marker
|
||||||
id={'arrow'}
|
id={'arrow'}
|
||||||
|
@ -4,15 +4,15 @@ import { blockWidth, useGraph } from 'contexts/GraphContext'
|
|||||||
import { BlockNode } from './BlockNode/BlockNode'
|
import { BlockNode } from './BlockNode/BlockNode'
|
||||||
import { useDnd } from 'contexts/DndContext'
|
import { useDnd } from 'contexts/DndContext'
|
||||||
import { Edges } from './Edges'
|
import { Edges } from './Edges'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { StartBlockNode } from './BlockNode/StartBlockNode'
|
|
||||||
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
|
import { headerHeight } from 'components/shared/TypebotHeader/TypebotHeader'
|
||||||
|
import { StepType } from 'models'
|
||||||
|
|
||||||
const Graph = ({ ...props }: FlexProps) => {
|
const Graph = ({ ...props }: FlexProps) => {
|
||||||
const { draggedStepType, setDraggedStepType, draggedStep, setDraggedStep } =
|
const { draggedStepType, setDraggedStepType, draggedStep, setDraggedStep } =
|
||||||
useDnd()
|
useDnd()
|
||||||
const graphContainerRef = useRef<HTMLDivElement | null>(null)
|
const graphContainerRef = useRef<HTMLDivElement | null>(null)
|
||||||
const { addNewBlock, typebot } = useTypebot()
|
const { createBlock, typebot } = useTypebot()
|
||||||
const { graphPosition, setGraphPosition } = useGraph()
|
const { graphPosition, setGraphPosition } = useGraph()
|
||||||
const transform = useMemo(
|
const transform = useMemo(
|
||||||
() =>
|
() =>
|
||||||
@ -41,11 +41,10 @@ const Graph = ({ ...props }: FlexProps) => {
|
|||||||
|
|
||||||
const handleMouseUp = (e: MouseEvent) => {
|
const handleMouseUp = (e: MouseEvent) => {
|
||||||
if (!draggedStep && !draggedStepType) return
|
if (!draggedStep && !draggedStepType) return
|
||||||
addNewBlock({
|
createBlock({
|
||||||
step: draggedStep,
|
|
||||||
type: draggedStepType,
|
|
||||||
x: e.clientX - graphPosition.x - blockWidth / 3,
|
x: e.clientX - graphPosition.x - blockWidth / 3,
|
||||||
y: e.clientY - graphPosition.y - 20 - headerHeight,
|
y: e.clientY - graphPosition.y - 20 - headerHeight,
|
||||||
|
step: draggedStep ?? (draggedStepType as StepType),
|
||||||
})
|
})
|
||||||
setDraggedStep(undefined)
|
setDraggedStep(undefined)
|
||||||
setDraggedStepType(undefined)
|
setDraggedStepType(undefined)
|
||||||
@ -71,9 +70,8 @@ const Graph = ({ ...props }: FlexProps) => {
|
|||||||
>
|
>
|
||||||
<Edges />
|
<Edges />
|
||||||
{props.children}
|
{props.children}
|
||||||
{typebot.startBlock && <StartBlockNode block={typebot.startBlock} />}
|
{typebot.blocks.allIds.map((blockId) => (
|
||||||
{(typebot.blocks ?? []).map((block) => (
|
<BlockNode block={typebot.blocks.byId[blockId]} key={blockId} />
|
||||||
<BlockNode block={block} key={block.id} />
|
|
||||||
))}
|
))}
|
||||||
</Flex>
|
</Flex>
|
||||||
</Flex>
|
</Flex>
|
||||||
|
@ -12,7 +12,7 @@ import { TypebotViewer } from 'bot-engine'
|
|||||||
import { headerHeight } from 'components/shared/TypebotHeader'
|
import { headerHeight } from 'components/shared/TypebotHeader'
|
||||||
import { useEditor } from 'contexts/EditorContext'
|
import { useEditor } from 'contexts/EditorContext'
|
||||||
import { useGraph } from 'contexts/GraphContext'
|
import { useGraph } from 'contexts/GraphContext'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React, { useMemo, useState } from 'react'
|
import React, { useMemo, useState } from 'react'
|
||||||
import { parseTypebotToPublicTypebot } from 'services/publicTypebot'
|
import { parseTypebotToPublicTypebot } from 'services/publicTypebot'
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { DashboardFolder } from '.prisma/client'
|
import { DashboardFolder } from '.prisma/client'
|
||||||
import { Typebot } from 'bot-engine'
|
import { Typebot } from 'models'
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Flex,
|
Flex,
|
||||||
|
@ -16,7 +16,7 @@ import { MoreButton } from 'components/MoreButton'
|
|||||||
import { ConfirmModal } from 'components/modals/ConfirmModal'
|
import { ConfirmModal } from 'components/modals/ConfirmModal'
|
||||||
import { GlobeIcon, ToolIcon } from 'assets/icons'
|
import { GlobeIcon, ToolIcon } from 'assets/icons'
|
||||||
import { deleteTypebot, duplicateTypebot } from 'services/typebots'
|
import { deleteTypebot, duplicateTypebot } from 'services/typebots'
|
||||||
import { Typebot } from 'bot-engine'
|
import { Typebot } from 'models'
|
||||||
|
|
||||||
type ChatbotCardProps = {
|
type ChatbotCardProps = {
|
||||||
typebot: Typebot
|
typebot: Typebot
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Button, Flex, Text, VStack } from '@chakra-ui/react'
|
import { Button, Flex, Text, VStack } from '@chakra-ui/react'
|
||||||
import { GlobeIcon, ToolIcon } from 'assets/icons'
|
import { GlobeIcon, ToolIcon } from 'assets/icons'
|
||||||
import { Typebot } from 'bot-engine'
|
import { Typebot } from 'models'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
typebot: Typebot
|
typebot: Typebot
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable react/jsx-key */
|
/* eslint-disable react/jsx-key */
|
||||||
import { Box, Checkbox, Flex } from '@chakra-ui/react'
|
import { Box, Checkbox, Flex } from '@chakra-ui/react'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React, { useEffect, useMemo, useRef } from 'react'
|
import React, { useEffect, useMemo, useRef } from 'react'
|
||||||
import { Hooks, useFlexLayout, useRowSelect, useTable } from 'react-table'
|
import { Hooks, useFlexLayout, useRowSelect, useTable } from 'react-table'
|
||||||
import { parseSubmissionsColumns } from 'services/publicTypebot'
|
import { parseSubmissionsColumns } from 'services/publicTypebot'
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
import { Flex, Stack } from '@chakra-ui/react'
|
import { Flex, Stack } from '@chakra-ui/react'
|
||||||
import { TypingEmulationSettings } from 'bot-engine'
|
import { TypingEmulationSettings } from 'models'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { TypingEmulation } from './TypingEmulation'
|
import { TypingEmulation } from './TypingEmulation'
|
||||||
|
|
||||||
export const SettingsContent = () => {
|
export const SettingsContent = () => {
|
||||||
const { typebot, updateSettings } = useTypebot()
|
const { typebot, updateTypebot } = useTypebot()
|
||||||
|
|
||||||
const handleTypingEmulationUpdate = (
|
const handleTypingEmulationUpdate = (
|
||||||
typingEmulation: TypingEmulationSettings
|
typingEmulation: TypingEmulationSettings
|
||||||
) => {
|
) => {
|
||||||
if (!typebot) return
|
if (!typebot) return
|
||||||
updateSettings({ ...typebot.settings, typingEmulation })
|
updateTypebot({ settings: { ...typebot.settings, typingEmulation } })
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<Flex h="full" w="full" justifyContent="center" align="flex-start">
|
<Flex h="full" w="full" justifyContent="center" align="flex-start">
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Flex, Stack, Switch, Text } from '@chakra-ui/react'
|
import { Flex, Stack, Switch, Text } from '@chakra-ui/react'
|
||||||
import { TypingEmulationSettings } from 'bot-engine'
|
import { TypingEmulationSettings } from 'models'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { SmartNumberInput } from './SmartNumberInput'
|
import { SmartNumberInput } from './SmartNumberInput'
|
||||||
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import { Flex, Heading, Stack } from '@chakra-ui/react'
|
import { Flex, Heading, Stack } from '@chakra-ui/react'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { parseDefaultPublicId } from 'services/typebots'
|
import { parseDefaultPublicId } from 'services/typebots'
|
||||||
import { EditableUrl } from './EditableUrl'
|
import { EditableUrl } from './EditableUrl'
|
||||||
|
|
||||||
export const ShareContent = () => {
|
export const ShareContent = () => {
|
||||||
const { typebot, updatePublicId } = useTypebot()
|
const { typebot, updateTypebot } = useTypebot()
|
||||||
|
|
||||||
const handlePublicIdChange = (publicId: string) => {
|
const handlePublicIdChange = (publicId: string) => {
|
||||||
if (publicId === typebot?.publicId) return
|
if (publicId === typebot?.publicId) return
|
||||||
updatePublicId(publicId)
|
updateTypebot({ publicId })
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<Flex h="full" w="full" justifyContent="center" align="flex-start">
|
<Flex h="full" w="full" justifyContent="center" align="flex-start">
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { IconButton, Text, Tooltip } from '@chakra-ui/react'
|
import { IconButton, Text, Tooltip } from '@chakra-ui/react'
|
||||||
import { CheckIcon, SaveIcon } from 'assets/icons'
|
import { CheckIcon, SaveIcon } from 'assets/icons'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
export const SaveButton = () => {
|
export const SaveButton = () => {
|
||||||
|
@ -2,7 +2,7 @@ import { Flex, HStack, Button, IconButton } from '@chakra-ui/react'
|
|||||||
import { ChevronLeftIcon } from 'assets/icons'
|
import { ChevronLeftIcon } from 'assets/icons'
|
||||||
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
|
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
|
||||||
import { RightPanel, useEditor } from 'contexts/EditorContext'
|
import { RightPanel, useEditor } from 'contexts/EditorContext'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { PublishButton } from '../buttons/PublishButton'
|
import { PublishButton } from '../buttons/PublishButton'
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Button } from '@chakra-ui/react'
|
import { Button } from '@chakra-ui/react'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
|
|
||||||
export const PublishButton = () => {
|
export const PublishButton = () => {
|
||||||
const { isPublishing, isPublished, publishTypebot } = useTypebot()
|
const { isPublishing, isPublished, publishTypebot } = useTypebot()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Flex, Text } from '@chakra-ui/react'
|
import { Flex, Text } from '@chakra-ui/react'
|
||||||
import { Background, BackgroundType } from 'bot-engine'
|
import { Background, BackgroundType } from 'models'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { ColorPicker } from '../ColorPicker'
|
import { ColorPicker } from '../ColorPicker'
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Stack, Text } from '@chakra-ui/react'
|
import { Stack, Text } from '@chakra-ui/react'
|
||||||
import { Background, BackgroundType } from 'bot-engine'
|
import { Background, BackgroundType } from 'models'
|
||||||
import { deepEqual } from 'fast-equals'
|
import { deepEqual } from 'fast-equals'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { BackgroundContent } from './BackgroundContent'
|
import { BackgroundContent } from './BackgroundContent'
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
useRadioGroup,
|
useRadioGroup,
|
||||||
UseRadioProps,
|
UseRadioProps,
|
||||||
} from '@chakra-ui/react'
|
} from '@chakra-ui/react'
|
||||||
import { BackgroundType } from 'bot-engine'
|
import { BackgroundType } from 'models'
|
||||||
import { ReactNode } from 'react'
|
import { ReactNode } from 'react'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
@ -8,28 +8,32 @@ import {
|
|||||||
Stack,
|
Stack,
|
||||||
} from '@chakra-ui/react'
|
} from '@chakra-ui/react'
|
||||||
import { PencilIcon } from 'assets/icons'
|
import { PencilIcon } from 'assets/icons'
|
||||||
import { Background } from 'bot-engine'
|
import { Background } from 'models'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { BackgroundSelector } from './BackgroundSelector'
|
import { BackgroundSelector } from './BackgroundSelector'
|
||||||
import { FontSelector } from './FontSelector'
|
import { FontSelector } from './FontSelector'
|
||||||
|
|
||||||
export const GeneralContent = () => {
|
export const GeneralContent = () => {
|
||||||
const { typebot, updateTheme } = useTypebot()
|
const { typebot, updateTypebot } = useTypebot()
|
||||||
|
|
||||||
const handleSelectFont = (font: string) => {
|
const handleSelectFont = (font: string) => {
|
||||||
if (!typebot) return
|
if (!typebot) return
|
||||||
updateTheme({
|
updateTypebot({
|
||||||
...typebot.theme,
|
theme: {
|
||||||
general: { ...typebot.theme.general, font },
|
...typebot.theme,
|
||||||
|
general: { ...typebot.theme.general, font },
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleBackgroundChange = (background: Background) => {
|
const handleBackgroundChange = (background: Background) => {
|
||||||
if (!typebot) return
|
if (!typebot) return
|
||||||
updateTheme({
|
updateTypebot({
|
||||||
...typebot.theme,
|
theme: {
|
||||||
general: { ...typebot.theme.general, background },
|
...typebot.theme,
|
||||||
|
general: { ...typebot.theme.general, background },
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Flex } from '@chakra-ui/react'
|
import { Flex } from '@chakra-ui/react'
|
||||||
import { TypebotViewer } from 'bot-engine'
|
import { TypebotViewer } from 'bot-engine'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import { parseTypebotToPublicTypebot } from 'services/publicTypebot'
|
import { parseTypebotToPublicTypebot } from 'services/publicTypebot'
|
||||||
import { SideMenu } from './SideMenu'
|
import { SideMenu } from './SideMenu'
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Block, PublicTypebot } from 'bot-engine'
|
import { PublicTypebot } from 'models'
|
||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
Dispatch,
|
Dispatch,
|
||||||
@ -8,6 +8,7 @@ import {
|
|||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { Coordinates } from './GraphContext'
|
import { Coordinates } from './GraphContext'
|
||||||
|
import produce from 'immer'
|
||||||
|
|
||||||
type Position = Coordinates & { scale: number }
|
type Position = Coordinates & { scale: number }
|
||||||
|
|
||||||
@ -35,31 +36,13 @@ export const AnalyticsGraphProvider = ({
|
|||||||
|
|
||||||
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
|
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
|
||||||
|
|
||||||
const updateBlocks = (blocks: Block[]) => {
|
|
||||||
if (!typebot) return
|
|
||||||
setTypebot({
|
|
||||||
...typebot,
|
|
||||||
blocks: [...blocks],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateBlockPosition = (blockId: string, newPosition: Coordinates) => {
|
const updateBlockPosition = (blockId: string, newPosition: Coordinates) => {
|
||||||
if (!typebot) return
|
if (!typebot) return
|
||||||
blockId === 'start-block'
|
setTypebot(
|
||||||
? setTypebot({
|
produce(typebot, (nextTypebot) => {
|
||||||
...typebot,
|
nextTypebot.blocks.byId[blockId].graphCoordinates = newPosition
|
||||||
startBlock: {
|
})
|
||||||
...typebot.startBlock,
|
)
|
||||||
graphCoordinates: newPosition,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: updateBlocks(
|
|
||||||
typebot.blocks.map((block) =>
|
|
||||||
block.id === blockId
|
|
||||||
? { ...block, graphCoordinates: newPosition }
|
|
||||||
: block
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Step, StepType } from 'bot-engine'
|
import { Step, StepType } from 'models'
|
||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
Dispatch,
|
Dispatch,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Block, Step, StepType, Target } from 'bot-engine'
|
import { Block, Step, Target } from 'models'
|
||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
Dispatch,
|
Dispatch,
|
||||||
@ -41,26 +41,22 @@ export type Node = Omit<Block, 'steps'> & {
|
|||||||
})[]
|
})[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NewBlockPayload = {
|
|
||||||
x: number
|
|
||||||
y: number
|
|
||||||
type?: StepType
|
|
||||||
step?: Step
|
|
||||||
}
|
|
||||||
|
|
||||||
const graphPositionDefaultValue = { x: 400, y: 100, scale: 1 }
|
const graphPositionDefaultValue = { x: 400, y: 100, scale: 1 }
|
||||||
|
|
||||||
|
type ConnectingIdsProps = {
|
||||||
|
source: { blockId: string; stepId: string }
|
||||||
|
target?: Target
|
||||||
|
} | null
|
||||||
|
|
||||||
|
type PreviewingIdsProps = { sourceId?: string; targetId?: string }
|
||||||
|
|
||||||
const graphContext = createContext<{
|
const graphContext = createContext<{
|
||||||
graphPosition: Position
|
graphPosition: Position
|
||||||
setGraphPosition: Dispatch<SetStateAction<Position>>
|
setGraphPosition: Dispatch<SetStateAction<Position>>
|
||||||
connectingIds: { blockId: string; stepId: string; target?: Target } | null
|
connectingIds: ConnectingIdsProps
|
||||||
setConnectingIds: Dispatch<
|
setConnectingIds: Dispatch<SetStateAction<ConnectingIdsProps>>
|
||||||
SetStateAction<{ blockId: string; stepId: string; target?: Target } | null>
|
previewingIds: PreviewingIdsProps
|
||||||
>
|
setPreviewingIds: Dispatch<SetStateAction<PreviewingIdsProps>>
|
||||||
previewingIds: { sourceId?: string; targetId?: string }
|
|
||||||
setPreviewingIds: Dispatch<
|
|
||||||
SetStateAction<{ sourceId?: string; targetId?: string }>
|
|
||||||
>
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
}>({
|
}>({
|
||||||
@ -70,15 +66,8 @@ const graphContext = createContext<{
|
|||||||
|
|
||||||
export const GraphProvider = ({ children }: { children: ReactNode }) => {
|
export const GraphProvider = ({ children }: { children: ReactNode }) => {
|
||||||
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
|
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
|
||||||
const [connectingIds, setConnectingIds] = useState<{
|
const [connectingIds, setConnectingIds] = useState<ConnectingIdsProps>(null)
|
||||||
blockId: string
|
const [previewingIds, setPreviewingIds] = useState<PreviewingIdsProps>({})
|
||||||
stepId: string
|
|
||||||
target?: Target
|
|
||||||
} | null>(null)
|
|
||||||
const [previewingIds, setPreviewingIds] = useState<{
|
|
||||||
sourceId?: string
|
|
||||||
targetId?: string
|
|
||||||
}>({})
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<graphContext.Provider
|
<graphContext.Provider
|
||||||
|
@ -1,402 +0,0 @@
|
|||||||
import { useToast } from '@chakra-ui/react'
|
|
||||||
import {
|
|
||||||
Block,
|
|
||||||
PublicTypebot,
|
|
||||||
Settings,
|
|
||||||
Step,
|
|
||||||
StepType,
|
|
||||||
Target,
|
|
||||||
Theme,
|
|
||||||
Typebot,
|
|
||||||
} from 'bot-engine'
|
|
||||||
import { deepEqual } from 'fast-equals'
|
|
||||||
import { useRouter } from 'next/router'
|
|
||||||
import {
|
|
||||||
createContext,
|
|
||||||
ReactNode,
|
|
||||||
useContext,
|
|
||||||
useEffect,
|
|
||||||
useMemo,
|
|
||||||
useState,
|
|
||||||
} from 'react'
|
|
||||||
import {
|
|
||||||
createPublishedTypebot,
|
|
||||||
parseTypebotToPublicTypebot,
|
|
||||||
updatePublishedTypebot,
|
|
||||||
} from 'services/publicTypebot'
|
|
||||||
import {
|
|
||||||
checkIfPublished,
|
|
||||||
checkIfTypebotsAreEqual,
|
|
||||||
parseDefaultPublicId,
|
|
||||||
parseNewBlock,
|
|
||||||
parseNewStep,
|
|
||||||
updateTypebot,
|
|
||||||
} from 'services/typebots'
|
|
||||||
import {
|
|
||||||
fetcher,
|
|
||||||
insertItemInList,
|
|
||||||
omit,
|
|
||||||
preventUserFromRefreshing,
|
|
||||||
} from 'services/utils'
|
|
||||||
import useSWR from 'swr'
|
|
||||||
import { isDefined } from 'utils'
|
|
||||||
import { NewBlockPayload, Coordinates } from './GraphContext'
|
|
||||||
|
|
||||||
const typebotContext = createContext<{
|
|
||||||
typebot?: Typebot
|
|
||||||
publishedTypebot?: PublicTypebot
|
|
||||||
isPublished: boolean
|
|
||||||
isPublishing: boolean
|
|
||||||
hasUnsavedChanges: boolean
|
|
||||||
isSavingLoading: boolean
|
|
||||||
save: () => void
|
|
||||||
updateStep: (
|
|
||||||
ids: { stepId: string; blockId: string },
|
|
||||||
updates: Partial<Step>
|
|
||||||
) => void
|
|
||||||
addNewBlock: (props: NewBlockPayload) => void
|
|
||||||
updateBlockPosition: (blockId: string, newPositon: Coordinates) => void
|
|
||||||
removeBlock: (blockId: string) => void
|
|
||||||
addStepToBlock: (
|
|
||||||
blockId: string,
|
|
||||||
step: StepType | Step,
|
|
||||||
index: number
|
|
||||||
) => void
|
|
||||||
removeStepFromBlock: (blockId: string, stepId: string) => void
|
|
||||||
updateTarget: (connectingIds: {
|
|
||||||
blockId: string
|
|
||||||
stepId: string
|
|
||||||
target?: Target
|
|
||||||
}) => void
|
|
||||||
undo: () => void
|
|
||||||
updateTheme: (theme: Theme) => void
|
|
||||||
updateSettings: (settings: Settings) => void
|
|
||||||
updatePublicId: (publicId: string) => void
|
|
||||||
publishTypebot: () => void
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
||||||
//@ts-ignore
|
|
||||||
}>({})
|
|
||||||
|
|
||||||
export const TypebotContext = ({
|
|
||||||
children,
|
|
||||||
typebotId,
|
|
||||||
}: {
|
|
||||||
children: ReactNode
|
|
||||||
typebotId?: string
|
|
||||||
}) => {
|
|
||||||
const router = useRouter()
|
|
||||||
const toast = useToast({
|
|
||||||
position: 'top-right',
|
|
||||||
status: 'error',
|
|
||||||
})
|
|
||||||
const [undoStack, setUndoStack] = useState<Typebot[]>([])
|
|
||||||
const { typebot, publishedTypebot, isLoading, mutate } = useFetchedTypebot({
|
|
||||||
typebotId,
|
|
||||||
onError: (error) =>
|
|
||||||
toast({
|
|
||||||
title: 'Error while fetching typebot',
|
|
||||||
description: error.message,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
const [localTypebot, setLocalTypebot] = useState<Typebot>()
|
|
||||||
const [localPublishedTypebot, setLocalPublishedTypebot] =
|
|
||||||
useState<PublicTypebot>()
|
|
||||||
const [isSavingLoading, setIsSavingLoading] = useState(false)
|
|
||||||
const [isPublishing, setIsPublishing] = useState(false)
|
|
||||||
|
|
||||||
const hasUnsavedChanges = useMemo(
|
|
||||||
() =>
|
|
||||||
isDefined(typebot) &&
|
|
||||||
isDefined(localTypebot) &&
|
|
||||||
!deepEqual(localTypebot, typebot),
|
|
||||||
[typebot, localTypebot]
|
|
||||||
)
|
|
||||||
const isPublished = useMemo(
|
|
||||||
() =>
|
|
||||||
isDefined(typebot) &&
|
|
||||||
isDefined(publishedTypebot) &&
|
|
||||||
checkIfPublished(typebot, publishedTypebot),
|
|
||||||
[typebot, publishedTypebot]
|
|
||||||
)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!localTypebot || !typebot) return
|
|
||||||
if (!checkIfTypebotsAreEqual(localTypebot, typebot)) {
|
|
||||||
pushNewTypebotInUndoStack(localTypebot)
|
|
||||||
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
|
||||||
window.addEventListener('beforeunload', preventUserFromRefreshing)
|
|
||||||
} else {
|
|
||||||
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
|
||||||
}
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [localTypebot])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (isLoading) return
|
|
||||||
if (!typebot) {
|
|
||||||
toast({ status: 'info', description: "Couldn't find typebot" })
|
|
||||||
router.replace('/typebots')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setLocalTypebot({ ...typebot })
|
|
||||||
if (publishedTypebot) setLocalPublishedTypebot({ ...publishedTypebot })
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [isLoading])
|
|
||||||
|
|
||||||
const pushNewTypebotInUndoStack = (typebot: Typebot) => {
|
|
||||||
setUndoStack([...undoStack, typebot])
|
|
||||||
}
|
|
||||||
|
|
||||||
const undo = () => {
|
|
||||||
const lastTypebot = [...undoStack].pop()
|
|
||||||
setUndoStack(undoStack.slice(0, -1))
|
|
||||||
setLocalTypebot(lastTypebot)
|
|
||||||
}
|
|
||||||
|
|
||||||
const saveTypebot = async () => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
setIsSavingLoading(true)
|
|
||||||
const { error } = await updateTypebot(localTypebot.id, localTypebot)
|
|
||||||
setIsSavingLoading(false)
|
|
||||||
if (error) return toast({ title: error.name, description: error.message })
|
|
||||||
mutate({ typebot: localTypebot })
|
|
||||||
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateBlocks = (blocks: Block[]) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
setLocalTypebot({
|
|
||||||
...localTypebot,
|
|
||||||
blocks: [...blocks],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateStep = (
|
|
||||||
{ blockId, stepId }: { blockId: string; stepId: string },
|
|
||||||
updates: Partial<Omit<Step, 'id' | 'type'>>
|
|
||||||
) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
setLocalTypebot({
|
|
||||||
...localTypebot,
|
|
||||||
blocks: localTypebot.blocks.map((block) =>
|
|
||||||
block.id === blockId
|
|
||||||
? {
|
|
||||||
...block,
|
|
||||||
steps: block.steps.map((step) =>
|
|
||||||
step.id === stepId ? { ...step, ...updates } : step
|
|
||||||
),
|
|
||||||
}
|
|
||||||
: block
|
|
||||||
),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const addNewBlock = ({ x, y, type, step }: NewBlockPayload) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
updateBlocks([
|
|
||||||
...localTypebot.blocks.filter((block) => block.steps.length > 0),
|
|
||||||
parseNewBlock({
|
|
||||||
step,
|
|
||||||
type,
|
|
||||||
totalBlocks: localTypebot.blocks.length,
|
|
||||||
initialCoordinates: {
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateBlockPosition = (blockId: string, newPosition: Coordinates) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
blockId === 'start-block'
|
|
||||||
? setLocalTypebot({
|
|
||||||
...localTypebot,
|
|
||||||
startBlock: {
|
|
||||||
...localTypebot.startBlock,
|
|
||||||
graphCoordinates: newPosition,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: updateBlocks(
|
|
||||||
localTypebot.blocks.map((block) =>
|
|
||||||
block.id === blockId
|
|
||||||
? { ...block, graphCoordinates: newPosition }
|
|
||||||
: block
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const addStepToBlock = (
|
|
||||||
blockId: string,
|
|
||||||
step: StepType | Step,
|
|
||||||
index: number
|
|
||||||
) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
updateBlocks(
|
|
||||||
localTypebot.blocks
|
|
||||||
.map((block) =>
|
|
||||||
block.id === blockId
|
|
||||||
? {
|
|
||||||
...block,
|
|
||||||
steps: insertItemInList<Step>(
|
|
||||||
block.steps,
|
|
||||||
index,
|
|
||||||
typeof step === 'string'
|
|
||||||
? parseNewStep(step as StepType, block.id)
|
|
||||||
: { ...step, blockId: block.id }
|
|
||||||
),
|
|
||||||
}
|
|
||||||
: block
|
|
||||||
)
|
|
||||||
.filter((block) => block.steps.length > 0)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const removeStepFromBlock = (blockId: string, stepId: string) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
updateBlocks(
|
|
||||||
localTypebot.blocks.map((block) =>
|
|
||||||
block.id === blockId
|
|
||||||
? {
|
|
||||||
...block,
|
|
||||||
steps: [...block.steps.filter((step) => step.id !== stepId)],
|
|
||||||
}
|
|
||||||
: block
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateTarget = ({
|
|
||||||
blockId,
|
|
||||||
stepId,
|
|
||||||
target,
|
|
||||||
}: {
|
|
||||||
blockId: string
|
|
||||||
stepId: string
|
|
||||||
target?: Target
|
|
||||||
}) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
blockId === 'start-block'
|
|
||||||
? setLocalTypebot({
|
|
||||||
...localTypebot,
|
|
||||||
startBlock: {
|
|
||||||
...localTypebot.startBlock,
|
|
||||||
steps: [{ ...localTypebot.startBlock.steps[0], target }],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: updateBlocks(
|
|
||||||
localTypebot.blocks.map((block) =>
|
|
||||||
block.id === blockId
|
|
||||||
? {
|
|
||||||
...block,
|
|
||||||
steps: [
|
|
||||||
...block.steps.map((step) =>
|
|
||||||
step.id === stepId ? { ...step, target } : step
|
|
||||||
),
|
|
||||||
],
|
|
||||||
}
|
|
||||||
: block
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const removeBlock = (blockId: string) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
const blocks = [...localTypebot.blocks.filter((b) => b.id !== blockId)]
|
|
||||||
setLocalTypebot({ ...localTypebot, blocks })
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateTheme = (theme: Theme) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
setLocalTypebot({ ...localTypebot, theme })
|
|
||||||
}
|
|
||||||
|
|
||||||
const updateSettings = (settings: Settings) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
setLocalTypebot({ ...localTypebot, settings })
|
|
||||||
}
|
|
||||||
|
|
||||||
const updatePublicId = (publicId: string) => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
setLocalTypebot({ ...localTypebot, publicId })
|
|
||||||
}
|
|
||||||
|
|
||||||
const publishTypebot = async () => {
|
|
||||||
if (!localTypebot) return
|
|
||||||
if (!localPublishedTypebot) {
|
|
||||||
const newPublicId = parseDefaultPublicId(
|
|
||||||
localTypebot.name,
|
|
||||||
localTypebot.id
|
|
||||||
)
|
|
||||||
updatePublicId(newPublicId)
|
|
||||||
localTypebot.publicId = newPublicId
|
|
||||||
}
|
|
||||||
if (hasUnsavedChanges || !localPublishedTypebot) await saveTypebot()
|
|
||||||
setIsPublishing(true)
|
|
||||||
if (localPublishedTypebot) {
|
|
||||||
const { error } = await updatePublishedTypebot(
|
|
||||||
localPublishedTypebot.id,
|
|
||||||
omit(parseTypebotToPublicTypebot(localTypebot), 'id')
|
|
||||||
)
|
|
||||||
setIsPublishing(false)
|
|
||||||
if (error) return toast({ title: error.name, description: error.message })
|
|
||||||
} else {
|
|
||||||
const { error } = await createPublishedTypebot(
|
|
||||||
omit(parseTypebotToPublicTypebot(localTypebot), 'id')
|
|
||||||
)
|
|
||||||
setIsPublishing(false)
|
|
||||||
if (error) return toast({ title: error.name, description: error.message })
|
|
||||||
}
|
|
||||||
mutate({ typebot: localTypebot })
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<typebotContext.Provider
|
|
||||||
value={{
|
|
||||||
typebot: localTypebot,
|
|
||||||
publishedTypebot: localPublishedTypebot,
|
|
||||||
updateStep,
|
|
||||||
addNewBlock,
|
|
||||||
addStepToBlock,
|
|
||||||
updateTarget,
|
|
||||||
removeStepFromBlock,
|
|
||||||
updateBlockPosition,
|
|
||||||
hasUnsavedChanges,
|
|
||||||
isSavingLoading,
|
|
||||||
save: saveTypebot,
|
|
||||||
removeBlock,
|
|
||||||
undo,
|
|
||||||
updateTheme,
|
|
||||||
updateSettings,
|
|
||||||
updatePublicId,
|
|
||||||
publishTypebot,
|
|
||||||
isPublishing,
|
|
||||||
isPublished,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</typebotContext.Provider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useTypebot = () => useContext(typebotContext)
|
|
||||||
|
|
||||||
export const useFetchedTypebot = ({
|
|
||||||
typebotId,
|
|
||||||
onError,
|
|
||||||
}: {
|
|
||||||
typebotId?: string
|
|
||||||
onError: (error: Error) => void
|
|
||||||
}) => {
|
|
||||||
const { data, error, mutate } = useSWR<
|
|
||||||
{ typebot: Typebot; publishedTypebot?: PublicTypebot },
|
|
||||||
Error
|
|
||||||
>(typebotId ? `/api/typebots/${typebotId}` : null, fetcher)
|
|
||||||
if (error) onError(error)
|
|
||||||
return {
|
|
||||||
typebot: data?.typebot,
|
|
||||||
publishedTypebot: data?.publishedTypebot,
|
|
||||||
isLoading: !error && !data,
|
|
||||||
mutate,
|
|
||||||
}
|
|
||||||
}
|
|
232
apps/builder/contexts/TypebotContext/TypebotContext.tsx
Normal file
232
apps/builder/contexts/TypebotContext/TypebotContext.tsx
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
import { useToast } from '@chakra-ui/react'
|
||||||
|
import { deepEqual } from 'fast-equals'
|
||||||
|
import { PublicTypebot, Settings, Theme, Typebot } from 'models'
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
|
import {
|
||||||
|
createContext,
|
||||||
|
ReactNode,
|
||||||
|
useContext,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useState,
|
||||||
|
} from 'react'
|
||||||
|
import {
|
||||||
|
createPublishedTypebot,
|
||||||
|
parseTypebotToPublicTypebot,
|
||||||
|
updatePublishedTypebot,
|
||||||
|
} from 'services/publicTypebot'
|
||||||
|
import {
|
||||||
|
checkIfPublished,
|
||||||
|
checkIfTypebotsAreEqual,
|
||||||
|
parseDefaultPublicId,
|
||||||
|
updateTypebot,
|
||||||
|
} from 'services/typebots'
|
||||||
|
import { fetcher, omit, preventUserFromRefreshing } from 'services/utils'
|
||||||
|
import useSWR from 'swr'
|
||||||
|
import { isDefined } from 'utils'
|
||||||
|
import { BlocksActions, blocksActions } from './actions/blocks'
|
||||||
|
import { useImmer, Updater } from 'use-immer'
|
||||||
|
import { stepsAction, StepsActions } from './actions/steps'
|
||||||
|
|
||||||
|
type UpdateTypebotPayload = Partial<{
|
||||||
|
theme: Theme
|
||||||
|
settings: Settings
|
||||||
|
publicId: string
|
||||||
|
}>
|
||||||
|
const typebotContext = createContext<
|
||||||
|
{
|
||||||
|
typebot?: Typebot
|
||||||
|
publishedTypebot?: PublicTypebot
|
||||||
|
isPublished: boolean
|
||||||
|
isPublishing: boolean
|
||||||
|
hasUnsavedChanges: boolean
|
||||||
|
isSavingLoading: boolean
|
||||||
|
save: () => void
|
||||||
|
undo: () => void
|
||||||
|
updateTypebot: (updates: UpdateTypebotPayload) => void
|
||||||
|
publishTypebot: () => void
|
||||||
|
} & BlocksActions &
|
||||||
|
StepsActions
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
//@ts-ignore
|
||||||
|
>({})
|
||||||
|
|
||||||
|
export const TypebotContext = ({
|
||||||
|
children,
|
||||||
|
typebotId,
|
||||||
|
}: {
|
||||||
|
children: ReactNode
|
||||||
|
typebotId?: string
|
||||||
|
}) => {
|
||||||
|
const router = useRouter()
|
||||||
|
const toast = useToast({
|
||||||
|
position: 'top-right',
|
||||||
|
status: 'error',
|
||||||
|
})
|
||||||
|
const [undoStack, setUndoStack] = useState<Typebot[]>([])
|
||||||
|
const { typebot, publishedTypebot, isLoading, mutate } = useFetchedTypebot({
|
||||||
|
typebotId,
|
||||||
|
onError: (error) =>
|
||||||
|
toast({
|
||||||
|
title: 'Error while fetching typebot',
|
||||||
|
description: error.message,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
const [localTypebot, setLocalTypebot] = useImmer<Typebot | undefined>(
|
||||||
|
undefined
|
||||||
|
)
|
||||||
|
const [localPublishedTypebot, setLocalPublishedTypebot] =
|
||||||
|
useState<PublicTypebot>()
|
||||||
|
const [isSavingLoading, setIsSavingLoading] = useState(false)
|
||||||
|
const [isPublishing, setIsPublishing] = useState(false)
|
||||||
|
|
||||||
|
const hasUnsavedChanges = useMemo(
|
||||||
|
() =>
|
||||||
|
isDefined(typebot) &&
|
||||||
|
isDefined(localTypebot) &&
|
||||||
|
!deepEqual(localTypebot, typebot),
|
||||||
|
[typebot, localTypebot]
|
||||||
|
)
|
||||||
|
const isPublished = useMemo(
|
||||||
|
() =>
|
||||||
|
isDefined(typebot) &&
|
||||||
|
isDefined(publishedTypebot) &&
|
||||||
|
checkIfPublished(typebot, publishedTypebot),
|
||||||
|
[typebot, publishedTypebot]
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!localTypebot || !typebot) return
|
||||||
|
if (!checkIfTypebotsAreEqual(localTypebot, typebot)) {
|
||||||
|
pushNewTypebotInUndoStack(localTypebot)
|
||||||
|
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
||||||
|
window.addEventListener('beforeunload', preventUserFromRefreshing)
|
||||||
|
} else {
|
||||||
|
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [localTypebot])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isLoading) return
|
||||||
|
if (!typebot) {
|
||||||
|
toast({ status: 'info', description: "Couldn't find typebot" })
|
||||||
|
router.replace('/typebots')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setLocalTypebot({ ...typebot })
|
||||||
|
if (publishedTypebot) setLocalPublishedTypebot({ ...publishedTypebot })
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [isLoading])
|
||||||
|
|
||||||
|
const pushNewTypebotInUndoStack = (typebot: Typebot) => {
|
||||||
|
setUndoStack([...undoStack, typebot])
|
||||||
|
}
|
||||||
|
|
||||||
|
const undo = () => {
|
||||||
|
const lastTypebot = [...undoStack].pop()
|
||||||
|
setUndoStack(undoStack.slice(0, -1))
|
||||||
|
setLocalTypebot(lastTypebot)
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveTypebot = async (typebot?: Typebot) => {
|
||||||
|
if (!localTypebot) return
|
||||||
|
setIsSavingLoading(true)
|
||||||
|
const { error } = await updateTypebot(
|
||||||
|
typebot?.id ?? localTypebot.id,
|
||||||
|
typebot ?? localTypebot
|
||||||
|
)
|
||||||
|
setIsSavingLoading(false)
|
||||||
|
if (error) return toast({ title: error.name, description: error.message })
|
||||||
|
mutate({ typebot: typebot ?? localTypebot })
|
||||||
|
window.removeEventListener('beforeunload', preventUserFromRefreshing)
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateLocalTypebot = ({
|
||||||
|
publicId,
|
||||||
|
settings,
|
||||||
|
theme,
|
||||||
|
}: UpdateTypebotPayload) => {
|
||||||
|
setLocalTypebot((typebot) => {
|
||||||
|
if (!typebot) return
|
||||||
|
if (publicId) typebot.publicId = publicId
|
||||||
|
if (settings) typebot.settings = settings
|
||||||
|
if (theme) typebot.theme = theme
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const publishTypebot = async () => {
|
||||||
|
if (!localTypebot) return
|
||||||
|
const newLocalTypebot = { ...localTypebot }
|
||||||
|
if (!localPublishedTypebot) {
|
||||||
|
const newPublicId = parseDefaultPublicId(
|
||||||
|
localTypebot.name,
|
||||||
|
localTypebot.id
|
||||||
|
)
|
||||||
|
updateLocalTypebot({ publicId: newPublicId })
|
||||||
|
newLocalTypebot.publicId = newPublicId
|
||||||
|
}
|
||||||
|
if (hasUnsavedChanges || !localPublishedTypebot)
|
||||||
|
await saveTypebot(newLocalTypebot)
|
||||||
|
setIsPublishing(true)
|
||||||
|
if (localPublishedTypebot) {
|
||||||
|
const { error } = await updatePublishedTypebot(
|
||||||
|
localPublishedTypebot.id,
|
||||||
|
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
|
||||||
|
)
|
||||||
|
setIsPublishing(false)
|
||||||
|
if (error) return toast({ title: error.name, description: error.message })
|
||||||
|
} else {
|
||||||
|
const { data, error } = await createPublishedTypebot(
|
||||||
|
omit(parseTypebotToPublicTypebot(newLocalTypebot), 'id')
|
||||||
|
)
|
||||||
|
setLocalPublishedTypebot(data)
|
||||||
|
setIsPublishing(false)
|
||||||
|
if (error) return toast({ title: error.name, description: error.message })
|
||||||
|
}
|
||||||
|
mutate({ typebot: localTypebot })
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<typebotContext.Provider
|
||||||
|
value={{
|
||||||
|
typebot: localTypebot,
|
||||||
|
publishedTypebot: localPublishedTypebot,
|
||||||
|
hasUnsavedChanges,
|
||||||
|
isSavingLoading,
|
||||||
|
save: saveTypebot,
|
||||||
|
undo,
|
||||||
|
publishTypebot,
|
||||||
|
isPublishing,
|
||||||
|
isPublished,
|
||||||
|
updateTypebot: updateLocalTypebot,
|
||||||
|
...blocksActions(setLocalTypebot as Updater<Typebot>),
|
||||||
|
...stepsAction(setLocalTypebot as Updater<Typebot>),
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</typebotContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useTypebot = () => useContext(typebotContext)
|
||||||
|
|
||||||
|
export const useFetchedTypebot = ({
|
||||||
|
typebotId,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
typebotId?: string
|
||||||
|
onError: (error: Error) => void
|
||||||
|
}) => {
|
||||||
|
const { data, error, mutate } = useSWR<
|
||||||
|
{ typebot: Typebot; publishedTypebot?: PublicTypebot },
|
||||||
|
Error
|
||||||
|
>(typebotId ? `/api/typebots/${typebotId}` : null, fetcher)
|
||||||
|
if (error) onError(error)
|
||||||
|
return {
|
||||||
|
typebot: data?.typebot,
|
||||||
|
publishedTypebot: data?.publishedTypebot,
|
||||||
|
isLoading: !error && !data,
|
||||||
|
mutate,
|
||||||
|
}
|
||||||
|
}
|
40
apps/builder/contexts/TypebotContext/actions/blocks.ts
Normal file
40
apps/builder/contexts/TypebotContext/actions/blocks.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import { Coordinates } from 'contexts/GraphContext'
|
||||||
|
import { Block, Step, StepType, Typebot } from 'models'
|
||||||
|
import { parseNewBlock } from 'services/typebots'
|
||||||
|
import { Updater } from 'use-immer'
|
||||||
|
import { createStepDraft, deleteStepDraft } from './steps'
|
||||||
|
|
||||||
|
export type BlocksActions = {
|
||||||
|
createBlock: (props: Coordinates & { step: StepType | Step }) => void
|
||||||
|
updateBlock: (blockId: string, updates: Partial<Omit<Block, 'id'>>) => void
|
||||||
|
deleteBlock: (blockId: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const blocksActions = (setTypebot: Updater<Typebot>): BlocksActions => ({
|
||||||
|
createBlock: ({ x, y, step }: Coordinates & { step: StepType | Step }) => {
|
||||||
|
setTypebot((typebot) => {
|
||||||
|
const newBlock = parseNewBlock({
|
||||||
|
totalBlocks: typebot.blocks.allIds.length,
|
||||||
|
initialCoordinates: { x, y },
|
||||||
|
})
|
||||||
|
typebot.blocks.byId[newBlock.id] = newBlock
|
||||||
|
typebot.blocks.allIds.push(newBlock.id)
|
||||||
|
createStepDraft(typebot, step, newBlock.id)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
updateBlock: (blockId: string, updates: Partial<Omit<Block, 'id'>>) =>
|
||||||
|
setTypebot((typebot) => {
|
||||||
|
typebot.blocks.byId[blockId] = {
|
||||||
|
...typebot.blocks.byId[blockId],
|
||||||
|
...updates,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
deleteBlock: (blockId: string) =>
|
||||||
|
setTypebot((typebot) => {
|
||||||
|
const block = typebot.blocks.byId[blockId]
|
||||||
|
block.stepIds.forEach((stepId) => deleteStepDraft(typebot, stepId))
|
||||||
|
delete typebot.blocks.byId[blockId]
|
||||||
|
const index = typebot.blocks.allIds.indexOf(blockId)
|
||||||
|
if (index !== -1) typebot.blocks.allIds.splice(index, 1)
|
||||||
|
}),
|
||||||
|
})
|
51
apps/builder/contexts/TypebotContext/actions/steps.ts
Normal file
51
apps/builder/contexts/TypebotContext/actions/steps.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import { Step, StepType, Typebot } from 'models'
|
||||||
|
import { parseNewStep } from 'services/typebots'
|
||||||
|
import { Updater } from 'use-immer'
|
||||||
|
import { WritableDraft } from 'immer/dist/types/types-external'
|
||||||
|
|
||||||
|
export type StepsActions = {
|
||||||
|
createStep: (blockId: string, step: StepType | Step, index?: number) => void
|
||||||
|
updateStep: (
|
||||||
|
stepId: string,
|
||||||
|
updates: Partial<Omit<Step, 'id' | 'type'>>
|
||||||
|
) => void
|
||||||
|
deleteStep: (stepId: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const stepsAction = (setTypebot: Updater<Typebot>): StepsActions => ({
|
||||||
|
createStep: (blockId: string, step: StepType | Step, index?: number) => {
|
||||||
|
setTypebot((typebot) => {
|
||||||
|
createStepDraft(typebot, step, blockId, index)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
updateStep: (stepId: string, updates: Partial<Omit<Step, 'id' | 'type'>>) =>
|
||||||
|
setTypebot((typebot) => {
|
||||||
|
typebot.steps.byId[stepId] = { ...typebot.steps.byId[stepId], ...updates }
|
||||||
|
}),
|
||||||
|
deleteStep: (stepId: string) => {
|
||||||
|
setTypebot((typebot) => {
|
||||||
|
deleteStepDraft(typebot, stepId)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export const deleteStepDraft = (
|
||||||
|
typebot: WritableDraft<Typebot>,
|
||||||
|
stepId: string
|
||||||
|
) => {
|
||||||
|
delete typebot.steps.byId[stepId]
|
||||||
|
const index = typebot.steps.allIds.indexOf(stepId)
|
||||||
|
if (index !== -1) typebot.steps.allIds.splice(index, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createStepDraft = (
|
||||||
|
typebot: WritableDraft<Typebot>,
|
||||||
|
step: StepType | Step,
|
||||||
|
blockId: string,
|
||||||
|
index?: number
|
||||||
|
) => {
|
||||||
|
const newStep = typeof step === 'string' ? parseNewStep(step, blockId) : step
|
||||||
|
typebot.steps.byId[newStep.id] = newStep
|
||||||
|
typebot.steps.allIds.push(newStep.id)
|
||||||
|
typebot.blocks.byId[blockId].stepIds.splice(index ?? 0, 0, newStep.id)
|
||||||
|
}
|
1
apps/builder/contexts/TypebotContext/index.ts
Normal file
1
apps/builder/contexts/TypebotContext/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export { TypebotContext } from './TypebotContext'
|
@ -1,5 +1,6 @@
|
|||||||
import { parseNewTypebot, PublicTypebot, StepType, Typebot } from 'bot-engine'
|
import { PublicTypebot, StepType, Typebot } from 'models'
|
||||||
import { Plan, PrismaClient } from 'db'
|
import { Plan, PrismaClient } from 'db'
|
||||||
|
import { parseTestTypebot } from './utils'
|
||||||
|
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
@ -34,51 +35,50 @@ const createFolders = () =>
|
|||||||
})
|
})
|
||||||
|
|
||||||
const createTypebots = async () => {
|
const createTypebots = async () => {
|
||||||
const typebot2: Typebot = {
|
const typebot2 = {
|
||||||
...(parseNewTypebot({
|
...parseTestTypebot({
|
||||||
|
id: 'typebot2',
|
||||||
name: 'Typebot #2',
|
name: 'Typebot #2',
|
||||||
ownerId: 'test2',
|
ownerId: 'test2',
|
||||||
folderId: null,
|
blocks: {
|
||||||
}) as Typebot),
|
byId: {
|
||||||
id: 'typebot2',
|
block1: {
|
||||||
startBlock: {
|
id: 'block1',
|
||||||
id: 'start-block',
|
title: 'Block #1',
|
||||||
steps: [
|
stepIds: ['step1'],
|
||||||
{
|
graphCoordinates: { x: 200, y: 200 },
|
||||||
id: 'start-step',
|
},
|
||||||
blockId: 'start-block',
|
|
||||||
type: StepType.START,
|
|
||||||
label: 'Start',
|
|
||||||
target: { blockId: 'block1' },
|
|
||||||
},
|
},
|
||||||
],
|
allIds: ['block1'],
|
||||||
graphCoordinates: { x: 0, y: 0 },
|
|
||||||
title: 'Start',
|
|
||||||
},
|
|
||||||
blocks: [
|
|
||||||
{
|
|
||||||
title: 'Block #1',
|
|
||||||
id: 'block1',
|
|
||||||
steps: [{ id: 'step1', type: StepType.TEXT_INPUT, blockId: 'block1' }],
|
|
||||||
graphCoordinates: { x: 200, y: 200 },
|
|
||||||
},
|
},
|
||||||
],
|
steps: {
|
||||||
|
byId: {
|
||||||
|
step1: {
|
||||||
|
id: 'step1',
|
||||||
|
type: StepType.TEXT_INPUT,
|
||||||
|
blockId: 'block1',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
allIds: ['step1'],
|
||||||
|
},
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
await prisma.typebot.createMany({
|
await prisma.typebot.createMany({
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
...parseNewTypebot({
|
...parseTestTypebot({
|
||||||
|
id: 'typebot1',
|
||||||
name: 'Typebot #1',
|
name: 'Typebot #1',
|
||||||
ownerId: 'test2',
|
ownerId: 'test2',
|
||||||
folderId: null,
|
blocks: { byId: {}, allIds: [] },
|
||||||
|
steps: { byId: {}, allIds: [] },
|
||||||
}),
|
}),
|
||||||
id: 'typebot1',
|
|
||||||
},
|
},
|
||||||
typebot2,
|
typebot2,
|
||||||
],
|
] as any,
|
||||||
})
|
})
|
||||||
return prisma.publicTypebot.createMany({
|
return prisma.publicTypebot.createMany({
|
||||||
data: [parseTypebotToPublicTypebot('publictypebot2', typebot2)],
|
data: [parseTypebotToPublicTypebot('publictypebot2', typebot2)] as any,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,8 +118,8 @@ const parseTypebotToPublicTypebot = (
|
|||||||
): PublicTypebot => ({
|
): PublicTypebot => ({
|
||||||
id,
|
id,
|
||||||
blocks: typebot.blocks,
|
blocks: typebot.blocks,
|
||||||
|
steps: typebot.steps,
|
||||||
name: typebot.name,
|
name: typebot.name,
|
||||||
startBlock: typebot.startBlock,
|
|
||||||
typebotId: typebot.id,
|
typebotId: typebot.id,
|
||||||
theme: typebot.theme,
|
theme: typebot.theme,
|
||||||
settings: typebot.settings,
|
settings: typebot.settings,
|
||||||
|
75
apps/builder/cypress/plugins/utils.ts
Normal file
75
apps/builder/cypress/plugins/utils.ts
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import {
|
||||||
|
Block,
|
||||||
|
StepType,
|
||||||
|
Theme,
|
||||||
|
BackgroundType,
|
||||||
|
Settings,
|
||||||
|
Typebot,
|
||||||
|
Table,
|
||||||
|
Step,
|
||||||
|
} from 'models'
|
||||||
|
|
||||||
|
export const parseTestTypebot = ({
|
||||||
|
id,
|
||||||
|
ownerId,
|
||||||
|
name,
|
||||||
|
blocks,
|
||||||
|
steps,
|
||||||
|
}: {
|
||||||
|
id: string
|
||||||
|
ownerId: string
|
||||||
|
name: string
|
||||||
|
blocks: Table<Block>
|
||||||
|
steps: Table<Step>
|
||||||
|
}): Typebot => {
|
||||||
|
const theme: Theme = {
|
||||||
|
general: {
|
||||||
|
font: 'Open Sans',
|
||||||
|
background: { type: BackgroundType.NONE, content: '#ffffff' },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const settings: Settings = {
|
||||||
|
typingEmulation: {
|
||||||
|
enabled: true,
|
||||||
|
speed: 300,
|
||||||
|
maxDelay: 1.5,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
folderId: null,
|
||||||
|
name,
|
||||||
|
ownerId,
|
||||||
|
theme,
|
||||||
|
settings,
|
||||||
|
createdAt: new Date(),
|
||||||
|
blocks: {
|
||||||
|
byId: {
|
||||||
|
block0: {
|
||||||
|
id: 'block0',
|
||||||
|
title: 'Block #0',
|
||||||
|
stepIds: ['step0'],
|
||||||
|
graphCoordinates: { x: 0, y: 0 },
|
||||||
|
},
|
||||||
|
...blocks.byId,
|
||||||
|
},
|
||||||
|
allIds: ['block0', ...blocks.allIds],
|
||||||
|
},
|
||||||
|
steps: {
|
||||||
|
byId: {
|
||||||
|
step0: {
|
||||||
|
id: 'step0',
|
||||||
|
type: StepType.START,
|
||||||
|
blockId: 'block0',
|
||||||
|
label: 'Start',
|
||||||
|
target: { blockId: 'block1' },
|
||||||
|
},
|
||||||
|
...steps.byId,
|
||||||
|
},
|
||||||
|
allIds: ['step0', ...steps.allIds],
|
||||||
|
},
|
||||||
|
publicId: null,
|
||||||
|
publishedTypebotId: null,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
}
|
||||||
|
}
|
@ -2,15 +2,10 @@ import path from 'path'
|
|||||||
import { parse } from 'papaparse'
|
import { parse } from 'papaparse'
|
||||||
|
|
||||||
describe('ResultsPage', () => {
|
describe('ResultsPage', () => {
|
||||||
before(() => {
|
|
||||||
cy.intercept({ url: '/api/typebots/typebot2/results*', method: 'GET' }).as(
|
|
||||||
'getResults'
|
|
||||||
)
|
|
||||||
cy.intercept({ url: '/api/typebots/typebot2/results*', method: 'GET' }).as(
|
|
||||||
'getResults'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
cy.intercept({ url: '/api/typebots/typebot2/results*', method: 'GET' }).as(
|
||||||
|
'getResults'
|
||||||
|
)
|
||||||
cy.task('seed')
|
cy.task('seed')
|
||||||
cy.signOut()
|
cy.signOut()
|
||||||
})
|
})
|
||||||
@ -48,7 +43,7 @@ describe('ResultsPage', () => {
|
|||||||
cy.findByText('content0').should('exist')
|
cy.findByText('content0').should('exist')
|
||||||
})
|
})
|
||||||
|
|
||||||
it.only('should correctly export selection in CSV', () => {
|
it('should correctly export selection in CSV', () => {
|
||||||
const downloadsFolder = Cypress.config('downloadsFolder')
|
const downloadsFolder = Cypress.config('downloadsFolder')
|
||||||
cy.signIn('test2@gmail.com')
|
cy.signIn('test2@gmail.com')
|
||||||
cy.visit('/typebots/typebot2/results')
|
cy.visit('/typebots/typebot2/results')
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { Flex, useToast } from '@chakra-ui/react'
|
import { Flex, useToast } from '@chakra-ui/react'
|
||||||
import { Stats } from 'bot-engine'
|
|
||||||
import AnalyticsGraph from 'components/analytics/graph/AnalyticsGraph'
|
import AnalyticsGraph from 'components/analytics/graph/AnalyticsGraph'
|
||||||
import { StatsCards } from 'components/analytics/StatsCards'
|
import { StatsCards } from 'components/analytics/StatsCards'
|
||||||
import { AnalyticsGraphProvider } from 'contexts/AnalyticsGraphProvider'
|
import { AnalyticsGraphProvider } from 'contexts/AnalyticsGraphProvider'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
|
import { Stats } from 'models'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useAnswersCount } from 'services/analytics'
|
import { useAnswersCount } from 'services/analytics'
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Button, Flex, HStack, Tag, useToast, Text } from '@chakra-ui/react'
|
import { Button, Flex, HStack, Tag, useToast, Text } from '@chakra-ui/react'
|
||||||
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
|
import { NextChakraLink } from 'components/nextChakra/NextChakraLink'
|
||||||
import { useTypebot } from 'contexts/TypebotContext'
|
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import { useStats } from 'services/analytics'
|
import { useStats } from 'services/analytics'
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
||||||
const withTM = require('next-transpile-modules')(['utils'])
|
|
||||||
|
|
||||||
module.exports = withTM({
|
|
||||||
reactStrictMode: true,
|
|
||||||
})
|
|
@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@chakra-ui/css-reset": "^1.1.1",
|
"@chakra-ui/css-reset": "^1.1.1",
|
||||||
"@chakra-ui/react": "^1.7.3",
|
"@chakra-ui/react": "^1.7.4",
|
||||||
"@dnd-kit/core": "^4.0.3",
|
"@dnd-kit/core": "^4.0.3",
|
||||||
"@dnd-kit/sortable": "^5.1.0",
|
"@dnd-kit/sortable": "^5.1.0",
|
||||||
"@emotion/react": "^11.7.1",
|
"@emotion/react": "^11.7.1",
|
||||||
@ -24,13 +24,14 @@
|
|||||||
"@udecode/plate-link": "^9.0.0",
|
"@udecode/plate-link": "^9.0.0",
|
||||||
"@udecode/plate-ui-link": "^9.0.0",
|
"@udecode/plate-ui-link": "^9.0.0",
|
||||||
"@udecode/plate-ui-toolbar": "^9.0.0",
|
"@udecode/plate-ui-toolbar": "^9.0.0",
|
||||||
"aws-sdk": "^2.1048.0",
|
"aws-sdk": "^2.1051.0",
|
||||||
"bot-engine": "*",
|
"bot-engine": "*",
|
||||||
"db": "*",
|
"db": "*",
|
||||||
"fast-equals": "^2.0.4",
|
"fast-equals": "^2.0.4",
|
||||||
"focus-visible": "^5.2.0",
|
"focus-visible": "^5.2.0",
|
||||||
"framer-motion": "^4",
|
"framer-motion": "^4",
|
||||||
"htmlparser2": "^7.2.0",
|
"htmlparser2": "^7.2.0",
|
||||||
|
"immer": "^9.0.7",
|
||||||
"kbar": "^0.1.0-beta.24",
|
"kbar": "^0.1.0-beta.24",
|
||||||
"micro": "^9.3.4",
|
"micro": "^9.3.4",
|
||||||
"micro-cors": "^0.1.1",
|
"micro-cors": "^0.1.1",
|
||||||
@ -45,7 +46,7 @@
|
|||||||
"react-frame-component": "^5.2.1",
|
"react-frame-component": "^5.2.1",
|
||||||
"react-table": "^7.7.0",
|
"react-table": "^7.7.0",
|
||||||
"short-uuid": "^4.2.0",
|
"short-uuid": "^4.2.0",
|
||||||
"slate": "^0.72.0",
|
"slate": "^0.72.3",
|
||||||
"slate-history": "^0.66.0",
|
"slate-history": "^0.66.0",
|
||||||
"slate-hyperscript": "^0.67.0",
|
"slate-hyperscript": "^0.67.0",
|
||||||
"slate-react": "^0.72.1",
|
"slate-react": "^0.72.1",
|
||||||
@ -54,7 +55,9 @@
|
|||||||
"svg-round-corners": "^0.3.0",
|
"svg-round-corners": "^0.3.0",
|
||||||
"swr": "^1.1.2",
|
"swr": "^1.1.2",
|
||||||
"use-debounce": "^7.0.1",
|
"use-debounce": "^7.0.1",
|
||||||
"utils": "*"
|
"use-immer": "^0.6.0",
|
||||||
|
"utils": "*",
|
||||||
|
"models": "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/cypress": "^8.0.2",
|
"@testing-library/cypress": "^8.0.2",
|
||||||
@ -63,10 +66,10 @@
|
|||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
"@types/papaparse": "^5.3.1",
|
"@types/papaparse": "^5.3.1",
|
||||||
"@types/qs": "^6.9.7",
|
"@types/qs": "^6.9.7",
|
||||||
"@types/react": "^17.0.37",
|
"@types/react": "^17.0.38",
|
||||||
"@types/react-table": "^7.7.9",
|
"@types/react-table": "^7.7.9",
|
||||||
"@types/testing-library__cypress": "^5.0.9",
|
"@types/testing-library__cypress": "^5.0.9",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||||
"cypress": "^9.2.0",
|
"cypress": "^9.2.0",
|
||||||
"cypress-file-upload": "^5.0.8",
|
"cypress-file-upload": "^5.0.8",
|
||||||
"cypress-social-logins": "^1.13.0",
|
"cypress-social-logins": "^1.13.0",
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { parseNewTypebot } from 'bot-engine'
|
import { Prisma, User } from 'db'
|
||||||
import { User } from 'db'
|
|
||||||
import prisma from 'libs/prisma'
|
import prisma from 'libs/prisma'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { getSession } from 'next-auth/react'
|
import { getSession } from 'next-auth/react'
|
||||||
|
import { parseNewTypebot } from 'services/typebots'
|
||||||
import { methodNotAllowed } from 'utils'
|
import { methodNotAllowed } from 'utils'
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
@ -26,7 +26,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const data = JSON.parse(req.body)
|
const data = JSON.parse(req.body)
|
||||||
const typebot = await prisma.typebot.create({
|
const typebot = await prisma.typebot.create({
|
||||||
data: parseNewTypebot({ ownerId: user.id, ...data }),
|
data: parseNewTypebot({
|
||||||
|
ownerId: user.id,
|
||||||
|
...data,
|
||||||
|
}) as Prisma.TypebotUncheckedCreateInput,
|
||||||
})
|
})
|
||||||
return res.send(typebot)
|
return res.send(typebot)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { PublicTypebot } from 'bot-engine'
|
import { PublicTypebot } from 'models'
|
||||||
import { User } from 'db'
|
import { User } from 'db'
|
||||||
import prisma from 'libs/prisma'
|
import prisma from 'libs/prisma'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
@ -24,14 +24,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
|
|
||||||
const answersCounts: { blockId: string; totalAnswers: number }[] =
|
const answersCounts: { blockId: string; totalAnswers: number }[] =
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
(typebot.publishedTypebot as PublicTypebot).blocks.map(
|
(
|
||||||
async (block) => {
|
typebot.publishedTypebot as unknown as PublicTypebot
|
||||||
const totalAnswers = await prisma.answer.count({
|
).blocks.allIds.map(async (blockId) => {
|
||||||
where: { blockId: block.id },
|
const totalAnswers = await prisma.answer.count({
|
||||||
})
|
where: { blockId },
|
||||||
return { blockId: block.id, totalAnswers }
|
})
|
||||||
}
|
return { blockId, totalAnswers }
|
||||||
)
|
})
|
||||||
)
|
)
|
||||||
return res.status(200).send({ answersCounts })
|
return res.status(200).send({ answersCounts })
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Stats } from 'bot-engine'
|
|
||||||
import { User } from 'db'
|
import { User } from 'db'
|
||||||
import prisma from 'libs/prisma'
|
import prisma from 'libs/prisma'
|
||||||
|
import { Stats } from 'models'
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { getSession } from 'next-auth/react'
|
import { getSession } from 'next-auth/react'
|
||||||
import { methodNotAllowed } from 'utils'
|
import { methodNotAllowed } from 'utils'
|
||||||
|
@ -3,7 +3,7 @@ import { Board } from 'components/board/Board'
|
|||||||
import { Seo } from 'components/Seo'
|
import { Seo } from 'components/Seo'
|
||||||
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
||||||
import { EditorContext } from 'contexts/EditorContext'
|
import { EditorContext } from 'contexts/EditorContext'
|
||||||
import { TypebotContext } from 'contexts/TypebotContext'
|
import { TypebotContext } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { KBarProvider } from 'kbar'
|
import { KBarProvider } from 'kbar'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
@ -2,7 +2,7 @@ import { Flex } from '@chakra-ui/layout'
|
|||||||
import { ResultsContent } from 'layouts/results/ResultsContent'
|
import { ResultsContent } from 'layouts/results/ResultsContent'
|
||||||
import { Seo } from 'components/Seo'
|
import { Seo } from 'components/Seo'
|
||||||
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
||||||
import { TypebotContext } from 'contexts/TypebotContext'
|
import { TypebotContext } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { UserContext } from 'contexts/UserContext'
|
import { UserContext } from 'contexts/UserContext'
|
||||||
|
@ -2,7 +2,7 @@ import { Flex } from '@chakra-ui/layout'
|
|||||||
import { ResultsContent } from 'layouts/results/ResultsContent'
|
import { ResultsContent } from 'layouts/results/ResultsContent'
|
||||||
import { Seo } from 'components/Seo'
|
import { Seo } from 'components/Seo'
|
||||||
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
||||||
import { TypebotContext } from 'contexts/TypebotContext'
|
import { TypebotContext } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { UserContext } from 'contexts/UserContext'
|
import { UserContext } from 'contexts/UserContext'
|
||||||
|
@ -2,7 +2,7 @@ import { Flex } from '@chakra-ui/layout'
|
|||||||
import { Seo } from 'components/Seo'
|
import { Seo } from 'components/Seo'
|
||||||
import { SettingsContent } from 'components/settings/SettingsContent'
|
import { SettingsContent } from 'components/settings/SettingsContent'
|
||||||
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
||||||
import { TypebotContext } from 'contexts/TypebotContext'
|
import { TypebotContext } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { UserContext } from 'contexts/UserContext'
|
import { UserContext } from 'contexts/UserContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
@ -2,7 +2,7 @@ import { Flex } from '@chakra-ui/layout'
|
|||||||
import { Seo } from 'components/Seo'
|
import { Seo } from 'components/Seo'
|
||||||
import { ShareContent } from 'components/share/ShareContent'
|
import { ShareContent } from 'components/share/ShareContent'
|
||||||
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
||||||
import { TypebotContext } from 'contexts/TypebotContext'
|
import { TypebotContext } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { UserContext } from 'contexts/UserContext'
|
import { UserContext } from 'contexts/UserContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
@ -2,7 +2,7 @@ import { Flex } from '@chakra-ui/layout'
|
|||||||
import { Seo } from 'components/Seo'
|
import { Seo } from 'components/Seo'
|
||||||
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
import { TypebotHeader } from 'components/shared/TypebotHeader'
|
||||||
import { ThemeContent } from 'components/theme/ThemeContent'
|
import { ThemeContent } from 'components/theme/ThemeContent'
|
||||||
import { TypebotContext } from 'contexts/TypebotContext'
|
import { TypebotContext } from 'contexts/TypebotContext/TypebotContext'
|
||||||
import { UserContext } from 'contexts/UserContext'
|
import { UserContext } from 'contexts/UserContext'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Stats } from 'bot-engine'
|
import { Stats } from 'models'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { fetcher } from './utils'
|
import { fetcher } from './utils'
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Coordinates } from '@dnd-kit/core/dist/types'
|
import { Coordinates } from '@dnd-kit/core/dist/types'
|
||||||
import { Block, StartBlock } from 'bot-engine'
|
import { Block } from 'models'
|
||||||
import { AnchorsPositionProps } from 'components/board/graph/Edges/Edge'
|
import { AnchorsPositionProps } from 'components/board/graph/Edges/Edge'
|
||||||
import {
|
import {
|
||||||
stubLength,
|
stubLength,
|
||||||
@ -143,7 +143,7 @@ const computeFiveSegments = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getAnchorsPosition = (
|
export const getAnchorsPosition = (
|
||||||
sourceBlock: Block | StartBlock,
|
sourceBlock: Block,
|
||||||
targetBlock: Block,
|
targetBlock: Block,
|
||||||
sourceStepIndex: number,
|
sourceStepIndex: number,
|
||||||
targetStepIndex?: number
|
targetStepIndex?: number
|
||||||
|
@ -1,11 +1,4 @@
|
|||||||
import {
|
import { InputStep, PublicTypebot, Step, StepType, Typebot } from 'models'
|
||||||
Block,
|
|
||||||
InputStep,
|
|
||||||
PublicTypebot,
|
|
||||||
Step,
|
|
||||||
StepType,
|
|
||||||
Typebot,
|
|
||||||
} from 'bot-engine'
|
|
||||||
import { sendRequest } from './utils'
|
import { sendRequest } from './utils'
|
||||||
import shortId from 'short-uuid'
|
import shortId from 'short-uuid'
|
||||||
import { HStack, Text } from '@chakra-ui/react'
|
import { HStack, Text } from '@chakra-ui/react'
|
||||||
@ -17,8 +10,8 @@ export const parseTypebotToPublicTypebot = (
|
|||||||
): PublicTypebot => ({
|
): PublicTypebot => ({
|
||||||
id: shortId.generate(),
|
id: shortId.generate(),
|
||||||
blocks: typebot.blocks,
|
blocks: typebot.blocks,
|
||||||
|
steps: typebot.steps,
|
||||||
name: typebot.name,
|
name: typebot.name,
|
||||||
startBlock: typebot.startBlock,
|
|
||||||
typebotId: typebot.id,
|
typebotId: typebot.id,
|
||||||
theme: typebot.theme,
|
theme: typebot.theme,
|
||||||
settings: typebot.settings,
|
settings: typebot.settings,
|
||||||
@ -28,7 +21,7 @@ export const parseTypebotToPublicTypebot = (
|
|||||||
export const createPublishedTypebot = async (
|
export const createPublishedTypebot = async (
|
||||||
typebot: Omit<PublicTypebot, 'id'>
|
typebot: Omit<PublicTypebot, 'id'>
|
||||||
) =>
|
) =>
|
||||||
sendRequest({
|
sendRequest<PublicTypebot>({
|
||||||
url: `/api/publicTypebots`,
|
url: `/api/publicTypebots`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: typebot,
|
body: typebot,
|
||||||
@ -49,32 +42,47 @@ export const parseSubmissionsColumns = (
|
|||||||
): {
|
): {
|
||||||
Header: JSX.Element
|
Header: JSX.Element
|
||||||
accessor: string
|
accessor: string
|
||||||
}[] => [
|
}[] => {
|
||||||
{
|
console.log(typebot)
|
||||||
Header: (
|
if (!typebot) return []
|
||||||
<HStack>
|
return [
|
||||||
<CalendarIcon />
|
{
|
||||||
<Text>Submitted at</Text>
|
Header: (
|
||||||
</HStack>
|
<HStack>
|
||||||
),
|
<CalendarIcon />
|
||||||
accessor: 'createdAt',
|
<Text>Submitted at</Text>
|
||||||
},
|
</HStack>
|
||||||
...(typebot?.blocks ?? []).filter(blockContainsInput).map((block) => ({
|
),
|
||||||
Header: (
|
accessor: 'createdAt',
|
||||||
<HStack>
|
},
|
||||||
<StepIcon
|
...typebot.blocks.allIds
|
||||||
type={
|
.filter((blockId) => typebot && blockContainsInput(typebot, blockId))
|
||||||
block.steps.find((step) => step.target)?.type ?? StepType.TEXT_INPUT
|
.map((blockId) => {
|
||||||
}
|
const block = typebot.blocks.byId[blockId]
|
||||||
/>
|
const inputStepId = block.stepIds.find((stepId) =>
|
||||||
<Text>{block.title}</Text>
|
stepIsInput(typebot.steps.byId[stepId])
|
||||||
</HStack>
|
)
|
||||||
),
|
const inputStep = typebot.steps.byId[inputStepId as string]
|
||||||
accessor: block.id,
|
return {
|
||||||
})),
|
Header: (
|
||||||
]
|
<HStack>
|
||||||
|
<StepIcon type={inputStep.type} />
|
||||||
|
<Text>{block.title}</Text>
|
||||||
|
</HStack>
|
||||||
|
),
|
||||||
|
accessor: blockId,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
const blockContainsInput = (block: Block) => block.steps.some(stepIsInput)
|
const blockContainsInput = (
|
||||||
|
typebot: PublicTypebot | Typebot,
|
||||||
|
blockId: string
|
||||||
|
) =>
|
||||||
|
typebot.blocks.byId[blockId].stepIds.some((stepId) =>
|
||||||
|
stepIsInput(typebot.steps.byId[stepId])
|
||||||
|
)
|
||||||
|
|
||||||
const stepIsInput = (step: Step): step is InputStep =>
|
const stepIsInput = (step: Step): step is InputStep =>
|
||||||
step.type === StepType.TEXT_INPUT
|
step.type === StepType.TEXT_INPUT
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Result } from 'bot-engine'
|
import { Result } from 'models'
|
||||||
import useSWRInfinite from 'swr/infinite'
|
import useSWRInfinite from 'swr/infinite'
|
||||||
import { fetcher, sendRequest } from './utils'
|
import { fetcher, sendRequest } from './utils'
|
||||||
import { stringify } from 'qs'
|
import { stringify } from 'qs'
|
||||||
|
@ -5,9 +5,13 @@ import {
|
|||||||
TextStep,
|
TextStep,
|
||||||
TextInputStep,
|
TextInputStep,
|
||||||
PublicTypebot,
|
PublicTypebot,
|
||||||
} from 'bot-engine'
|
BackgroundType,
|
||||||
|
Settings,
|
||||||
|
StartStep,
|
||||||
|
Theme,
|
||||||
|
} from 'models'
|
||||||
import shortId from 'short-uuid'
|
import shortId from 'short-uuid'
|
||||||
import { Typebot } from 'bot-engine'
|
import { Typebot } from 'models'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { fetcher, sendRequest, toKebabCase } from './utils'
|
import { fetcher, sendRequest, toKebabCase } from './utils'
|
||||||
import { deepEqual } from 'fast-equals'
|
import { deepEqual } from 'fast-equals'
|
||||||
@ -85,13 +89,9 @@ export const patchTypebot = async (id: string, typebot: Partial<Typebot>) =>
|
|||||||
})
|
})
|
||||||
|
|
||||||
export const parseNewBlock = ({
|
export const parseNewBlock = ({
|
||||||
type,
|
|
||||||
totalBlocks,
|
totalBlocks,
|
||||||
initialCoordinates,
|
initialCoordinates,
|
||||||
step,
|
|
||||||
}: {
|
}: {
|
||||||
step?: Step
|
|
||||||
type?: StepType
|
|
||||||
totalBlocks: number
|
totalBlocks: number
|
||||||
initialCoordinates: { x: number; y: number }
|
initialCoordinates: { x: number; y: number }
|
||||||
}): Block => {
|
}): Block => {
|
||||||
@ -100,9 +100,7 @@ export const parseNewBlock = ({
|
|||||||
id,
|
id,
|
||||||
title: `Block #${totalBlocks + 1}`,
|
title: `Block #${totalBlocks + 1}`,
|
||||||
graphCoordinates: initialCoordinates,
|
graphCoordinates: initialCoordinates,
|
||||||
steps: [
|
stepIds: [],
|
||||||
step ? { ...step, blockId: id } : parseNewStep(type as StepType, id),
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,11 +156,58 @@ export const checkIfPublished = (
|
|||||||
publicTypebot: PublicTypebot
|
publicTypebot: PublicTypebot
|
||||||
) =>
|
) =>
|
||||||
deepEqual(typebot.blocks, publicTypebot.blocks) &&
|
deepEqual(typebot.blocks, publicTypebot.blocks) &&
|
||||||
deepEqual(typebot.startBlock, publicTypebot.startBlock) &&
|
deepEqual(typebot.steps, publicTypebot.steps) &&
|
||||||
typebot.name === publicTypebot.name &&
|
typebot.name === publicTypebot.name &&
|
||||||
typebot.publicId === publicTypebot.publicId &&
|
typebot.publicId === publicTypebot.publicId &&
|
||||||
deepEqual(typebot.settings, publicTypebot.settings) &&
|
deepEqual(typebot.settings, publicTypebot.settings) &&
|
||||||
deepEqual(typebot.theme, publicTypebot.theme)
|
deepEqual(typebot.theme, publicTypebot.theme)
|
||||||
|
|
||||||
export const parseDefaultPublicId = (name: string, id: string) =>
|
export const parseDefaultPublicId = (name: string, id: string) =>
|
||||||
toKebabCase(`${name}-${id?.slice(0, 5)}`)
|
toKebabCase(name) + `-${id?.slice(-7)}`
|
||||||
|
|
||||||
|
export const parseNewTypebot = ({
|
||||||
|
ownerId,
|
||||||
|
folderId,
|
||||||
|
name,
|
||||||
|
}: {
|
||||||
|
ownerId: string
|
||||||
|
folderId: string | null
|
||||||
|
name: string
|
||||||
|
}): Partial<Typebot> => {
|
||||||
|
const startBlockId = shortId.generate()
|
||||||
|
const startStepId = shortId.generate()
|
||||||
|
const startStep: StartStep = {
|
||||||
|
blockId: startBlockId,
|
||||||
|
id: startStepId,
|
||||||
|
label: 'Start',
|
||||||
|
type: StepType.START,
|
||||||
|
}
|
||||||
|
const startBlock: Block = {
|
||||||
|
id: startBlockId,
|
||||||
|
title: 'Start',
|
||||||
|
graphCoordinates: { x: 0, y: 0 },
|
||||||
|
stepIds: [startStepId],
|
||||||
|
}
|
||||||
|
const theme: Theme = {
|
||||||
|
general: {
|
||||||
|
font: 'Open Sans',
|
||||||
|
background: { type: BackgroundType.NONE, content: '#ffffff' },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const settings: Settings = {
|
||||||
|
typingEmulation: {
|
||||||
|
enabled: true,
|
||||||
|
speed: 300,
|
||||||
|
maxDelay: 1.5,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
folderId,
|
||||||
|
name,
|
||||||
|
ownerId,
|
||||||
|
blocks: { byId: { [startBlockId]: startBlock }, allIds: [startBlockId] },
|
||||||
|
steps: { byId: { [startStepId]: startStep }, allIds: [startStepId] },
|
||||||
|
theme,
|
||||||
|
settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { Answer, PublicTypebot, TypebotViewer } from 'bot-engine'
|
import { TypebotViewer } from 'bot-engine'
|
||||||
|
import { Answer, PublicTypebot } from 'models'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { upsertAnswer } from 'services/answer'
|
import { upsertAnswer } from 'services/answer'
|
||||||
import { SEO } from '../components/Seo'
|
import { SEO } from '../components/Seo'
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
||||||
const withTM = require('next-transpile-modules')(['utils'])
|
|
||||||
|
|
||||||
module.exports = withTM({
|
|
||||||
reactStrictMode: true,
|
|
||||||
})
|
|
@ -11,6 +11,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bot-engine": "*",
|
"bot-engine": "*",
|
||||||
"db": "*",
|
"db": "*",
|
||||||
|
"models": "*",
|
||||||
"next": "^12.0.7",
|
"next": "^12.0.7",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
@ -19,7 +20,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^17.0.4",
|
"@types/node": "^17.0.4",
|
||||||
"@types/react": "^17.0.38",
|
"@types/react": "^17.0.38",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||||
"eslint": "<8.0.0",
|
"eslint": "<8.0.0",
|
||||||
"eslint-config-next": "12.0.7",
|
"eslint-config-next": "12.0.7",
|
||||||
"eslint-config-prettier": "^8.3.0",
|
"eslint-config-prettier": "^8.3.0",
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { PublicTypebot } from 'bot-engine'
|
import { PublicTypebot } from 'models'
|
||||||
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
||||||
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
||||||
import prisma from '../libs/prisma'
|
import prisma from '../libs/prisma'
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { PublicTypebot } from 'bot-engine'
|
import { PublicTypebot } from 'models'
|
||||||
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
import { GetServerSideProps, GetServerSidePropsContext } from 'next'
|
||||||
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
import { TypebotPage, TypebotPageProps } from '../layouts/TypebotPage'
|
||||||
import prisma from '../libs/prisma'
|
import prisma from '../libs/prisma'
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Answer } from 'bot-engine'
|
import { Answer } from 'models'
|
||||||
import { sendRequest } from 'utils'
|
import { sendRequest } from 'utils'
|
||||||
|
|
||||||
export const upsertAnswer = async (answer: Answer & { resultId: string }) => {
|
export const upsertAnswer = async (answer: Answer & { resultId: string }) => {
|
||||||
|
@ -8,26 +8,28 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"db": "*",
|
"db": "*",
|
||||||
"fast-equals": "^2.0.4",
|
"fast-equals": "^2.0.4",
|
||||||
|
"models": "*",
|
||||||
"react-frame-component": "^5.2.1",
|
"react-frame-component": "^5.2.1",
|
||||||
"react-scroll": "^1.8.4",
|
"react-scroll": "^1.8.4",
|
||||||
"react-transition-group": "^4.4.2"
|
"react-transition-group": "^4.4.2",
|
||||||
|
"utils": "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^21.0.1",
|
"@rollup/plugin-commonjs": "^21.0.1",
|
||||||
"@rollup/plugin-node-resolve": "^13.1.1",
|
"@rollup/plugin-node-resolve": "^13.1.3",
|
||||||
"@rollup/plugin-typescript": "^8.3.0",
|
"@rollup/plugin-typescript": "^8.3.0",
|
||||||
"@types/react": "^17.0.38",
|
"@types/react": "^17.0.38",
|
||||||
"@types/react-scroll": "^1.8.3",
|
"@types/react-scroll": "^1.8.3",
|
||||||
"@types/react-transition-group": "^4.4.4",
|
"@types/react-transition-group": "^4.4.4",
|
||||||
"autoprefixer": "^10.4.0",
|
"autoprefixer": "^10.4.1",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"postcss": "^8.4.5",
|
"postcss": "^8.4.5",
|
||||||
"rollup": "^2.62.0",
|
"rollup": "^2.63.0",
|
||||||
"rollup-plugin-dts": "^4.1.0",
|
"rollup-plugin-dts": "^4.1.0",
|
||||||
"rollup-plugin-peer-deps-external": "^2.2.4",
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
||||||
"rollup-plugin-postcss": "^4.0.2",
|
"rollup-plugin-postcss": "^4.0.2",
|
||||||
"rollup-plugin-terser": "^7.0.2",
|
"rollup-plugin-terser": "^7.0.2",
|
||||||
"tailwindcss": "^3.0.8",
|
"tailwindcss": "^3.0.11",
|
||||||
"typescript": "^4.5.4"
|
"typescript": "^4.5.4"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { Block, Step } from '../../models'
|
|
||||||
import { animateScroll as scroll } from 'react-scroll'
|
import { animateScroll as scroll } from 'react-scroll'
|
||||||
import { TransitionGroup, CSSTransition } from 'react-transition-group'
|
import { TransitionGroup, CSSTransition } from 'react-transition-group'
|
||||||
import { ChatStep } from './ChatStep'
|
import { ChatStep } from './ChatStep'
|
||||||
import { AvatarSideContainer } from './AvatarSideContainer'
|
import { AvatarSideContainer } from './AvatarSideContainer'
|
||||||
import { HostAvatarsContext } from '../../contexts/HostAvatarsContext'
|
import { HostAvatarsContext } from '../../contexts/HostAvatarsContext'
|
||||||
|
import { Step, Table } from 'models'
|
||||||
|
|
||||||
type ChatBlockProps = {
|
type ChatBlockProps = {
|
||||||
block: Block
|
steps: Table<Step>
|
||||||
onBlockEnd: (nextBlockId?: string) => void
|
onBlockEnd: (nextBlockId?: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ChatBlock = ({ block, onBlockEnd }: ChatBlockProps) => {
|
export const ChatBlock = ({ steps, onBlockEnd }: ChatBlockProps) => {
|
||||||
const [displayedSteps, setDisplayedSteps] = useState<Step[]>([])
|
const [displayedSteps, setDisplayedSteps] = useState<Step[]>([])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setDisplayedSteps([block.steps[0]])
|
setDisplayedSteps([steps.byId[steps.allIds[0]]])
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -33,10 +33,10 @@ export const ChatBlock = ({ block, onBlockEnd }: ChatBlockProps) => {
|
|||||||
const currentStep = [...displayedSteps].pop()
|
const currentStep = [...displayedSteps].pop()
|
||||||
if (
|
if (
|
||||||
currentStep?.target?.blockId ||
|
currentStep?.target?.blockId ||
|
||||||
displayedSteps.length === block.steps.length
|
displayedSteps.length === steps.allIds.length
|
||||||
)
|
)
|
||||||
return onBlockEnd(currentStep?.target?.blockId)
|
return onBlockEnd(currentStep?.target?.blockId)
|
||||||
const nextStep = block.steps[displayedSteps.length]
|
const nextStep = steps.byId[displayedSteps.length]
|
||||||
if (nextStep) setDisplayedSteps([...displayedSteps, nextStep])
|
if (nextStep) setDisplayedSteps([...displayedSteps, nextStep])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { useAnswers } from '../../../contexts/AnswersContext'
|
import { useAnswers } from '../../../contexts/AnswersContext'
|
||||||
import { useHostAvatars } from '../../../contexts/HostAvatarsContext'
|
import { useHostAvatars } from '../../../contexts/HostAvatarsContext'
|
||||||
import { Step } from '../../../models'
|
import { Step } from 'models'
|
||||||
import { isTextInputStep, isTextStep } from '../../../services/utils'
|
import { isTextInputStep, isTextStep } from '../../../services/utils'
|
||||||
import { GuestBubble } from './bubbles/GuestBubble'
|
import { GuestBubble } from './bubbles/GuestBubble'
|
||||||
import { HostMessageBubble } from './bubbles/HostMessageBubble'
|
import { HostMessageBubble } from './bubbles/HostMessageBubble'
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React, { useEffect, useRef, useState } from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
import { useHostAvatars } from '../../../../contexts/HostAvatarsContext'
|
import { useHostAvatars } from '../../../../contexts/HostAvatarsContext'
|
||||||
import { useTypebot } from '../../../../contexts/TypebotContext'
|
import { useTypebot } from '../../../../contexts/TypebotContext'
|
||||||
import { StepType, TextStep } from '../../../../models'
|
import { StepType, TextStep } from 'models'
|
||||||
import { computeTypingTimeout } from '../../../../services/chat'
|
import { computeTypingTimeout } from '../../../../services/chat'
|
||||||
import { TypingContent } from './TypingContent'
|
import { TypingContent } from './TypingContent'
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import React, { useEffect, useRef, useState } from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
import { Answer, PublicTypebot } from '..'
|
|
||||||
|
|
||||||
import { Block } from '..'
|
|
||||||
import { ChatBlock } from './ChatBlock/ChatBlock'
|
import { ChatBlock } from './ChatBlock/ChatBlock'
|
||||||
import { useFrame } from 'react-frame-component'
|
import { useFrame } from 'react-frame-component'
|
||||||
import { setCssVariablesValue } from '../services/theme'
|
import { setCssVariablesValue } from '../services/theme'
|
||||||
import { useAnswers } from '../contexts/AnswersContext'
|
import { useAnswers } from '../contexts/AnswersContext'
|
||||||
import { deepEqual } from 'fast-equals'
|
import { deepEqual } from 'fast-equals'
|
||||||
|
import { Answer, Block, PublicTypebot } from 'models'
|
||||||
|
import { filterTable } from 'utils'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
typebot: PublicTypebot
|
typebot: PublicTypebot
|
||||||
@ -28,14 +28,17 @@ export const ConversationContainer = ({
|
|||||||
|
|
||||||
const displayNextBlock = (blockId?: string) => {
|
const displayNextBlock = (blockId?: string) => {
|
||||||
if (!blockId) return onCompleted()
|
if (!blockId) return onCompleted()
|
||||||
const nextBlock = typebot.blocks.find((b) => b.id === blockId)
|
const nextBlock = typebot.blocks.byId[blockId]
|
||||||
if (!nextBlock) return onCompleted()
|
if (!nextBlock) return onCompleted()
|
||||||
onNewBlockVisible(blockId)
|
onNewBlockVisible(blockId)
|
||||||
setDisplayedBlocks([...displayedBlocks, nextBlock])
|
setDisplayedBlocks([...displayedBlocks, nextBlock])
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const firstBlockId = typebot.startBlock.steps[0].target?.blockId
|
const blocks = typebot.blocks
|
||||||
|
const firstBlockId =
|
||||||
|
typebot.steps.byId[blocks.byId[blocks.allIds[0]].stepIds[0]].target
|
||||||
|
?.blockId
|
||||||
if (firstBlockId) displayNextBlock(firstBlockId)
|
if (firstBlockId) displayNextBlock(firstBlockId)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@ -58,7 +61,7 @@ export const ConversationContainer = ({
|
|||||||
{displayedBlocks.map((block, idx) => (
|
{displayedBlocks.map((block, idx) => (
|
||||||
<ChatBlock
|
<ChatBlock
|
||||||
key={block.id + idx}
|
key={block.id + idx}
|
||||||
block={block}
|
steps={filterTable(block.stepIds, typebot.steps)}
|
||||||
onBlockEnd={displayNextBlock}
|
onBlockEnd={displayNextBlock}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import React, { useMemo } from 'react'
|
import React, { useMemo } from 'react'
|
||||||
import { Answer, BackgroundType, PublicTypebot } from '../models'
|
|
||||||
import { TypebotContext } from '../contexts/TypebotContext'
|
import { TypebotContext } from '../contexts/TypebotContext'
|
||||||
import Frame from 'react-frame-component'
|
import Frame from 'react-frame-component'
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
import style from '../assets/style.css'
|
import style from '../assets/style.css'
|
||||||
import { ConversationContainer } from './ConversationContainer'
|
import { ConversationContainer } from './ConversationContainer'
|
||||||
import { AnswersContext } from '../contexts/AnswersContext'
|
import { AnswersContext } from '../contexts/AnswersContext'
|
||||||
|
import { Answer, BackgroundType, PublicTypebot } from 'models'
|
||||||
|
|
||||||
export type TypebotViewerProps = {
|
export type TypebotViewerProps = {
|
||||||
typebot: PublicTypebot
|
typebot: PublicTypebot
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Answer } from '../models'
|
import { Answer } from 'models'
|
||||||
import React, { createContext, ReactNode, useContext, useState } from 'react'
|
import React, { createContext, ReactNode, useContext, useState } from 'react'
|
||||||
|
|
||||||
const answersContext = createContext<{
|
const answersContext = createContext<{
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
import { PublicTypebot } from 'models'
|
||||||
import React, { createContext, ReactNode, useContext } from 'react'
|
import React, { createContext, ReactNode, useContext } from 'react'
|
||||||
import { PublicTypebot } from '../models/publicTypebot'
|
|
||||||
|
|
||||||
const typebotContext = createContext<{
|
const typebotContext = createContext<{
|
||||||
typebot: PublicTypebot
|
typebot: PublicTypebot
|
||||||
|
@ -1,3 +1 @@
|
|||||||
export * from './components/TypebotViewer'
|
export * from './components/TypebotViewer'
|
||||||
export * from './models'
|
|
||||||
export { parseNewTypebot } from './services/utils'
|
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
import { PublicTypebot as PublicTypebotFromPrisma } from 'db'
|
|
||||||
import { Block, Settings, StartBlock, Theme } from '.'
|
|
||||||
|
|
||||||
export type PublicTypebot = Omit<
|
|
||||||
PublicTypebotFromPrisma,
|
|
||||||
'blocks' | 'startBlock' | 'theme' | 'settings'
|
|
||||||
> & {
|
|
||||||
blocks: Block[]
|
|
||||||
startBlock: StartBlock
|
|
||||||
theme: Theme
|
|
||||||
settings: Settings
|
|
||||||
}
|
|
@ -1,87 +0,0 @@
|
|||||||
import { Typebot as TypebotFromPrisma } from 'db'
|
|
||||||
|
|
||||||
export type Typebot = Omit<
|
|
||||||
TypebotFromPrisma,
|
|
||||||
'blocks' | 'startBlock' | 'theme' | 'settings'
|
|
||||||
> & {
|
|
||||||
blocks: Block[]
|
|
||||||
startBlock: StartBlock
|
|
||||||
theme: Theme
|
|
||||||
settings: Settings
|
|
||||||
}
|
|
||||||
|
|
||||||
export type StartBlock = {
|
|
||||||
id: `start-block`
|
|
||||||
graphCoordinates: {
|
|
||||||
x: number
|
|
||||||
y: number
|
|
||||||
}
|
|
||||||
title: string
|
|
||||||
steps: [StartStep]
|
|
||||||
}
|
|
||||||
|
|
||||||
export type StartStep = {
|
|
||||||
id: 'start-step'
|
|
||||||
blockId: 'start-block'
|
|
||||||
target?: Target
|
|
||||||
type: StepType.START
|
|
||||||
label: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Block = {
|
|
||||||
id: string
|
|
||||||
title: string
|
|
||||||
steps: Step[]
|
|
||||||
graphCoordinates: {
|
|
||||||
x: number
|
|
||||||
y: number
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum StepType {
|
|
||||||
START = 'start',
|
|
||||||
TEXT = 'text',
|
|
||||||
TEXT_INPUT = 'text input',
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Target = { blockId: string; stepId?: string }
|
|
||||||
|
|
||||||
export type Step = BubbleStep | InputStep
|
|
||||||
export type BubbleStep = TextStep
|
|
||||||
export type InputStep = TextInputStep
|
|
||||||
export type StepBase = { id: string; blockId: string; target?: Target }
|
|
||||||
export type TextStep = StepBase & {
|
|
||||||
type: StepType.TEXT
|
|
||||||
content: { html: string; richText: unknown[]; plainText: string }
|
|
||||||
}
|
|
||||||
export type TextInputStep = StepBase & {
|
|
||||||
type: StepType.TEXT_INPUT
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Theme = {
|
|
||||||
general: {
|
|
||||||
font: string
|
|
||||||
background: Background
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum BackgroundType {
|
|
||||||
COLOR = 'Color',
|
|
||||||
IMAGE = 'Image',
|
|
||||||
NONE = 'None',
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Background = {
|
|
||||||
type: BackgroundType
|
|
||||||
content: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Settings = {
|
|
||||||
typingEmulation: TypingEmulationSettings
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TypingEmulationSettings = {
|
|
||||||
enabled: boolean
|
|
||||||
speed: number
|
|
||||||
maxDelay: number
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
import { TypingEmulationSettings } from '../models'
|
import { TypingEmulationSettings } from 'models'
|
||||||
|
|
||||||
export const computeTypingTimeout = (
|
export const computeTypingTimeout = (
|
||||||
bubbleContent: string,
|
bubbleContent: string,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { BackgroundType, Theme } from '../models'
|
import { BackgroundType, Theme } from 'models'
|
||||||
|
|
||||||
const cssVariableNames = {
|
const cssVariableNames = {
|
||||||
container: {
|
container: {
|
||||||
|
@ -1,55 +1,7 @@
|
|||||||
import { Prisma } from 'db'
|
import { Step, TextStep, StepType, TextInputStep } from 'models'
|
||||||
import {
|
|
||||||
Step,
|
|
||||||
TextStep,
|
|
||||||
StepType,
|
|
||||||
TextInputStep,
|
|
||||||
BackgroundType,
|
|
||||||
Settings,
|
|
||||||
StartBlock,
|
|
||||||
Theme,
|
|
||||||
} from '../models'
|
|
||||||
|
|
||||||
export const isTextStep = (step: Step): step is TextStep =>
|
export const isTextStep = (step: Step): step is TextStep =>
|
||||||
step.type === StepType.TEXT
|
step.type === StepType.TEXT
|
||||||
|
|
||||||
export const isTextInputStep = (step: Step): step is TextInputStep =>
|
export const isTextInputStep = (step: Step): step is TextInputStep =>
|
||||||
step.type === StepType.TEXT_INPUT
|
step.type === StepType.TEXT_INPUT
|
||||||
|
|
||||||
export const parseNewTypebot = ({
|
|
||||||
ownerId,
|
|
||||||
folderId,
|
|
||||||
name,
|
|
||||||
}: {
|
|
||||||
ownerId: string
|
|
||||||
folderId: string | null
|
|
||||||
name: string
|
|
||||||
}): Prisma.TypebotUncheckedCreateInput => {
|
|
||||||
const startBlock: StartBlock = {
|
|
||||||
id: 'start-block',
|
|
||||||
title: 'Start',
|
|
||||||
graphCoordinates: { x: 0, y: 0 },
|
|
||||||
steps: [
|
|
||||||
{
|
|
||||||
id: 'start-step',
|
|
||||||
blockId: 'start-block',
|
|
||||||
label: 'Form starts here',
|
|
||||||
type: StepType.START,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
const theme: Theme = {
|
|
||||||
general: {
|
|
||||||
font: 'Open Sans',
|
|
||||||
background: { type: BackgroundType.NONE, content: '#ffffff' },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
const settings: Settings = {
|
|
||||||
typingEmulation: {
|
|
||||||
enabled: true,
|
|
||||||
speed: 300,
|
|
||||||
maxDelay: 1.5,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return { folderId, name, ownerId, startBlock, theme, settings }
|
|
||||||
}
|
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- You are about to drop the column `startBlock` on the `PublicTypebot` table. All the data in the column will be lost.
|
||||||
|
- You are about to drop the column `startBlock` on the `Typebot` table. All the data in the column will be lost.
|
||||||
|
- Added the required column `steps` to the `PublicTypebot` table without a default value. This is not possible if the table is not empty.
|
||||||
|
- Changed the type of `blocks` on the `PublicTypebot` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
|
||||||
|
- Added the required column `steps` to the `Typebot` table without a default value. This is not possible if the table is not empty.
|
||||||
|
- Changed the type of `blocks` on the `Typebot` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "PublicTypebot" DROP COLUMN "startBlock",
|
||||||
|
ADD COLUMN "steps" JSONB NOT NULL,
|
||||||
|
DROP COLUMN "blocks",
|
||||||
|
ADD COLUMN "blocks" JSONB NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Typebot" DROP COLUMN "startBlock",
|
||||||
|
ADD COLUMN "steps" JSONB NOT NULL,
|
||||||
|
DROP COLUMN "blocks",
|
||||||
|
ADD COLUMN "blocks" JSONB NOT NULL;
|
@ -90,23 +90,23 @@ model Typebot {
|
|||||||
results Result[]
|
results Result[]
|
||||||
folderId String?
|
folderId String?
|
||||||
folder DashboardFolder? @relation(fields: [folderId], references: [id])
|
folder DashboardFolder? @relation(fields: [folderId], references: [id])
|
||||||
blocks Json[]
|
blocks Json
|
||||||
startBlock Json
|
steps Json
|
||||||
theme Json
|
theme Json
|
||||||
settings Json
|
settings Json
|
||||||
publicId String? @unique
|
publicId String? @unique
|
||||||
}
|
}
|
||||||
|
|
||||||
model PublicTypebot {
|
model PublicTypebot {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
typebotId String @unique
|
typebotId String @unique
|
||||||
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
|
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
|
||||||
name String
|
name String
|
||||||
blocks Json[]
|
blocks Json
|
||||||
startBlock Json
|
steps Json
|
||||||
theme Json
|
theme Json
|
||||||
settings Json
|
settings Json
|
||||||
publicId String? @unique
|
publicId String? @unique
|
||||||
}
|
}
|
||||||
|
|
||||||
model Result {
|
model Result {
|
||||||
|
7
packages/models/.gitignore
vendored
Normal file
7
packages/models/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
node_modules
|
||||||
|
# Keep environment variables out of version control
|
||||||
|
.env
|
||||||
|
|
||||||
|
dist
|
||||||
|
types
|
||||||
|
yarn-error.log
|
19
packages/models/package.json
Normal file
19
packages/models/package.json
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name": "models",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "types/index.d.ts",
|
||||||
|
"license": "AGPL-3.0-or-later",
|
||||||
|
"private": true,
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^4.5.4"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"next": "^12.0.7",
|
||||||
|
"db": "*"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"dev": "tsc --watch"
|
||||||
|
}
|
||||||
|
}
|
@ -2,3 +2,4 @@ export * from './typebot'
|
|||||||
export * from './publicTypebot'
|
export * from './publicTypebot'
|
||||||
export * from './result'
|
export * from './result'
|
||||||
export * from './answer'
|
export * from './answer'
|
||||||
|
export * from './utils'
|
13
packages/models/src/publicTypebot.ts
Normal file
13
packages/models/src/publicTypebot.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { PublicTypebot as PublicTypebotFromPrisma } from 'db'
|
||||||
|
import { Block, Settings, Step, Theme } from './typebot'
|
||||||
|
import { Table } from './utils'
|
||||||
|
|
||||||
|
export type PublicTypebot = Omit<
|
||||||
|
PublicTypebotFromPrisma,
|
||||||
|
'blocks' | 'startBlock' | 'theme' | 'settings' | 'steps'
|
||||||
|
> & {
|
||||||
|
blocks: Table<Block>
|
||||||
|
steps: Table<Step>
|
||||||
|
theme: Theme
|
||||||
|
settings: Settings
|
||||||
|
}
|
4
packages/models/src/typebot/index.ts
Normal file
4
packages/models/src/typebot/index.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export * from './typebot'
|
||||||
|
export * from './steps'
|
||||||
|
export * from './theme'
|
||||||
|
export * from './settings'
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user