Skip to content

Commit

Permalink
chore(viewer): 🛂 Dynamically parse the viewer api host
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 10, 2022
1 parent 0338aca commit cfbf3d4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
19 changes: 11 additions & 8 deletions packages/bot-engine/src/components/ChatBlock/ChatBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ChatBlock = ({
onScroll,
onBlockEnd,
}: ChatBlockProps) => {
const { typebot, updateVariableValue, createEdge } = useTypebot()
const { typebot, updateVariableValue, createEdge, apiHost } = useTypebot()
const [displayedSteps, setDisplayedSteps] = useState<PublicStep[]>([])

const currentStepIndex = displayedSteps.length - 1
Expand Down Expand Up @@ -60,13 +60,16 @@ export const ChatBlock = ({
nextEdgeId ? onBlockEnd(nextEdgeId) : displayNextStep()
}
if (isIntegrationStep(currentStep)) {
const nextEdgeId = await executeIntegration(
typebot.typebotId,
currentStep,
typebot.variables,
{ blockIndex, stepIndex: currentStepIndex },
updateVariableValue
)
const nextEdgeId = await executeIntegration({
step: currentStep,
context: {
apiHost,
typebotId: typebot.id,
indices: { blockIndex, stepIndex: currentStepIndex },
variables: typebot.variables,
updateVariableValue,
},
})
nextEdgeId ? onBlockEnd(nextEdgeId) : displayNextStep()
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/bot-engine/src/components/TypebotViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import { Answer, BackgroundType, Edge, PublicTypebot } from 'models'

export type TypebotViewerProps = {
typebot: PublicTypebot
apiHost?: string
onNewBlockVisible?: (edge: Edge) => void
onNewAnswer?: (answer: Answer) => void
onCompleted?: () => void
}
export const TypebotViewer = ({
typebot,
apiHost = process.env.NEXT_PUBLIC_VIEWER_HOST,
onNewBlockVisible,
onNewAnswer,
onCompleted,
Expand All @@ -41,6 +43,8 @@ export const TypebotViewer = ({
if (onCompleted) onCompleted()
}

if (!apiHost)
return <p>process.env.NEXT_PUBLIC_VIEWER_HOST is missing in env</p>
return (
<Frame
id="typebot-iframe"
Expand All @@ -62,7 +66,7 @@ export const TypebotViewer = ({
}:wght@300;400;600&display=swap');`,
}}
/>
<TypebotContext typebot={typebot}>
<TypebotContext typebot={typebot} apiHost={apiHost}>
<AnswersContext>
<div
className="flex text-base overflow-hidden bg-cover h-screen w-screen flex-col items-center typebot-container"
Expand Down
4 changes: 4 additions & 0 deletions packages/bot-engine/src/contexts/TypebotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { createContext, ReactNode, useContext, useState } from 'react'

const typebotContext = createContext<{
typebot: PublicTypebot
apiHost: string
updateVariableValue: (variableId: string, value: string) => void
createEdge: (edge: Edge) => void
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -12,9 +13,11 @@ const typebotContext = createContext<{
export const TypebotContext = ({
children,
typebot,
apiHost,
}: {
children: ReactNode
typebot: PublicTypebot
apiHost: string
}) => {
const [localTypebot, setLocalTypebot] = useState<PublicTypebot>(typebot)

Expand All @@ -38,6 +41,7 @@ export const TypebotContext = ({
<typebotContext.Provider
value={{
typebot: localTypebot,
apiHost,
updateVariableValue,
createEdge,
}}
Expand Down
77 changes: 41 additions & 36 deletions packages/bot-engine/src/services/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,35 @@ import { parseVariables, parseVariablesInObject } from './variable'
const safeEval = eval

type Indices = { blockIndex: number; stepIndex: number }
export const executeIntegration = (
typebotId: string,
step: IntegrationStep,
variables: Variable[],
indices: Indices,
type IntegrationContext = {
apiHost: string
typebotId: string
indices: Indices
variables: Variable[]
updateVariableValue: (variableId: string, value: string) => void
): Promise<string | undefined> => {
}
export const executeIntegration = ({
step,
context,
}: {
step: IntegrationStep
context: IntegrationContext
}): Promise<string | undefined> => {
switch (step.type) {
case IntegrationStepType.GOOGLE_SHEETS:
return executeGoogleSheetIntegration(step, variables, updateVariableValue)
return executeGoogleSheetIntegration(step, context)
case IntegrationStepType.GOOGLE_ANALYTICS:
return executeGoogleAnalyticsIntegration(step, variables)
return executeGoogleAnalyticsIntegration(step, context)
case IntegrationStepType.WEBHOOK:
return executeWebhook(
typebotId,
step,
variables,
indices,
updateVariableValue
)
return executeWebhook(step, context)
case IntegrationStepType.EMAIL:
return sendEmail(step, variables)
return sendEmail(step, context)
}
}

export const executeGoogleAnalyticsIntegration = async (
step: GoogleAnalyticsStep,
variables: Variable[]
{ variables }: IntegrationContext
) => {
if (!step.options?.trackingId) return step.outgoingEdgeId
const { default: initGoogleAnalytics } = await import('../../lib/gtag')
Expand All @@ -58,31 +59,30 @@ export const executeGoogleAnalyticsIntegration = async (

const executeGoogleSheetIntegration = async (
step: GoogleSheetsStep,
variables: Variable[],
updateVariableValue: (variableId: string, value: string) => void
context: IntegrationContext
) => {
if (!('action' in step.options)) return step.outgoingEdgeId
switch (step.options.action) {
case GoogleSheetsAction.INSERT_ROW:
await insertRowInGoogleSheets(step.options, variables)
await insertRowInGoogleSheets(step.options, context)
break
case GoogleSheetsAction.UPDATE_ROW:
await updateRowInGoogleSheets(step.options, variables)
await updateRowInGoogleSheets(step.options, context)
break
case GoogleSheetsAction.GET:
await getRowFromGoogleSheets(step.options, variables, updateVariableValue)
await getRowFromGoogleSheets(step.options, context)
break
}
return step.outgoingEdgeId
}

const insertRowInGoogleSheets = async (
options: GoogleSheetsInsertRowOptions,
variables: Variable[]
{ variables, apiHost }: IntegrationContext
) => {
if (!options.cellsToInsert) return
return sendRequest({
url: `http://localhost:3001/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
method: 'POST',
body: {
credentialsId: options.credentialsId,
Expand All @@ -93,11 +93,11 @@ const insertRowInGoogleSheets = async (

const updateRowInGoogleSheets = async (
options: GoogleSheetsUpdateRowOptions,
variables: Variable[]
{ variables, apiHost }: IntegrationContext
) => {
if (!options.cellsToUpsert || !options.referenceCell) return
return sendRequest({
url: `http://localhost:3001/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
method: 'PATCH',
body: {
credentialsId: options.credentialsId,
Expand All @@ -112,8 +112,7 @@ const updateRowInGoogleSheets = async (

const getRowFromGoogleSheets = async (
options: GoogleSheetsGetOptions,
variables: Variable[],
updateVariableValue: (variableId: string, value: string) => void
{ variables, updateVariableValue, apiHost }: IntegrationContext
) => {
if (!options.referenceCell || !options.cellsToExtract) return
const queryParams = stringify(
Expand All @@ -128,7 +127,7 @@ const getRowFromGoogleSheets = async (
{ indices: false }
)
const { data } = await sendRequest<{ [key: string]: string }>({
url: `http://localhost:3001/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${queryParams}`,
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${queryParams}`,
method: 'GET',
})
if (!data) return
Expand All @@ -150,16 +149,19 @@ const parseCellValues = (
}, {})

const executeWebhook = async (
typebotId: string,
step: WebhookStep,
variables: Variable[],
indices: Indices,
updateVariableValue: (variableId: string, value: string) => void
{
indices,
variables,
updateVariableValue,
typebotId,
apiHost,
}: IntegrationContext
) => {
if (!step.webhook) return step.outgoingEdgeId
const { blockIndex, stepIndex } = indices
const { data, error } = await sendRequest({
url: `http://localhost:3000/api/typebots/${typebotId}/blocks/${blockIndex}/steps/${stepIndex}/executeWebhook`,
url: `${apiHost}/api/typebots/${typebotId}/blocks/${blockIndex}/steps/${stepIndex}/executeWebhook`,
method: 'POST',
body: {
variables,
Expand All @@ -173,10 +175,13 @@ const executeWebhook = async (
})
}

const sendEmail = async (step: SendEmailStep, variables: Variable[]) => {
const sendEmail = async (
step: SendEmailStep,
{ variables, apiHost }: IntegrationContext
) => {
const { options } = step
const { error } = await sendRequest({
url: `http://localhost:3001/api/integrations/email`,
url: `${apiHost}/api/integrations/email`,
method: 'POST',
body: {
credentialsId: options.credentialsId,
Expand Down

2 comments on commit cfbf3d4

@vercel
Copy link

@vercel vercel bot commented on cfbf3d4 Feb 10, 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:

viewer-v2 – ./apps/viewer

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

@vercel
Copy link

@vercel vercel bot commented on cfbf3d4 Feb 10, 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-git-main-typebot-io.vercel.app
next.typebot.io
builder-v2-typebot-io.vercel.app

Please sign in to comment.