UNPKG

@douyinfe/semi-ui

Version:

A modern, comprehensive, flexible design system and UI library. Connect DesignOps & DevOps. Quickly build beautiful React apps. Maintained by Douyin-fe team.

118 lines (117 loc) 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _markdownRender = _interopRequireDefault(require("../../markdownRender")); var _constants = require("@douyinfe/semi-foundation/lib/cjs/chat/constants"); var _attachment = require("../attachment"); var _code = _interopRequireDefault(require("./code")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const { PREFIX_CHAT_BOX } = _constants.cssClasses; const { MESSAGE_STATUS, MODE, ROLE } = _constants.strings; const ChatBoxContent = props => { const { message = {}, customRenderFunc, role: roleInfo, customMarkDownComponents, mode, markdownRenderProps } = props; const { content, role, status } = message; const markdownComponents = (0, _react.useMemo)(() => Object.assign({ 'code': _code.default, 'SemiFile': _attachment.FileAttachment, 'img': _attachment.ImageAttachment }, customMarkDownComponents), [customMarkDownComponents]); const wrapCls = (0, _react.useMemo)(() => { const isUser = role === ROLE.USER; const bubble = mode === MODE.BUBBLE; const userBubble = mode === MODE.USER_BUBBLE && isUser; return (0, _classnames.default)(`${PREFIX_CHAT_BOX}-content`, { [`${PREFIX_CHAT_BOX}-content-${mode}`]: bubble || userBubble, [`${PREFIX_CHAT_BOX}-content-user`]: bubble && isUser || userBubble, [`${PREFIX_CHAT_BOX}-content-error`]: status === MESSAGE_STATUS.ERROR && (bubble || userBubble) }); }, [role, status, mode]); const node = (0, _react.useMemo)(() => { if (status === MESSAGE_STATUS.LOADING) { return /*#__PURE__*/_react.default.createElement("span", { className: `${PREFIX_CHAT_BOX}-content-loading` }, /*#__PURE__*/_react.default.createElement("span", { className: `${PREFIX_CHAT_BOX}-content-loading-item` })); } else { let realContent; if (typeof content === 'string') { realContent = /*#__PURE__*/_react.default.createElement(_markdownRender.default, Object.assign({ format: 'md', raw: content, components: markdownComponents }, markdownRenderProps)); } else if (Array.isArray(content)) { realContent = content.map((item, index) => { var _a; if (item.type === 'text') { return /*#__PURE__*/_react.default.createElement(_markdownRender.default, Object.assign({ key: `index`, format: 'md', raw: item.text, components: markdownComponents }, markdownRenderProps)); } else if (item.type === 'image_url') { return /*#__PURE__*/_react.default.createElement(_attachment.ImageAttachment, { key: `index`, src: item.image_url.url }); } else if (item.type === 'file_url') { const { name, size, url, type } = item.file_url; const realType = (_a = name.split('.').pop()) !== null && _a !== void 0 ? _a : type === null || type === void 0 ? void 0 : type.split('/').pop(); return /*#__PURE__*/_react.default.createElement(_attachment.FileAttachment, { key: `index`, url: url, name: name, size: size, type: realType }); } return null; }); } return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, realContent); } }, [status, content]); if (customRenderFunc) { return customRenderFunc({ message, role: roleInfo, defaultContent: node, className: wrapCls }); } else { return /*#__PURE__*/_react.default.createElement("div", { className: wrapCls }, node); } }; var _default = exports.default = ChatBoxContent;