♻️ Introduce typebot v6 with events (#1013)

Closes #885
This commit is contained in:
Baptiste Arnaud
2023-11-08 15:34:16 +01:00
committed by GitHub
parent 68e4fc71fb
commit 35300eaf34
634 changed files with 58971 additions and 31449 deletions

View File

@@ -1,96 +0,0 @@
import { isInputBlock } from '@typebot.io/lib'
import { PublicTypebot } from '@typebot.io/schemas'
import { TotalAnswersInBlock } from '@typebot.io/schemas/features/analytics'
export const computePreviousTotalAnswers = (
publishedTypebot: PublicTypebot,
currentBlockId: string,
totalAnswersInBlocks: TotalAnswersInBlock[],
visitedBlocks: string[] = []
): number => {
let totalAnswers = 0
const allBlocks = publishedTypebot.groups.flatMap((group) => group.blocks)
const currentGroup = publishedTypebot.groups.find((group) =>
group.blocks.find((block) => block.id === currentBlockId)
)
if (!currentGroup) return 0
const currentBlockIndex = currentGroup.blocks.findIndex(
(block) => block.id === currentBlockId
)
const previousBlocks = currentGroup.blocks.slice(0, currentBlockIndex + 1)
for (const block of previousBlocks.reverse()) {
if (visitedBlocks.includes(block.id)) continue
if (
currentBlockId !== block.id &&
(isInputBlock(block) || block.type === 'start')
) {
visitedBlocks.push(block.id)
return (
totalAnswersInBlocks.find(
(totalAnswersInBlock) =>
totalAnswersInBlock.blockId === block.id &&
totalAnswersInBlock.itemId === undefined
)?.total ?? 0
)
}
const connectedEdges = publishedTypebot.edges.filter(
(edge) => edge.to.blockId === block.id
)
if (connectedEdges.length) {
for (const connectedEdge of connectedEdges) {
const connectedBlock = allBlocks.find(
(block) => block.id === connectedEdge.from.blockId
)
if (connectedBlock && !visitedBlocks.includes(connectedBlock.id)) {
if (isInputBlock(connectedBlock) || connectedBlock.type === 'start') {
visitedBlocks.push(connectedBlock.id)
totalAnswers +=
totalAnswersInBlocks.find(
(totalAnswersInBlock) =>
totalAnswersInBlock.blockId === connectedEdge.from.blockId &&
totalAnswersInBlock.itemId === connectedEdge.from.itemId
)?.total ?? 0
} else {
totalAnswers += computePreviousTotalAnswers(
publishedTypebot,
connectedBlock.id,
totalAnswersInBlocks,
visitedBlocks
)
}
}
}
}
}
const edgesConnectedToGroup = publishedTypebot.edges.filter(
(edge) => edge.to.groupId === currentGroup.id
)
if (edgesConnectedToGroup.length) {
for (const connectedEdge of edgesConnectedToGroup) {
const connectedBlock = allBlocks.find(
(block) => block.id === connectedEdge.from.blockId
)
if (connectedBlock && !visitedBlocks.includes(connectedBlock.id)) {
if (isInputBlock(connectedBlock) || connectedBlock.type === 'start') {
visitedBlocks.push(connectedBlock.id)
totalAnswers +=
totalAnswersInBlocks.find(
(totalAnswersInBlock) =>
totalAnswersInBlock.blockId === connectedEdge.from.blockId &&
totalAnswersInBlock.itemId === connectedEdge.from.itemId
)?.total ?? 0
} else {
totalAnswers += computePreviousTotalAnswers(
publishedTypebot,
connectedBlock.id,
totalAnswersInBlocks,
visitedBlocks
)
}
}
}
}
return totalAnswers
}

View File

@@ -0,0 +1,60 @@
import { isInputBlock, isNotDefined } from '@typebot.io/lib'
import { PublicTypebotV6 } from '@typebot.io/schemas'
import {
TotalAnswers,
TotalVisitedEdges,
} from '@typebot.io/schemas/features/analytics'
export const computeTotalUsersAtBlock = (
currentBlockId: string,
{
publishedTypebot,
totalVisitedEdges,
totalAnswers,
}: {
publishedTypebot: PublicTypebotV6
totalVisitedEdges: TotalVisitedEdges[]
totalAnswers: TotalAnswers[]
}
): number => {
let totalUsers = 0
const currentGroup = publishedTypebot.groups.find((group) =>
group.blocks.find((block) => block.id === currentBlockId)
)
if (!currentGroup) return 0
const currentBlockIndex = currentGroup.blocks.findIndex(
(block) => block.id === currentBlockId
)
const previousBlocks = currentGroup.blocks.slice(0, currentBlockIndex + 1)
for (const block of previousBlocks.reverse()) {
if (currentBlockId !== block.id && isInputBlock(block))
return totalAnswers.find((t) => t.blockId === block.id)?.total ?? 0
const incomingEdges = publishedTypebot.edges.filter(
(edge) => edge.to.blockId === block.id
)
if (!incomingEdges.length) continue
totalUsers += incomingEdges.reduce(
(acc, incomingEdge) =>
acc +
(totalVisitedEdges.find(
(totalEdge) => totalEdge.edgeId === incomingEdge.id
)?.total ?? 0),
0
)
}
const edgesConnectedToGroup = publishedTypebot.edges.filter(
(edge) =>
edge.to.groupId === currentGroup.id && isNotDefined(edge.to.blockId)
)
totalUsers += edgesConnectedToGroup.reduce(
(acc, connectedEdge) =>
acc +
(totalVisitedEdges.find(
(totalEdge) => totalEdge.edgeId === connectedEdge.id
)?.total ?? 0),
0
)
return totalUsers
}

View File

@@ -0,0 +1,20 @@
import { byId } from '@typebot.io/lib'
import { PublicTypebotV6 } from '@typebot.io/schemas'
import { TotalAnswers } from '@typebot.io/schemas/features/analytics'
export const getTotalAnswersAtBlock = (
currentBlockId: string,
{
publishedTypebot,
totalAnswers,
}: {
publishedTypebot: PublicTypebotV6
totalAnswers: TotalAnswers[]
}
): number => {
const block = publishedTypebot.groups
.flatMap((g) => g.blocks)
.find(byId(currentBlockId))
if (!block) throw new Error(`Block ${currentBlockId} not found`)
return totalAnswers.find((t) => t.blockId === block.id)?.total ?? 0
}