Skip to content

Commit

Permalink
fix(engine): 🐛 Nested typebots webhhok exec
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 19, 2022
1 parent 240cbee commit 9fbe1cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/bot-engine/src/components/ChatBlock/ChatBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const ChatBlock = ({
createEdge,
setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue,
currentTypebotId,
})
nextEdgeId ? onBlockEnd(nextEdgeId, linkedTypebot) : displayNextStep()
}
Expand Down
6 changes: 3 additions & 3 deletions packages/bot-engine/src/components/ConversationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ConversationContainer = ({
const {
typebot,
updateVariableValue,
linkedBotEdgeIdsQueue,
linkedBotQueue,
popEdgeIdFromLinkedTypebotQueue,
} = useTypebot()
const { document: frameDocument } = useFrame()
Expand All @@ -42,8 +42,8 @@ export const ConversationContainer = ({
const currentTypebot = updatedTypebot ?? typebot
const nextEdge = currentTypebot.edges.find(byId(edgeId))
if (!nextEdge) {
if (linkedBotEdgeIdsQueue.length > 0) {
const nextEdgeId = linkedBotEdgeIdsQueue[0]
if (linkedBotQueue.length > 0) {
const nextEdgeId = linkedBotQueue[0].edgeId
popEdgeIdFromLinkedTypebotQueue()
displayNextBlock(nextEdgeId)
}
Expand Down
31 changes: 21 additions & 10 deletions packages/bot-engine/src/contexts/TypebotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ export type LinkedTypebot = Pick<
PublicTypebot | Typebot,
'id' | 'blocks' | 'variables' | 'edges'
>

export type LinkedTypebotQueue = {
typebotId: string
edgeId: string
}[]

const typebotContext = createContext<{
currentTypebotId: string
typebot: PublicTypebot
linkedTypebots: LinkedTypebot[]
apiHost: string
isPreview: boolean
linkedBotEdgeIdsQueue: string[]
linkedBotQueue: LinkedTypebotQueue
setCurrentTypebotId: (id: string) => void
updateVariableValue: (variableId: string, value: string) => void
createEdge: (edge: Edge) => void
injectLinkedTypebot: (typebot: Typebot | PublicTypebot) => LinkedTypebot
popEdgeIdFromLinkedTypebotQueue: () => void
pushEdgeIdInLinkedTypebotQueue: (edgeId: string) => void
pushEdgeIdInLinkedTypebotQueue: (bot: {
typebotId: string
edgeId: string
}) => void
onNewLog: (log: Omit<Log, 'id' | 'createdAt' | 'resultId'>) => void
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
Expand All @@ -46,9 +55,7 @@ export const TypebotContext = ({
const [localTypebot, setLocalTypebot] = useState<PublicTypebot>(typebot)
const [linkedTypebots, setLinkedTypebots] = useState<LinkedTypebot[]>([])
const [currentTypebotId, setCurrentTypebotId] = useState(typebot.typebotId)
const [linkedBotEdgeIdsQueue, setLinkedBotEdgeIdsQueue] = useState<string[]>(
[]
)
const [linkedBotQueue, setLinkedBotQueue] = useState<LinkedTypebotQueue>([])

useEffect(() => {
setLocalTypebot((localTypebot) => ({
Expand Down Expand Up @@ -93,11 +100,15 @@ export const TypebotContext = ({
return typebotToInject
}

const pushEdgeIdInLinkedTypebotQueue = (edgeId: string) =>
setLinkedBotEdgeIdsQueue((queue) => [...queue, edgeId])
const pushEdgeIdInLinkedTypebotQueue = (bot: {
typebotId: string
edgeId: string
}) => setLinkedBotQueue((queue) => [...queue, bot])

const popEdgeIdFromLinkedTypebotQueue = () =>
setLinkedBotEdgeIdsQueue((queue) => queue.slice(1))
const popEdgeIdFromLinkedTypebotQueue = () => {
setLinkedBotQueue((queue) => queue.slice(1))
const typebot = setCurrentTypebotId(linkedBotQueue[0].typebotId)
}

return (
<typebotContext.Provider
Expand All @@ -110,7 +121,7 @@ export const TypebotContext = ({
createEdge,
injectLinkedTypebot,
onNewLog,
linkedBotEdgeIdsQueue,
linkedBotQueue,
pushEdgeIdInLinkedTypebotQueue,
popEdgeIdFromLinkedTypebotQueue,
currentTypebotId,
Expand Down
13 changes: 11 additions & 2 deletions packages/bot-engine/src/services/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ type LogicContext = {
apiHost: string
typebot: PublicTypebot
linkedTypebots: LinkedTypebot[]
pushEdgeIdInLinkedTypebotQueue: (edgeId: EdgeId) => void
currentTypebotId: string
pushEdgeIdInLinkedTypebotQueue: (bot: {
edgeId: string
typebotId: string
}) => void
setCurrentTypebotId: (id: string) => void
updateVariableValue: (variableId: string, value: string) => void
updateVariables: (variables: VariableWithValue[]) => void
Expand Down Expand Up @@ -157,6 +161,7 @@ const executeTypebotLink = async (
createEdge,
setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue,
currentTypebotId,
} = context
const linkedTypebot = (
step.options.typebotId === 'current'
Expand All @@ -172,7 +177,11 @@ const executeTypebotLink = async (
})
return { nextEdgeId: step.outgoingEdgeId }
}
if (step.outgoingEdgeId) pushEdgeIdInLinkedTypebotQueue(step.outgoingEdgeId)
if (step.outgoingEdgeId)
pushEdgeIdInLinkedTypebotQueue({
edgeId: step.outgoingEdgeId,
typebotId: currentTypebotId,
})
setCurrentTypebotId(
'typebotId' in linkedTypebot ? linkedTypebot.typebotId : linkedTypebot.id
)
Expand Down

4 comments on commit 9fbe1cc

@vercel
Copy link

@vercel vercel bot commented on 9fbe1cc Apr 19, 2022

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 9fbe1cc Apr 19, 2022

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 9fbe1cc Apr 19, 2022

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-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.