@@ -1,25 +1,25 @@
|
||||
import {
|
||||
TypingEmulation,
|
||||
defaultSettings,
|
||||
} from '@typebot.io/schemas/features/typebot/settings'
|
||||
import { Settings } from '@typebot.io/schemas/features/typebot/settings'
|
||||
import { defaultSettings } from '@typebot.io/schemas/features/typebot/settings/constants'
|
||||
|
||||
type Props = {
|
||||
bubbleContent: string
|
||||
typingSettings?: TypingEmulation
|
||||
typingSettings?: Settings['typingEmulation']
|
||||
}
|
||||
|
||||
export const computeTypingDuration = ({
|
||||
bubbleContent,
|
||||
typingSettings = defaultSettings({ isBrandingEnabled: false })
|
||||
.typingEmulation,
|
||||
typingSettings,
|
||||
}: Props) => {
|
||||
let wordCount = bubbleContent.match(/(\w+)/g)?.length ?? 0
|
||||
if (wordCount === 0) wordCount = bubbleContent.length
|
||||
const typedWordsPerMinute = typingSettings.speed
|
||||
let typingTimeout = typingSettings.enabled
|
||||
? (wordCount / typedWordsPerMinute) * 60000
|
||||
: 0
|
||||
if (typingTimeout > typingSettings.maxDelay * 1000)
|
||||
typingTimeout = typingSettings.maxDelay * 1000
|
||||
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
|
||||
return typingTimeout
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user