2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteLogicResponse } from '../../../types'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { SessionState, WaitBlock } from '@typebot.io/schemas'
|
2023-12-13 10:22:02 +01:00
|
|
|
import { parseVariables } from '@typebot.io/variables/parseVariables'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { isNotDefined } from '@typebot.io/lib'
|
2023-01-26 18:23:09 +01:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
export const executeWait = (
|
2023-08-24 07:48:30 +02:00
|
|
|
state: SessionState,
|
2023-05-26 09:20:22 +02:00
|
|
|
block: WaitBlock
|
2023-07-18 14:31:20 +02:00
|
|
|
): ExecuteLogicResponse => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { variables } = state.typebotsQueue[0].typebot
|
2023-11-08 15:34:16 +01:00
|
|
|
if (!block.options?.secondsToWaitFor)
|
|
|
|
|
return { outgoingEdgeId: block.outgoingEdgeId }
|
|
|
|
|
|
2024-03-15 14:31:35 +01:00
|
|
|
const parsedSecondsToWaitFor = safeParseFloat(
|
2023-01-27 10:54:59 +01:00
|
|
|
parseVariables(variables)(block.options.secondsToWaitFor)
|
2023-01-26 18:23:09 +01:00
|
|
|
)
|
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
if (isNotDefined(parsedSecondsToWaitFor))
|
|
|
|
|
return { outgoingEdgeId: block.outgoingEdgeId }
|
|
|
|
|
|
2023-01-26 18:23:09 +01:00
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
2023-09-27 16:45:14 +02:00
|
|
|
clientSideActions:
|
2023-11-08 15:34:16 +01:00
|
|
|
parsedSecondsToWaitFor || block.options?.shouldPause
|
2023-09-27 16:45:14 +02:00
|
|
|
? [
|
|
|
|
|
{
|
2023-12-22 09:13:53 +01:00
|
|
|
type: 'wait',
|
2023-09-27 16:45:14 +02:00
|
|
|
wait: { secondsToWaitFor: parsedSecondsToWaitFor ?? 0 },
|
|
|
|
|
expectsDedicatedReply: block.options.shouldPause,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
: undefined,
|
2023-01-26 18:23:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-27 10:54:59 +01:00
|
|
|
|
2024-03-15 14:31:35 +01:00
|
|
|
const safeParseFloat = (value: string) => {
|
|
|
|
|
const parsedValue = parseFloat(value)
|
2023-01-27 10:54:59 +01:00
|
|
|
return isNaN(parsedValue) ? undefined : parsedValue
|
|
|
|
|
}
|