2022-12-22 09:59:50 +01:00
|
|
|
import {
|
|
|
|
|
VStack,
|
|
|
|
|
Tag,
|
|
|
|
|
Text,
|
|
|
|
|
Tooltip,
|
|
|
|
|
useColorModeValue,
|
|
|
|
|
theme,
|
|
|
|
|
} from '@chakra-ui/react'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { useTypebot } from '@/features/editor/providers/TypebotProvider'
|
|
|
|
|
import { useWorkspace } from '@/features/workspace/WorkspaceProvider'
|
2022-02-11 18:06:59 +01:00
|
|
|
import React, { useMemo } from 'react'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { byId, isDefined } from '@typebot.io/lib'
|
2023-02-28 15:06:43 +01:00
|
|
|
import { useEndpoints } from '../../providers/EndpointsProvider'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { useGroupsCoordinates } from '../../providers/GroupsCoordinateProvider'
|
|
|
|
|
import { AnswersCount } from '@/features/analytics/types'
|
|
|
|
|
import { isProPlan } from '@/features/billing/helpers/isProPlan'
|
|
|
|
|
import { computeDropOffPath } from '../../helpers/computeDropOffPath'
|
|
|
|
|
import { computeSourceCoordinates } from '../../helpers/computeSourceCoordinates'
|
2022-02-11 18:06:59 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2022-06-11 07:27:38 +02:00
|
|
|
groupId: string
|
2022-02-11 18:06:59 +01:00
|
|
|
answersCounts: AnswersCount[]
|
2022-02-13 06:53:48 +01:00
|
|
|
onUnlockProPlanClick?: () => void
|
2022-02-11 18:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-13 06:53:48 +01:00
|
|
|
export const DropOffEdge = ({
|
|
|
|
|
answersCounts,
|
2022-06-11 07:27:38 +02:00
|
|
|
groupId,
|
2022-02-13 06:53:48 +01:00
|
|
|
onUnlockProPlanClick,
|
|
|
|
|
}: Props) => {
|
2022-12-22 09:59:50 +01:00
|
|
|
const dropOffColor = useColorModeValue(
|
|
|
|
|
theme.colors.red[500],
|
2023-01-27 09:50:36 +01:00
|
|
|
theme.colors.red[400]
|
2022-12-22 09:59:50 +01:00
|
|
|
)
|
2022-05-13 15:22:44 -07:00
|
|
|
const { workspace } = useWorkspace()
|
2022-06-26 16:12:28 +02:00
|
|
|
const { groupsCoordinates } = useGroupsCoordinates()
|
2023-02-28 15:06:43 +01:00
|
|
|
const { sourceEndpointYOffsets: sourceEndpoints } = useEndpoints()
|
2022-02-11 18:06:59 +01:00
|
|
|
const { publishedTypebot } = useTypebot()
|
|
|
|
|
|
2022-11-15 09:35:48 +01:00
|
|
|
const isWorkspaceProPlan = isProPlan(workspace)
|
2022-02-13 06:53:48 +01:00
|
|
|
|
2022-02-11 18:06:59 +01:00
|
|
|
const totalAnswers = useMemo(
|
2022-06-11 07:27:38 +02:00
|
|
|
() => answersCounts.find((a) => a.groupId === groupId)?.totalAnswers,
|
|
|
|
|
[answersCounts, groupId]
|
2022-02-11 18:06:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const { totalDroppedUser, dropOffRate } = useMemo(() => {
|
|
|
|
|
if (!publishedTypebot || totalAnswers === undefined)
|
|
|
|
|
return { previousTotal: undefined, dropOffRate: undefined }
|
2022-06-11 07:27:38 +02:00
|
|
|
const previousGroupIds = publishedTypebot.edges
|
2022-02-11 18:06:59 +01:00
|
|
|
.map((edge) =>
|
2022-06-11 07:27:38 +02:00
|
|
|
edge.to.groupId === groupId ? edge.from.groupId : undefined
|
2022-02-11 18:06:59 +01:00
|
|
|
)
|
|
|
|
|
.filter(isDefined)
|
|
|
|
|
const previousTotal = answersCounts
|
2022-06-11 07:27:38 +02:00
|
|
|
.filter((a) => previousGroupIds.includes(a.groupId))
|
2022-02-11 18:06: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),
|
|
|
|
|
}
|
2022-06-11 07:27:38 +02:00
|
|
|
}, [answersCounts, groupId, totalAnswers, publishedTypebot])
|
2022-02-11 18:06:59 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
const group = publishedTypebot?.groups.find(byId(groupId))
|
2023-02-28 15:06:43 +01:00
|
|
|
|
|
|
|
|
const sourceTop = useMemo(() => {
|
|
|
|
|
const endpointId = group?.blocks[group.blocks.length - 1].id
|
|
|
|
|
return endpointId ? sourceEndpoints.get(endpointId)?.y : undefined
|
|
|
|
|
}, [group?.blocks, sourceEndpoints])
|
2022-02-11 18:06:59 +01:00
|
|
|
|
|
|
|
|
const labelCoordinates = useMemo(() => {
|
2022-06-11 07:27:38 +02:00
|
|
|
if (!groupsCoordinates[groupId]) return
|
|
|
|
|
return computeSourceCoordinates(groupsCoordinates[groupId], sourceTop ?? 0)
|
|
|
|
|
}, [groupsCoordinates, groupId, sourceTop])
|
2022-02-11 18:06:59 +01:00
|
|
|
|
|
|
|
|
if (!labelCoordinates) return <></>
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<path
|
|
|
|
|
d={computeDropOffPath(
|
|
|
|
|
{ x: labelCoordinates.x - 300, y: labelCoordinates.y },
|
|
|
|
|
sourceTop ?? 0
|
|
|
|
|
)}
|
2022-12-22 09:59:50 +01:00
|
|
|
stroke={dropOffColor}
|
2022-02-11 18:06:59 +01:00
|
|
|
strokeWidth="2px"
|
|
|
|
|
markerEnd="url(#red-arrow)"
|
|
|
|
|
fill="none"
|
|
|
|
|
/>
|
|
|
|
|
<foreignObject
|
2022-02-13 06:53:48 +01:00
|
|
|
width="100"
|
2022-02-11 18:06:59 +01:00
|
|
|
height="80"
|
2022-02-13 06:53:48 +01:00
|
|
|
x={labelCoordinates.x - 30}
|
2022-02-11 18:06:59 +01:00
|
|
|
y={labelCoordinates.y + 80}
|
|
|
|
|
>
|
2022-02-13 06:53:48 +01:00
|
|
|
<Tooltip
|
|
|
|
|
label="Unlock Drop-off rate by upgrading to Pro plan"
|
2022-11-15 09:35:48 +01:00
|
|
|
isDisabled={isWorkspaceProPlan}
|
2022-02-11 18:06:59 +01:00
|
|
|
>
|
2022-02-13 06:53:48 +01:00
|
|
|
<VStack
|
2022-12-22 09:59:50 +01:00
|
|
|
bgColor={dropOffColor}
|
2022-02-13 06:53:48 +01:00
|
|
|
color="white"
|
|
|
|
|
rounded="md"
|
|
|
|
|
p="2"
|
|
|
|
|
justifyContent="center"
|
|
|
|
|
w="full"
|
|
|
|
|
h="full"
|
2022-11-15 09:35:48 +01:00
|
|
|
onClick={isWorkspaceProPlan ? undefined : onUnlockProPlanClick}
|
|
|
|
|
cursor={isWorkspaceProPlan ? 'auto' : 'pointer'}
|
2022-02-13 06:53:48 +01:00
|
|
|
>
|
2022-11-15 09:35:48 +01:00
|
|
|
<Text filter={isWorkspaceProPlan ? '' : 'blur(2px)'}>
|
|
|
|
|
{isWorkspaceProPlan ? (
|
2022-09-24 08:58:23 +02:00
|
|
|
dropOffRate
|
|
|
|
|
) : (
|
|
|
|
|
<Text as="span" filter="blur(2px)">
|
|
|
|
|
X
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
%
|
|
|
|
|
</Text>
|
2022-02-13 06:53:48 +01:00
|
|
|
<Tag colorScheme="red">
|
2022-11-15 09:35:48 +01:00
|
|
|
{isWorkspaceProPlan ? (
|
2022-09-24 08:58:23 +02:00
|
|
|
totalDroppedUser
|
|
|
|
|
) : (
|
|
|
|
|
<Text as="span" filter="blur(3px)" mr="1">
|
|
|
|
|
NN
|
|
|
|
|
</Text>
|
|
|
|
|
)}{' '}
|
|
|
|
|
users
|
2022-02-13 06:53:48 +01:00
|
|
|
</Tag>
|
|
|
|
|
</VStack>
|
|
|
|
|
</Tooltip>
|
2022-02-11 18:06:59 +01:00
|
|
|
</foreignObject>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|