2
0

Add Wait block

Closes #142
This commit is contained in:
Baptiste Arnaud
2023-01-26 18:23:09 +01:00
parent ee864d9729
commit fa9e4b7b67
29 changed files with 621 additions and 313 deletions

View File

@ -0,0 +1 @@
export { executeWait } from './utils/executeWait'

View File

@ -0,0 +1,19 @@
import { parseVariables } from '@/features/variables'
import { LogicState } from '@/types'
import { WaitBlock } from 'models'
export const executeWait = async (
block: WaitBlock,
{ typebot: { variables } }: LogicState
) => {
if (!block.options.secondsToWaitFor) return block.outgoingEdgeId
const parsedSecondsToWaitFor = parseVariables(variables)(
block.options.secondsToWaitFor
)
// @ts-expect-error isNaN can be used with strings
if (isNaN(parsedSecondsToWaitFor)) return block.outgoingEdgeId
await new Promise((resolve) =>
setTimeout(resolve, parseInt(parsedSecondsToWaitFor) * 1000)
)
return block.outgoingEdgeId
}

View File

@ -4,6 +4,7 @@ import { executeCondition } from '@/features/blocks/logic/condition'
import { executeRedirect } from '@/features/blocks/logic/redirect'
import { executeSetVariable } from '@/features/blocks/logic/setVariable'
import { executeTypebotLink } from '@/features/blocks/logic/typebotLink'
import { executeWait } from '@/features/blocks/logic/wait'
import { LinkedTypebot } from '@/providers/TypebotProvider'
import { EdgeId, LogicState } from '@/types'
import { LogicBlock, LogicBlockType } from 'models'
@ -26,5 +27,7 @@ export const executeLogic = async (
return { nextEdgeId: await executeCode(block, context) }
case LogicBlockType.TYPEBOT_LINK:
return executeTypebotLink(block, context)
case LogicBlockType.WAIT:
return { nextEdgeId: await executeWait(block, context) }
}
}

View File

@ -0,0 +1,7 @@
type Props = {
secondsToWaitFor: number
}
export const executeWait = async ({ secondsToWaitFor }: Props) => {
await new Promise((resolve) => setTimeout(resolve, secondsToWaitFor * 1000))
}

View File

@ -2,6 +2,7 @@ import { executeChatwoot } from '@/features/blocks/integrations/chatwoot'
import { executeGoogleAnalyticsBlock } from '@/features/blocks/integrations/googleAnalytics/utils/executeGoogleAnalytics'
import { executeCode } from '@/features/blocks/logic/code'
import { executeRedirect } from '@/features/blocks/logic/redirect'
import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait'
import type { ChatReply } from 'models'
export const executeClientSideAction = async (
@ -19,4 +20,7 @@ export const executeClientSideAction = async (
if ('redirect' in clientSideAction) {
executeRedirect(clientSideAction.redirect)
}
if ('wait' in clientSideAction) {
await executeWait(clientSideAction.wait)
}
}

View File

@ -4,4 +4,5 @@ export enum LogicBlockType {
REDIRECT = 'Redirect',
CODE = 'Code',
TYPEBOT_LINK = 'Typebot link',
WAIT = 'Wait',
}

View File

@ -5,3 +5,4 @@ export * from './logicBlock'
export * from './redirect'
export * from './setVariable'
export * from './typebotLink'
export * from './wait'

View File

@ -4,17 +4,20 @@ import { conditionBlockSchema } from './condition'
import { redirectOptionsSchema, redirectBlockSchema } from './redirect'
import { setVariableOptionsSchema, setVariableBlockSchema } from './setVariable'
import { typebotLinkOptionsSchema, typebotLinkBlockSchema } from './typebotLink'
import { waitBlockSchema, waitOptionsSchema } from './wait'
const logicBlockOptionsSchema = codeOptionsSchema
.or(redirectOptionsSchema)
.or(setVariableOptionsSchema)
.or(typebotLinkOptionsSchema)
.or(waitOptionsSchema)
export const logicBlockSchema = codeBlockSchema
.or(conditionBlockSchema)
.or(redirectBlockSchema)
.or(typebotLinkBlockSchema)
.or(setVariableBlockSchema)
.or(waitBlockSchema)
export type LogicBlock = z.infer<typeof logicBlockSchema>
export type LogicBlockOptions = z.infer<typeof logicBlockOptionsSchema>

View File

@ -0,0 +1,19 @@
import { z } from 'zod'
import { blockBaseSchema } from '../baseSchemas'
import { LogicBlockType } from './enums'
export const waitOptionsSchema = z.object({
secondsToWaitFor: z.string().optional(),
})
export const waitBlockSchema = blockBaseSchema.and(
z.object({
type: z.enum([LogicBlockType.WAIT]),
options: waitOptionsSchema,
})
)
export const defaultWaitOptions: WaitOptions = {}
export type WaitBlock = z.infer<typeof waitBlockSchema>
export type WaitOptions = z.infer<typeof waitOptionsSchema>

View File

@ -184,6 +184,13 @@ const clientSideActionSchema = z
googleAnalytics: googleAnalyticsOptionsSchema,
})
)
.or(
z.object({
wait: z.object({
secondsToWaitFor: z.number(),
}),
})
)
export const chatReplySchema = z.object({
messages: z.array(chatMessageSchema),