import React from 'react' ; import stripIndent from 'strip-indent' ; import { Step , TextStep , OptionStep , StepTrigger , UserStep } from 'react-simple-chatbot' ; import i18n from './i18n' ; import MarkdownBubble from 'components/markdown-bubble' ; const stepsT = i18n . getFixedT ( null , 'steps' ) ; const isTextStep = ( step ) : step is TextStep => { return typeof ( step as TextStep ) . message === 'string' ; } ; const isOptionStep = ( step ) : step is OptionStep => { return Array . isArray ( ( step as OptionStep ) . options ) ; } ; const isUserStep = ( step ) : step is UserStep => { return ( step as UserStep ) . user ; } ; const addMarkdownToTextStep = ( step ) : Step => { if ( ! isTextStep ( step ) ) return step ; const { message , ... rest } = step ; return { ... rest , asMessage : true , component : < MarkdownBubble > { stripIndent ( message as string ) } < /MarkdownBubble> } ; } ; const addAnalyticsToOptionStep = ( step ) : Step => { if ( ! isOptionStep ( step ) ) return step ; const wrappedOptions = step . options . map ( option => { const triggerWrapper = data => { const triggerId = typeof option . trigger === 'function' ? option . trigger ( data ) : option . trigger ; return triggerId ; } ; return { ... option , trigger : triggerWrapper } ; } ) ; return { ... step , options : wrappedOptions } ; } ; const addAnalyticsToInputStep = ( step ) : Step => { if ( ! isUserStep ( step ) ) { return step ; } const triggerWrapper = data => { const triggerId = typeof step . trigger === 'function' ? step . trigger ( data ) : step . trigger ; return triggerId ; } ; return { ... step , trigger : triggerWrapper } ; } ; const addI18n = ( step ) : Step => { if ( isTextStep ( step ) && typeof step . message === 'string' ) { return { ... step , message : stepsT ( step . message ) } ; } if ( isOptionStep ( step ) ) { const optionsI18n = step . options . map ( option => ( { ... option , label : stepsT ( option . label ) } ) ) ; return { ... step , options : optionsI18n } ; } return step ; } ; const TRANSFORMS = [ addI18n , addMarkdownToTextStep , addAnalyticsToOptionStep , addAnalyticsToInputStep ] ; export const transformStep = ( step ) : Step => { return TRANSFORMS . reduce ( ( transformedStep , transformFn ) => transformFn ( transformedStep ) , step ) ; } ;