2
0

fix(engine): 🐛 Nested typebots webhhok exec

This commit is contained in:
Baptiste Arnaud
2022-04-19 10:09:38 -07:00
parent 240cbeed62
commit 9fbe1cc34c
4 changed files with 36 additions and 15 deletions

View File

@ -115,6 +115,7 @@ export const ChatBlock = ({
createEdge,
setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue,
currentTypebotId,
})
nextEdgeId ? onBlockEnd(nextEdgeId, linkedTypebot) : displayNextStep()
}

View File

@ -24,7 +24,7 @@ export const ConversationContainer = ({
const {
typebot,
updateVariableValue,
linkedBotEdgeIdsQueue,
linkedBotQueue,
popEdgeIdFromLinkedTypebotQueue,
} = useTypebot()
const { document: frameDocument } = useFrame()
@ -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)
}

View File

@ -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
@ -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) => ({
@ -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
@ -110,7 +121,7 @@ export const TypebotContext = ({
createEdge,
injectLinkedTypebot,
onNewLog,
linkedBotEdgeIdsQueue,
linkedBotQueue,
pushEdgeIdInLinkedTypebotQueue,
popEdgeIdFromLinkedTypebotQueue,
currentTypebotId,

View File

@ -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
@ -157,6 +161,7 @@ const executeTypebotLink = async (
createEdge,
setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue,
currentTypebotId,
} = context
const linkedTypebot = (
step.options.typebotId === 'current'
@ -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
)