2022-01-03 17:39:59 +01:00
|
|
|
import { Tag, Text, VStack } from '@chakra-ui/react'
|
2022-02-02 08:05:02 +01:00
|
|
|
import { useTypebot } from 'contexts/TypebotContext'
|
2022-01-03 17:39:59 +01:00
|
|
|
import React, { useMemo } from 'react'
|
|
|
|
import { AnswersCount } from 'services/analytics'
|
|
|
|
import { computeSourceCoordinates } from 'services/graph'
|
2022-02-04 19:00:08 +01:00
|
|
|
import { byId, isDefined } from 'utils'
|
2022-01-03 17:39:59 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
answersCounts: AnswersCount[]
|
|
|
|
blockId: string
|
|
|
|
}
|
|
|
|
|
2022-02-02 08:05:02 +01:00
|
|
|
export const DropOffNode = ({ answersCounts, blockId }: Props) => {
|
|
|
|
const { typebot } = useTypebot()
|
2022-01-03 17:39:59 +01:00
|
|
|
|
|
|
|
const totalAnswers = useMemo(
|
|
|
|
() => answersCounts.find((a) => a.blockId === blockId)?.totalAnswers,
|
|
|
|
[answersCounts, blockId]
|
|
|
|
)
|
|
|
|
|
|
|
|
const { totalDroppedUser, dropOffRate } = useMemo(() => {
|
|
|
|
if (!typebot || totalAnswers === undefined)
|
|
|
|
return { previousTotal: undefined, dropOffRate: undefined }
|
2022-02-04 19:00:08 +01:00
|
|
|
const previousBlockIds = typebot.edges
|
|
|
|
.map((edge) =>
|
|
|
|
edge.to.blockId === blockId ? edge.from.blockId : undefined
|
|
|
|
)
|
2022-01-19 18:54:49 +01:00
|
|
|
.filter((blockId) => isDefined(blockId))
|
2022-01-06 09:40:56 +01:00
|
|
|
const previousTotal = answersCounts
|
|
|
|
.filter((a) => previousBlockIds.includes(a.blockId))
|
2022-01-03 17:39:59 +01:00
|
|
|
.reduce((prev, acc) => acc.totalAnswers + prev, 0)
|
|
|
|
if (previousTotal === 0)
|
|
|
|
return { previousTotal: undefined, dropOffRate: undefined }
|
|
|
|
const totalDroppedUser = previousTotal - totalAnswers
|
|
|
|
|
|
|
|
return {
|
|
|
|
totalDroppedUser,
|
|
|
|
dropOffRate: Math.round((totalDroppedUser / previousTotal) * 100),
|
|
|
|
}
|
|
|
|
}, [answersCounts, blockId, totalAnswers, typebot])
|
|
|
|
|
|
|
|
const labelCoordinates = useMemo(() => {
|
2022-02-04 19:00:08 +01:00
|
|
|
const sourceBlock = typebot?.blocks.find(byId(blockId))
|
2022-01-03 17:39:59 +01:00
|
|
|
if (!sourceBlock) return
|
|
|
|
return computeSourceCoordinates(
|
|
|
|
sourceBlock?.graphCoordinates,
|
2022-02-04 19:00:08 +01:00
|
|
|
sourceBlock?.steps.length - 1
|
2022-01-03 17:39:59 +01:00
|
|
|
)
|
|
|
|
}, [blockId, typebot])
|
|
|
|
|
|
|
|
if (!labelCoordinates) return <></>
|
|
|
|
return (
|
|
|
|
<VStack
|
|
|
|
bgColor={'red.500'}
|
|
|
|
color="white"
|
|
|
|
rounded="md"
|
|
|
|
p="2"
|
|
|
|
justifyContent="center"
|
|
|
|
style={{
|
|
|
|
transform: `translate(${labelCoordinates.x - 20}px, ${
|
|
|
|
labelCoordinates.y + 80
|
|
|
|
}px)`,
|
|
|
|
}}
|
|
|
|
pos="absolute"
|
|
|
|
>
|
|
|
|
<Text>{dropOffRate}%</Text>
|
|
|
|
<Tag colorScheme="red">{totalDroppedUser} users</Tag>
|
|
|
|
</VStack>
|
|
|
|
)
|
|
|
|
}
|