From e50ce645eb226406b30532b229fcfae56a8bdc96 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 14 Apr 2022 16:17:28 -0700 Subject: [PATCH] =?UTF-8?q?fix(logic):=20=E2=9A=A1=EF=B8=8F=20Make=20linke?= =?UTF-8?q?d=20bot=20edge=20id=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/FolderContent/TypebotButton.tsx | 2 +- .../src/components/ChatBlock/ChatBlock.tsx | 2 ++ .../src/components/ConversationContainer.tsx | 16 ++++++++++++++-- .../bot-engine/src/contexts/TypebotContext.tsx | 15 +++++++++++++++ packages/bot-engine/src/services/logic.ts | 12 ++++++++++-- 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/apps/builder/components/dashboard/FolderContent/TypebotButton.tsx b/apps/builder/components/dashboard/FolderContent/TypebotButton.tsx index fe0f1b527a..7d533edaef 100644 --- a/apps/builder/components/dashboard/FolderContent/TypebotButton.tsx +++ b/apps/builder/components/dashboard/FolderContent/TypebotButton.tsx @@ -159,7 +159,7 @@ export const TypebotButton = ({ > {} - {typebot.name} + {typebot.name} {!isReadOnly && ( ([]) @@ -113,6 +114,7 @@ export const ChatBlock = ({ onNewLog, createEdge, setCurrentTypebotId, + pushEdgeIdInLinkedTypebotQueue, }) nextEdgeId ? onBlockEnd(nextEdgeId, linkedTypebot) : displayNextStep() } diff --git a/packages/bot-engine/src/components/ConversationContainer.tsx b/packages/bot-engine/src/components/ConversationContainer.tsx index 843215189a..b7ceb222e0 100644 --- a/packages/bot-engine/src/components/ConversationContainer.tsx +++ b/packages/bot-engine/src/components/ConversationContainer.tsx @@ -21,7 +21,12 @@ export const ConversationContainer = ({ onNewBlockVisible, onCompleted, }: Props) => { - const { typebot, updateVariableValue } = useTypebot() + const { + typebot, + updateVariableValue, + linkedBotEdgeIdsQueue, + popEdgeIdFromLinkedTypebotQueue, + } = useTypebot() const { document: frameDocument } = useFrame() const [displayedBlocks, setDisplayedBlocks] = useState< { block: Block; startStepIndex: number }[] @@ -36,7 +41,14 @@ export const ConversationContainer = ({ ) => { const currentTypebot = updatedTypebot ?? typebot const nextEdge = currentTypebot.edges.find(byId(edgeId)) - if (!nextEdge) return onCompleted() + if (!nextEdge) { + if (linkedBotEdgeIdsQueue.length > 0) { + const nextEdgeId = linkedBotEdgeIdsQueue[0] + popEdgeIdFromLinkedTypebotQueue() + displayNextBlock(nextEdgeId) + } + return onCompleted() + } const nextBlock = currentTypebot.blocks.find(byId(nextEdge.to.blockId)) if (!nextBlock) return onCompleted() const startStepIndex = nextEdge.to.stepId diff --git a/packages/bot-engine/src/contexts/TypebotContext.tsx b/packages/bot-engine/src/contexts/TypebotContext.tsx index e5dd8c1457..d82ef39c18 100644 --- a/packages/bot-engine/src/contexts/TypebotContext.tsx +++ b/packages/bot-engine/src/contexts/TypebotContext.tsx @@ -18,10 +18,13 @@ const typebotContext = createContext<{ linkedTypebots: LinkedTypebot[] apiHost: string isPreview: boolean + linkedBotEdgeIdsQueue: string[] 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 onNewLog: (log: Omit) => void // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore @@ -43,6 +46,9 @@ export const TypebotContext = ({ const [localTypebot, setLocalTypebot] = useState(typebot) const [linkedTypebots, setLinkedTypebots] = useState([]) const [currentTypebotId, setCurrentTypebotId] = useState(typebot.typebotId) + const [linkedBotEdgeIdsQueue, setLinkedBotEdgeIdsQueue] = useState( + [] + ) useEffect(() => { setLocalTypebot((localTypebot) => ({ @@ -87,6 +93,12 @@ export const TypebotContext = ({ return typebotToInject } + const pushEdgeIdInLinkedTypebotQueue = (edgeId: string) => + setLinkedBotEdgeIdsQueue((queue) => [...queue, edgeId]) + + const popEdgeIdFromLinkedTypebotQueue = () => + setLinkedBotEdgeIdsQueue((queue) => queue.slice(1)) + return ( void setCurrentTypebotId: (id: string) => void updateVariableValue: (variableId: string, value: string) => void updateVariables: (variables: VariableWithValue[]) => void @@ -149,8 +150,14 @@ const executeTypebotLink = async ( nextEdgeId?: EdgeId linkedTypebot?: PublicTypebot | LinkedTypebot }> => { - const { typebot, linkedTypebots, onNewLog, createEdge, setCurrentTypebotId } = - context + const { + typebot, + linkedTypebots, + onNewLog, + createEdge, + setCurrentTypebotId, + pushEdgeIdInLinkedTypebotQueue, + } = context const linkedTypebot = ( step.options.typebotId === 'current' ? typebot @@ -165,6 +172,7 @@ const executeTypebotLink = async ( }) return { nextEdgeId: step.outgoingEdgeId } } + if (step.outgoingEdgeId) pushEdgeIdInLinkedTypebotQueue(step.outgoingEdgeId) setCurrentTypebotId( 'typebotId' in linkedTypebot ? linkedTypebot.typebotId : linkedTypebot.id )