UNPKG

@botonic/react

Version:

Build Chatbots using React

57 lines 2.97 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { createElement as _createElement } from "react"; import { isFacebook, isWhatsapp } from '@botonic/core'; import React, { useContext } from 'react'; import { RequestContext } from '../../contexts'; import { deepMapWithIndex } from '../../util/react'; import { Text } from '../text'; import { MultichannelButton } from './multichannel-button'; import { MultichannelCarousel } from './multichannel-carousel'; import { MultichannelContext } from './multichannel-context'; import { MultichannelReply } from './multichannel-reply'; import { MultichannelText } from './multichannel-text'; import { isNodeButton, isNodeCarousel, isNodeReply, isNodeText, } from './multichannel-utils'; import { MULTICHANNEL_WHATSAPP_PROPS } from './whatsapp/constants'; export const Multichannel = props => { const requestContext = useContext(RequestContext); if (!isWhatsapp(requestContext.session) && !isFacebook(requestContext.session)) { return props.children; } if (isFacebook(requestContext.session)) { const newChildren = deepMapWithIndex(props.children, child => { if (isNodeText(child)) { return (_createElement(MultichannelText, Object.assign({}, child.props, { key: child.key }), child.props.children)); } return child; }); return newChildren; } let newChildren = deepMapWithIndex(props.children, (child, index) => { if (isNodeButton(child)) { return (_createElement(MultichannelButton, Object.assign({}, child.props, { key: child.key }), child.props.children)); } if (isNodeReply(child)) { return (_createElement(MultichannelReply, Object.assign({}, child.props, { key: child.key }), child.props.children)); } if (isNodeText(child)) { return (_createElement(MultichannelText, Object.assign({}, child.props, props.text, { key: child.key }, (props.messageSeparator && index > 0 && { newline: props.messageSeparator })), child.props.children)); } if (isNodeCarousel(child)) { return (_createElement(MultichannelCarousel, Object.assign({}, child.props, props.carousel, { key: child.key }), child.props.children)); } return child; }); if (props.messageSeparator != null) { newChildren = newChildren.map((c, index) => index > 0 && typeof c === 'string' ? props.messageSeparator + c : c); newChildren = (_jsx(Text, Object.assign({}, MULTICHANNEL_WHATSAPP_PROPS, { children: newChildren }), props.key)); } return (_jsx(MultichannelContext.Provider, Object.assign({ value: { currentIndex: props.firstIndex, boldIndex: props.boldIndex, indexSeparator: props.indexSeparator, messageSeparator: props.messageSeparator, } }, { children: newChildren }))); }; //# sourceMappingURL=multichannel.js.map