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, createEdge,
setCurrentTypebotId, setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue, pushEdgeIdInLinkedTypebotQueue,
currentTypebotId,
}) })
nextEdgeId ? onBlockEnd(nextEdgeId, linkedTypebot) : displayNextStep() nextEdgeId ? onBlockEnd(nextEdgeId, linkedTypebot) : displayNextStep()
} }

View File

@ -24,7 +24,7 @@ export const ConversationContainer = ({
const { const {
typebot, typebot,
updateVariableValue, updateVariableValue,
linkedBotEdgeIdsQueue, linkedBotQueue,
popEdgeIdFromLinkedTypebotQueue, popEdgeIdFromLinkedTypebotQueue,
} = useTypebot() } = useTypebot()
const { document: frameDocument } = useFrame() const { document: frameDocument } = useFrame()
@ -42,8 +42,8 @@ export const ConversationContainer = ({
const currentTypebot = updatedTypebot ?? typebot const currentTypebot = updatedTypebot ?? typebot
const nextEdge = currentTypebot.edges.find(byId(edgeId)) const nextEdge = currentTypebot.edges.find(byId(edgeId))
if (!nextEdge) { if (!nextEdge) {
if (linkedBotEdgeIdsQueue.length > 0) { if (linkedBotQueue.length > 0) {
const nextEdgeId = linkedBotEdgeIdsQueue[0] const nextEdgeId = linkedBotQueue[0].edgeId
popEdgeIdFromLinkedTypebotQueue() popEdgeIdFromLinkedTypebotQueue()
displayNextBlock(nextEdgeId) displayNextBlock(nextEdgeId)
} }

View File

@ -12,19 +12,28 @@ export type LinkedTypebot = Pick<
PublicTypebot | Typebot, PublicTypebot | Typebot,
'id' | 'blocks' | 'variables' | 'edges' 'id' | 'blocks' | 'variables' | 'edges'
> >
export type LinkedTypebotQueue = {
typebotId: string
edgeId: string
}[]
const typebotContext = createContext<{ const typebotContext = createContext<{
currentTypebotId: string currentTypebotId: string
typebot: PublicTypebot typebot: PublicTypebot
linkedTypebots: LinkedTypebot[] linkedTypebots: LinkedTypebot[]
apiHost: string apiHost: string
isPreview: boolean isPreview: boolean
linkedBotEdgeIdsQueue: string[] linkedBotQueue: LinkedTypebotQueue
setCurrentTypebotId: (id: string) => void setCurrentTypebotId: (id: string) => void
updateVariableValue: (variableId: string, value: string) => void updateVariableValue: (variableId: string, value: string) => void
createEdge: (edge: Edge) => void createEdge: (edge: Edge) => void
injectLinkedTypebot: (typebot: Typebot | PublicTypebot) => LinkedTypebot injectLinkedTypebot: (typebot: Typebot | PublicTypebot) => LinkedTypebot
popEdgeIdFromLinkedTypebotQueue: () => void popEdgeIdFromLinkedTypebotQueue: () => void
pushEdgeIdInLinkedTypebotQueue: (edgeId: string) => void pushEdgeIdInLinkedTypebotQueue: (bot: {
typebotId: string
edgeId: string
}) => void
onNewLog: (log: Omit<Log, 'id' | 'createdAt' | 'resultId'>) => void onNewLog: (log: Omit<Log, 'id' | 'createdAt' | 'resultId'>) => void
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore //@ts-ignore
@ -46,9 +55,7 @@ export const TypebotContext = ({
const [localTypebot, setLocalTypebot] = useState<PublicTypebot>(typebot) const [localTypebot, setLocalTypebot] = useState<PublicTypebot>(typebot)
const [linkedTypebots, setLinkedTypebots] = useState<LinkedTypebot[]>([]) const [linkedTypebots, setLinkedTypebots] = useState<LinkedTypebot[]>([])
const [currentTypebotId, setCurrentTypebotId] = useState(typebot.typebotId) const [currentTypebotId, setCurrentTypebotId] = useState(typebot.typebotId)
const [linkedBotEdgeIdsQueue, setLinkedBotEdgeIdsQueue] = useState<string[]>( const [linkedBotQueue, setLinkedBotQueue] = useState<LinkedTypebotQueue>([])
[]
)
useEffect(() => { useEffect(() => {
setLocalTypebot((localTypebot) => ({ setLocalTypebot((localTypebot) => ({
@ -93,11 +100,15 @@ export const TypebotContext = ({
return typebotToInject return typebotToInject
} }
const pushEdgeIdInLinkedTypebotQueue = (edgeId: string) => const pushEdgeIdInLinkedTypebotQueue = (bot: {
setLinkedBotEdgeIdsQueue((queue) => [...queue, edgeId]) typebotId: string
edgeId: string
}) => setLinkedBotQueue((queue) => [...queue, bot])
const popEdgeIdFromLinkedTypebotQueue = () => const popEdgeIdFromLinkedTypebotQueue = () => {
setLinkedBotEdgeIdsQueue((queue) => queue.slice(1)) setLinkedBotQueue((queue) => queue.slice(1))
const typebot = setCurrentTypebotId(linkedBotQueue[0].typebotId)
}
return ( return (
<typebotContext.Provider <typebotContext.Provider
@ -110,7 +121,7 @@ export const TypebotContext = ({
createEdge, createEdge,
injectLinkedTypebot, injectLinkedTypebot,
onNewLog, onNewLog,
linkedBotEdgeIdsQueue, linkedBotQueue,
pushEdgeIdInLinkedTypebotQueue, pushEdgeIdInLinkedTypebotQueue,
popEdgeIdFromLinkedTypebotQueue, popEdgeIdFromLinkedTypebotQueue,
currentTypebotId, currentTypebotId,

View File

@ -28,7 +28,11 @@ type LogicContext = {
apiHost: string apiHost: string
typebot: PublicTypebot typebot: PublicTypebot
linkedTypebots: LinkedTypebot[] linkedTypebots: LinkedTypebot[]
pushEdgeIdInLinkedTypebotQueue: (edgeId: EdgeId) => void currentTypebotId: string
pushEdgeIdInLinkedTypebotQueue: (bot: {
edgeId: string
typebotId: string
}) => void
setCurrentTypebotId: (id: string) => void setCurrentTypebotId: (id: string) => void
updateVariableValue: (variableId: string, value: string) => void updateVariableValue: (variableId: string, value: string) => void
updateVariables: (variables: VariableWithValue[]) => void updateVariables: (variables: VariableWithValue[]) => void
@ -157,6 +161,7 @@ const executeTypebotLink = async (
createEdge, createEdge,
setCurrentTypebotId, setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue, pushEdgeIdInLinkedTypebotQueue,
currentTypebotId,
} = context } = context
const linkedTypebot = ( const linkedTypebot = (
step.options.typebotId === 'current' step.options.typebotId === 'current'
@ -172,7 +177,11 @@ const executeTypebotLink = async (
}) })
return { nextEdgeId: step.outgoingEdgeId } return { nextEdgeId: step.outgoingEdgeId }
} }
if (step.outgoingEdgeId) pushEdgeIdInLinkedTypebotQueue(step.outgoingEdgeId) if (step.outgoingEdgeId)
pushEdgeIdInLinkedTypebotQueue({
edgeId: step.outgoingEdgeId,
typebotId: currentTypebotId,
})
setCurrentTypebotId( setCurrentTypebotId(
'typebotId' in linkedTypebot ? linkedTypebot.typebotId : linkedTypebot.id 'typebotId' in linkedTypebot ? linkedTypebot.typebotId : linkedTypebot.id
) )