2
0

🚸 (typebotLink) Make "current" option work like typebot links instead of jump

This commit is contained in:
Baptiste Arnaud
2023-11-08 17:05:02 +01:00
parent aceba0abd0
commit 64418df3a1
2 changed files with 64 additions and 30 deletions

View File

@@ -26,7 +26,7 @@ export const GroupsDropdown = ({
value: group.id, value: group.id,
}))} }))}
onSelect={onGroupIdSelected} onSelect={onGroupIdSelected}
placeholder={'Select a block'} placeholder={'Select a group'}
/> />
) )
} }

View File

@@ -12,10 +12,8 @@ import { ExecuteLogicResponse } from '../../../types'
import { createId } from '@paralleldrive/cuid2' import { createId } from '@paralleldrive/cuid2'
import { isNotDefined } from '@typebot.io/lib/utils' import { isNotDefined } from '@typebot.io/lib/utils'
import { createResultIfNotExist } from '../../../queries/createResultIfNotExist' import { createResultIfNotExist } from '../../../queries/createResultIfNotExist'
import { executeJumpBlock } from '../jump/executeJumpBlock'
import prisma from '@typebot.io/lib/prisma' import prisma from '@typebot.io/lib/prisma'
import { defaultTypebotLinkOptions } from '@typebot.io/schemas/features/blocks/logic/typebotLink/constants' import { defaultTypebotLinkOptions } from '@typebot.io/schemas/features/blocks/logic/typebotLink/constants'
import { saveVisitedEdges } from '../../../queries/saveVisitedEdges'
export const executeTypebotLink = async ( export const executeTypebotLink = async (
state: SessionState, state: SessionState,
@@ -23,14 +21,6 @@ export const executeTypebotLink = async (
): Promise<ExecuteLogicResponse> => { ): Promise<ExecuteLogicResponse> => {
const logs: ReplyLog[] = [] const logs: ReplyLog[] = []
const typebotId = block.options?.typebotId const typebotId = block.options?.typebotId
if (
typebotId === 'current' ||
typebotId === state.typebotsQueue[0].typebot.id
) {
return executeJumpBlock(state, {
groupId: block.options?.groupId,
})
}
if (!typebotId) { if (!typebotId) {
logs.push({ logs.push({
status: 'error', status: 'error',
@@ -39,26 +29,31 @@ export const executeTypebotLink = async (
}) })
return { outgoingEdgeId: block.outgoingEdgeId, logs } return { outgoingEdgeId: block.outgoingEdgeId, logs }
} }
const linkedTypebot = await fetchTypebot(state, typebotId) const isLinkingSameTypebot =
if (!linkedTypebot) { typebotId === 'current' || typebotId === state.typebotsQueue[0].typebot.id
logs.push({ let newSessionState = state
status: 'error', let nextGroupId: string | undefined
description: `Failed to link typebot`, if (isLinkingSameTypebot) {
details: `Typebot with ID ${block.options?.typebotId} not found`, newSessionState = await addSameTypebotToState({ state, block })
}) nextGroupId = block.options?.groupId
return { outgoingEdgeId: block.outgoingEdgeId, logs } } else {
const linkedTypebot = await fetchTypebot(state, typebotId)
if (!linkedTypebot) {
logs.push({
status: 'error',
description: `Failed to link typebot`,
details: `Typebot with ID ${block.options?.typebotId} not found`,
})
return { outgoingEdgeId: block.outgoingEdgeId, logs }
}
newSessionState = await addLinkedTypebotToState(state, block, linkedTypebot)
nextGroupId =
block.options?.groupId ??
linkedTypebot.groups.find((group) =>
group.blocks.some((block) => block.type === 'start')
)?.id
} }
let newSessionState = await addLinkedTypebotToState(
state,
block,
linkedTypebot
)
const nextGroupId =
block.options?.groupId ??
linkedTypebot.groups.find((group) =>
group.blocks.some((block) => block.type === 'start')
)?.id
if (!nextGroupId) { if (!nextGroupId) {
logs.push({ logs.push({
status: 'error', status: 'error',
@@ -78,13 +73,51 @@ export const executeTypebotLink = async (
} }
} }
const addSameTypebotToState = async ({
state,
block,
}: {
state: SessionState
block: TypebotLinkBlock
}) => {
const currentTypebotInQueue = state.typebotsQueue[0]
const resumeEdge = createResumeEdgeIfNecessary(state, block)
const currentTypebotWithResumeEdge = resumeEdge
? {
...currentTypebotInQueue,
typebot: {
...currentTypebotInQueue.typebot,
edges: [...currentTypebotInQueue.typebot.edges, resumeEdge],
},
}
: currentTypebotInQueue
return {
...state,
typebotsQueue: [
{
typebot: {
...currentTypebotInQueue.typebot,
},
resultId: currentTypebotInQueue.resultId,
edgeIdToTriggerWhenDone: block.outgoingEdgeId ?? resumeEdge?.id,
answers: currentTypebotInQueue.answers,
isMergingWithParent: true,
},
currentTypebotWithResumeEdge,
...state.typebotsQueue.slice(1),
],
}
}
const addLinkedTypebotToState = async ( const addLinkedTypebotToState = async (
state: SessionState, state: SessionState,
block: TypebotLinkBlock, block: TypebotLinkBlock,
linkedTypebot: TypebotInSession linkedTypebot: TypebotInSession
): Promise<SessionState> => { ): Promise<SessionState> => {
const currentTypebotInQueue = state.typebotsQueue[0] const currentTypebotInQueue = state.typebotsQueue[0]
const isPreview = isNotDefined(currentTypebotInQueue.resultId)
const resumeEdge = createResumeEdgeIfNecessary(state, block) const resumeEdge = createResumeEdgeIfNecessary(state, block)
@@ -115,6 +148,7 @@ const addLinkedTypebotToState = async (
}) })
} }
const isPreview = isNotDefined(currentTypebotInQueue.resultId)
return { return {
...state, ...state,
typebotsQueue: [ typebotsQueue: [