Files
bot/packages/deprecated/bot-engine/src/utils/inputs.ts
Baptiste Arnaud 35300eaf34 ♻️ Introduce typebot v6 with events (#1013)
Closes #885
2023-11-08 15:34:16 +01:00

65 lines
1.8 KiB
TypeScript

import { validateEmail } from '@/features/blocks/inputs/email'
import { validatePhoneNumber } from '@/features/blocks/inputs/phone'
import { validateUrl } from '@/features/blocks/inputs/url'
import { parseVariables } from '@/features/variables'
import {
BubbleBlock,
Edge,
EmailInputBlock,
PhoneNumberInputBlock,
Block,
UrlInputBlock,
Variable,
} from '@typebot.io/schemas'
import { isDefined, isInputBlock } from '@typebot.io/lib'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/constants'
export const isInputValid = (
inputValue: string,
type: InputBlockType
): boolean => {
switch (type) {
case InputBlockType.EMAIL:
return validateEmail(inputValue)
case InputBlockType.PHONE:
return validatePhoneNumber(inputValue)
case InputBlockType.URL:
return validateUrl(inputValue)
}
return true
}
export const blockCanBeRetried = (
block: Block
): block is EmailInputBlock | UrlInputBlock | PhoneNumberInputBlock =>
isInputBlock(block) &&
isDefined(block.options) &&
'retryMessageContent' in block.options
export const parseRetryBlock = (
block: EmailInputBlock | UrlInputBlock | PhoneNumberInputBlock,
groupId: string,
variables: Variable[],
createEdge: (edge: Edge) => void
): BubbleBlock => {
const content = parseVariables(variables)(block.options?.retryMessageContent)
const newBlockId = block.id + Math.random() * 1000
const newEdge: Edge = {
id: (Math.random() * 1000).toString(),
from: { blockId: newBlockId },
to: { groupId, blockId: block.id },
}
createEdge(newEdge)
return {
id: newBlockId,
type: BubbleBlockType.TEXT,
content: {
html: `<div>${content}</div>`,
richText: [],
plainText: content,
},
outgoingEdgeId: newEdge.id,
}
}