2023-11-08 15:34:16 +01:00
|
|
|
import { Settings } from '@typebot.io/schemas/features/typebot/settings'
|
|
|
|
import { defaultSettings } from '@typebot.io/schemas/features/typebot/settings/constants'
|
2022-12-22 17:02:34 +01:00
|
|
|
|
2023-08-29 10:01:28 +02:00
|
|
|
type Props = {
|
|
|
|
bubbleContent: string
|
2023-11-08 15:34:16 +01:00
|
|
|
typingSettings?: Settings['typingEmulation']
|
2023-08-29 10:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const computeTypingDuration = ({
|
|
|
|
bubbleContent,
|
2023-11-08 15:34:16 +01:00
|
|
|
typingSettings,
|
2023-08-29 10:01:28 +02:00
|
|
|
}: Props) => {
|
2022-12-22 17:02:34 +01:00
|
|
|
let wordCount = bubbleContent.match(/(\w+)/g)?.length ?? 0
|
|
|
|
if (wordCount === 0) wordCount = bubbleContent.length
|
2023-11-08 15:34:16 +01:00
|
|
|
const { enabled, speed, maxDelay } = {
|
|
|
|
enabled: typingSettings?.enabled ?? defaultSettings.typingEmulation.enabled,
|
|
|
|
speed: typingSettings?.speed ?? defaultSettings.typingEmulation.speed,
|
|
|
|
maxDelay:
|
|
|
|
typingSettings?.maxDelay ?? defaultSettings.typingEmulation.maxDelay,
|
|
|
|
}
|
|
|
|
const typedWordsPerMinute = speed
|
|
|
|
let typingTimeout = enabled ? (wordCount / typedWordsPerMinute) * 60000 : 0
|
|
|
|
if (typingTimeout > maxDelay * 1000) typingTimeout = maxDelay * 1000
|
2022-12-22 17:02:34 +01:00
|
|
|
return typingTimeout
|
|
|
|
}
|