@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.
124 lines (123 loc) • 5.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _chatBoxAvatar = _interopRequireDefault(require("./chatBoxAvatar"));
var _chatBoxTitle = _interopRequireDefault(require("./chatBoxTitle"));
var _chatBoxContent = _interopRequireDefault(require("./chatBoxContent"));
var _chatBoxAction = _interopRequireDefault(require("./chatBoxAction"));
var _constants = require("@douyinfe/semi-foundation/lib/cjs/chat/constants");
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 {
ROLE,
CHAT_ALIGN
} = _constants.strings;
const ChatBox = /*#__PURE__*/_react.default.memo(props => {
const {
message,
lastChat,
align,
toast,
mode,
roleConfig,
onMessageBadFeedback,
onMessageGoodFeedback,
onMessageCopy,
onChatsChange,
onMessageDelete,
onMessageReset,
chatBoxRenderConfig = {},
customMarkDownComponents,
previousMessage,
markdownRenderProps
} = props;
const {
renderChatBoxAvatar,
renderChatBoxAction,
renderChatBoxContent,
renderChatBoxTitle,
renderFullChatBox
} = chatBoxRenderConfig;
const continueSend = (0, _react.useMemo)(() => {
return (message === null || message === void 0 ? void 0 : message.role) === (previousMessage === null || previousMessage === void 0 ? void 0 : previousMessage.role);
}, [message.role, previousMessage]);
const info = (0, _react.useMemo)(() => {
var _a;
let info = {};
if (roleConfig) {
info = (_a = roleConfig[message.role]) !== null && _a !== void 0 ? _a : {};
}
return info;
}, [message.role, roleConfig]);
const avatarNode = (0, _react.useMemo)(() => {
return /*#__PURE__*/_react.default.createElement(_chatBoxAvatar.default, {
continueSend: continueSend,
role: info,
message: message,
customRenderFunc: renderChatBoxAvatar
});
}, [info, message, renderChatBoxAvatar]);
const titleNode = (0, _react.useMemo)(() => {
return /*#__PURE__*/_react.default.createElement(_chatBoxTitle.default, {
role: info,
message: message,
customRenderFunc: renderChatBoxTitle
});
}, [info, message, renderChatBoxTitle]);
const contentNode = (0, _react.useMemo)(() => {
return /*#__PURE__*/_react.default.createElement(_chatBoxContent.default, {
mode: mode,
role: info,
message: message,
customMarkDownComponents: customMarkDownComponents,
customRenderFunc: renderChatBoxContent,
markdownRenderProps: markdownRenderProps
});
}, [message, info, renderChatBoxContent, mode]);
const actionNode = (0, _react.useMemo)(() => {
return /*#__PURE__*/_react.default.createElement(_chatBoxAction.default, {
toast: toast,
role: info,
message: message,
lastChat: lastChat,
onMessageBadFeedback: onMessageBadFeedback,
onMessageCopy: onMessageCopy,
onChatsChange: onChatsChange,
onMessageDelete: onMessageDelete,
onMessageGoodFeedback: onMessageGoodFeedback,
onMessageReset: onMessageReset,
customRenderFunc: renderChatBoxAction
});
}, [message, info, lastChat, onMessageBadFeedback, onMessageGoodFeedback, onMessageCopy, onChatsChange, onMessageDelete, onMessageReset, renderChatBoxAction]);
const containerCls = (0, _react.useMemo)(() => (0, _classnames.default)(PREFIX_CHAT_BOX, {
[`${PREFIX_CHAT_BOX}-right`]: message.role === ROLE.USER && align === CHAT_ALIGN.LEFT_RIGHT
}), [message.role, align]);
if (typeof renderFullChatBox !== 'function') {
return /*#__PURE__*/_react.default.createElement("div", {
className: containerCls
}, avatarNode, /*#__PURE__*/_react.default.createElement("div", {
className: `${PREFIX_CHAT_BOX}-wrap`
}, !continueSend && titleNode, contentNode, actionNode));
} else {
return renderFullChatBox({
message,
role: info,
defaultNodes: {
avatar: avatarNode,
title: titleNode,
content: contentNode,
action: actionNode
},
className: containerCls
});
}
});
var _default = exports.default = ChatBox;