Skip to content

Commit

Permalink
fix(logic): ⚡️ Make linked bot edge id work
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 14, 2022
1 parent 1a59ce2 commit e50ce64
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const TypebotButton = ({
>
{<TypebotIcon icon={typebot.icon} boxSize={'35px'} />}
</Flex>
<Text>{typebot.name}</Text>
<Text textAlign="center">{typebot.name}</Text>
</VStack>
{!isReadOnly && (
<ConfirmModal
Expand Down
2 changes: 2 additions & 0 deletions packages/bot-engine/src/components/ChatBlock/ChatBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const ChatBlock = ({
injectLinkedTypebot,
linkedTypebots,
setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue,
} = useTypebot()
const { resultValues, updateVariables } = useAnswers()
const [processedSteps, setProcessedSteps] = useState<Step[]>([])
Expand Down Expand Up @@ -113,6 +114,7 @@ export const ChatBlock = ({
onNewLog,
createEdge,
setCurrentTypebotId,
pushEdgeIdInLinkedTypebotQueue,
})
nextEdgeId ? onBlockEnd(nextEdgeId, linkedTypebot) : displayNextStep()
}
Expand Down
16 changes: 14 additions & 2 deletions packages/bot-engine/src/components/ConversationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }[]
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions packages/bot-engine/src/contexts/TypebotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Log, 'id' | 'createdAt' | 'resultId'>) => void
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
Expand All @@ -43,6 +46,9 @@ 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[]>(
[]
)

useEffect(() => {
setLocalTypebot((localTypebot) => ({
Expand Down Expand Up @@ -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 (
<typebotContext.Provider
value={{
Expand All @@ -98,6 +110,9 @@ export const TypebotContext = ({
createEdge,
injectLinkedTypebot,
onNewLog,
linkedBotEdgeIdsQueue,
pushEdgeIdInLinkedTypebotQueue,
popEdgeIdFromLinkedTypebotQueue,
currentTypebotId,
setCurrentTypebotId,
}}
Expand Down
12 changes: 10 additions & 2 deletions packages/bot-engine/src/services/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type LogicContext = {
apiHost: string
typebot: PublicTypebot
linkedTypebots: LinkedTypebot[]
pushEdgeIdInLinkedTypebotQueue: (edgeId: EdgeId) => void
setCurrentTypebotId: (id: string) => void
updateVariableValue: (variableId: string, value: string) => void
updateVariables: (variables: VariableWithValue[]) => void
Expand Down Expand Up @@ -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
Expand All @@ -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
)
Expand Down

4 comments on commit e50ce64

@vercel
Copy link

@vercel vercel bot commented on e50ce64 Apr 14, 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 e50ce64 Apr 14, 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

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

@vercel
Copy link

@vercel vercel bot commented on e50ce64 Apr 14, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.