Skip to content

Commit

Permalink
🚸 (typebotLink) Make "current" option work like typebot links instead…
Browse files Browse the repository at this point in the history
… of jump
  • Loading branch information
baptisteArno committed Nov 8, 2023
1 parent aceba0a commit 64418df
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const GroupsDropdown = ({
value: group.id,
}))}
onSelect={onGroupIdSelected}
placeholder={'Select a block'}
placeholder={'Select a group'}
/>
)
}
92 changes: 63 additions & 29 deletions packages/bot-engine/blocks/logic/typebotLink/executeTypebotLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,15 @@ import { ExecuteLogicResponse } from '../../../types'
import { createId } from '@paralleldrive/cuid2'
import { isNotDefined } from '@typebot.io/lib/utils'
import { createResultIfNotExist } from '../../../queries/createResultIfNotExist'
import { executeJumpBlock } from '../jump/executeJumpBlock'
import prisma from '@typebot.io/lib/prisma'
import { defaultTypebotLinkOptions } from '@typebot.io/schemas/features/blocks/logic/typebotLink/constants'
import { saveVisitedEdges } from '../../../queries/saveVisitedEdges'

export const executeTypebotLink = async (
state: SessionState,
block: TypebotLinkBlock
): Promise<ExecuteLogicResponse> => {
const logs: ReplyLog[] = []
const typebotId = block.options?.typebotId
if (
typebotId === 'current' ||
typebotId === state.typebotsQueue[0].typebot.id
) {
return executeJumpBlock(state, {
groupId: block.options?.groupId,
})
}
if (!typebotId) {
logs.push({
status: 'error',
Expand All @@ -39,26 +29,31 @@ export const executeTypebotLink = async (
})
return { outgoingEdgeId: block.outgoingEdgeId, logs }
}
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 }
const isLinkingSameTypebot =
typebotId === 'current' || typebotId === state.typebotsQueue[0].typebot.id
let newSessionState = state
let nextGroupId: string | undefined
if (isLinkingSameTypebot) {
newSessionState = await addSameTypebotToState({ state, block })
nextGroupId = block.options?.groupId
} 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) {
logs.push({
status: 'error',
Expand All @@ -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 (
state: SessionState,
block: TypebotLinkBlock,
linkedTypebot: TypebotInSession
): Promise<SessionState> => {
const currentTypebotInQueue = state.typebotsQueue[0]
const isPreview = isNotDefined(currentTypebotInQueue.resultId)

const resumeEdge = createResumeEdgeIfNecessary(state, block)

Expand Down Expand Up @@ -115,6 +148,7 @@ const addLinkedTypebotToState = async (
})
}

const isPreview = isNotDefined(currentTypebotInQueue.resultId)
return {
...state,
typebotsQueue: [
Expand Down

2 comments on commit 64418df

@vercel
Copy link

@vercel vercel bot commented on 64418df Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 64418df Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app

Please sign in to comment.