fix(analytics): 🐛 Analytics board
This commit is contained in:
100
apps/builder/components/shared/Graph/Edges/DropOffEdge.tsx
Normal file
100
apps/builder/components/shared/Graph/Edges/DropOffEdge.tsx
Normal file
@@ -0,0 +1,100 @@
|
||||
import { VStack, Tag, Text } from '@chakra-ui/react'
|
||||
import { useGraph } from 'contexts/GraphContext'
|
||||
import { useTypebot } from 'contexts/TypebotContext'
|
||||
import React, { useMemo } from 'react'
|
||||
import { AnswersCount } from 'services/analytics'
|
||||
import {
|
||||
getEndpointTopOffset,
|
||||
computeSourceCoordinates,
|
||||
computeDropOffPath,
|
||||
} from 'services/graph'
|
||||
import { byId, isDefined } from 'utils'
|
||||
|
||||
type Props = {
|
||||
blockId: string
|
||||
answersCounts: AnswersCount[]
|
||||
}
|
||||
|
||||
export const DropOffEdge = ({ answersCounts, blockId }: Props) => {
|
||||
const { sourceEndpoints, graphPosition, blocksCoordinates } = useGraph()
|
||||
const { publishedTypebot } = useTypebot()
|
||||
|
||||
const totalAnswers = useMemo(
|
||||
() => answersCounts.find((a) => a.blockId === blockId)?.totalAnswers,
|
||||
[answersCounts, blockId]
|
||||
)
|
||||
|
||||
const { totalDroppedUser, dropOffRate } = useMemo(() => {
|
||||
if (!publishedTypebot || totalAnswers === undefined)
|
||||
return { previousTotal: undefined, dropOffRate: undefined }
|
||||
const previousBlockIds = publishedTypebot.edges
|
||||
.map((edge) =>
|
||||
edge.to.blockId === blockId ? edge.from.blockId : undefined
|
||||
)
|
||||
.filter(isDefined)
|
||||
const previousTotal = answersCounts
|
||||
.filter((a) => previousBlockIds.includes(a.blockId))
|
||||
.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, publishedTypebot])
|
||||
|
||||
const block = publishedTypebot?.blocks.find(byId(blockId))
|
||||
const sourceTop = useMemo(
|
||||
() =>
|
||||
getEndpointTopOffset(
|
||||
graphPosition,
|
||||
sourceEndpoints,
|
||||
block?.steps[block.steps.length - 1].id,
|
||||
true
|
||||
),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[block?.steps, graphPosition, sourceEndpoints, blocksCoordinates]
|
||||
)
|
||||
|
||||
const labelCoordinates = useMemo(() => {
|
||||
if (!blocksCoordinates[blockId]) return
|
||||
return computeSourceCoordinates(blocksCoordinates[blockId], sourceTop ?? 0)
|
||||
}, [blocksCoordinates, blockId, sourceTop])
|
||||
|
||||
if (!labelCoordinates) return <></>
|
||||
return (
|
||||
<>
|
||||
<path
|
||||
d={computeDropOffPath(
|
||||
{ x: labelCoordinates.x - 300, y: labelCoordinates.y },
|
||||
sourceTop ?? 0
|
||||
)}
|
||||
stroke="#e53e3e"
|
||||
strokeWidth="2px"
|
||||
markerEnd="url(#red-arrow)"
|
||||
fill="none"
|
||||
/>
|
||||
<foreignObject
|
||||
width="80"
|
||||
height="80"
|
||||
x={labelCoordinates.x - 20}
|
||||
y={labelCoordinates.y + 80}
|
||||
>
|
||||
<VStack
|
||||
bgColor={'red.500'}
|
||||
color="white"
|
||||
rounded="md"
|
||||
p="2"
|
||||
justifyContent="center"
|
||||
w="full"
|
||||
h="full"
|
||||
>
|
||||
<Text>{dropOffRate}%</Text>
|
||||
<Tag colorScheme="red">{totalDroppedUser} users</Tag>
|
||||
</VStack>
|
||||
</foreignObject>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -22,6 +22,7 @@ export const Edge = ({ edge }: { edge: EdgeProps }) => {
|
||||
targetEndpoints,
|
||||
graphPosition,
|
||||
blocksCoordinates,
|
||||
isReadOnly,
|
||||
} = useGraph()
|
||||
const isPreviewing = previewingEdge?.id === edge.id
|
||||
|
||||
@@ -35,14 +36,21 @@ export const Edge = ({ edge }: { edge: EdgeProps }) => {
|
||||
getEndpointTopOffset(
|
||||
graphPosition,
|
||||
sourceEndpoints,
|
||||
getSourceEndpointId(edge)
|
||||
getSourceEndpointId(edge),
|
||||
isReadOnly
|
||||
),
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[edge, graphPosition, sourceEndpoints, sourceBlockCoordinates?.y]
|
||||
)
|
||||
const targetTop = useMemo(
|
||||
() => getEndpointTopOffset(graphPosition, targetEndpoints, edge?.to.stepId),
|
||||
() =>
|
||||
getEndpointTopOffset(
|
||||
graphPosition,
|
||||
targetEndpoints,
|
||||
edge?.to.stepId,
|
||||
isReadOnly
|
||||
),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[graphPosition, targetEndpoints, edge?.to.stepId, targetBlockCoordinates?.y]
|
||||
)
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import { chakra } from '@chakra-ui/system'
|
||||
import { Edge as EdgeProps } from 'models'
|
||||
import React from 'react'
|
||||
import { AnswersCount } from 'services/analytics'
|
||||
import { DrawingEdge } from './DrawingEdge'
|
||||
import { DropOffEdge } from './DropOffEdge'
|
||||
import { Edge } from './Edge'
|
||||
|
||||
type Props = {
|
||||
edges: EdgeProps[]
|
||||
answersCounts?: AnswersCount[]
|
||||
}
|
||||
export const Edges = ({ edges }: Props) => {
|
||||
export const Edges = ({ edges, answersCounts }: Props) => {
|
||||
return (
|
||||
<chakra.svg
|
||||
width="full"
|
||||
@@ -21,6 +24,13 @@ export const Edges = ({ edges }: Props) => {
|
||||
{edges.map((edge) => (
|
||||
<Edge key={edge.id} edge={edge} />
|
||||
))}
|
||||
{answersCounts?.slice(1)?.map((answerCount) => (
|
||||
<DropOffEdge
|
||||
key={answerCount.blockId}
|
||||
answersCounts={answersCounts}
|
||||
blockId={answerCount.blockId}
|
||||
/>
|
||||
))}
|
||||
<marker
|
||||
id={'arrow'}
|
||||
refX="8"
|
||||
@@ -51,6 +61,21 @@ export const Edges = ({ edges }: Props) => {
|
||||
fill="#1a5fff"
|
||||
/>
|
||||
</marker>
|
||||
<marker
|
||||
id={'red-arrow'}
|
||||
refX="8"
|
||||
refY="4"
|
||||
orient="auto"
|
||||
viewBox="0 0 20 20"
|
||||
markerUnits="userSpaceOnUse"
|
||||
markerWidth="20"
|
||||
markerHeight="20"
|
||||
>
|
||||
<path
|
||||
d="M7.07138888,5.50174526 L2.43017246,7.82235347 C1.60067988,8.23709976 0.592024983,7.90088146 0.177278692,7.07138888 C0.0606951226,6.83822174 0,6.58111307 0,6.32042429 L0,1.67920787 C0,0.751806973 0.751806973,0 1.67920787,0 C1.93989666,0 2.19700532,0.0606951226 2.43017246,0.177278692 L7,3 C7.82949258,3.41474629 8.23709976,3.92128809 7.82235347,4.75078067 C7.6598671,5.07575341 7.39636161,5.33925889 7.07138888,5.50174526 Z"
|
||||
fill="#e53e3e"
|
||||
/>
|
||||
</marker>
|
||||
</chakra.svg>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user