@ant-design/pro-chat
Version:
a solution for ai chat
115 lines (112 loc) • 4.86 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import ChatList from "../../../ChatList";
import isEqual from 'fast-deep-equal';
import { memo, useMemo } from 'react';
import { useStore } from "../../store";
import { chatSelectors } from "../../store/selectors";
import useProChatLocale from "../../hooks/useProChatLocale";
import { useRefFunction } from "../../hooks/useRefFunction";
import { renderActions } from "./Actions";
import { renderMessagesExtra } from "./Extras";
import { renderMessages } from "./Messages";
import SkeletonList from "./SkeletonList";
import { jsx as _jsx } from "react/jsx-runtime";
var List = /*#__PURE__*/memo(function (_ref) {
var showTitle = _ref.showTitle,
itemShouldUpdate = _ref.itemShouldUpdate,
chatItemRenderConfig = _ref.chatItemRenderConfig,
renderErrorMessages = _ref.renderErrorMessages,
markdownProps = _ref.markdownProps;
var data = useStore(chatSelectors.currentChatsWithGuideMessage, isEqual);
var _useProChatLocale = useProChatLocale(),
localeObj = _useProChatLocale.localeObject;
var locale = useStore(function (s) {
return s.locale;
});
var _useStore = useStore(function (s) {
var config = s.config;
return [s.init, s.displayMode, config.enableHistoryCount, config.historyCount, s.chatLoadingId, s.deleteMessage, s.resendMessage, s.dispatchMessage];
}),
_useStore2 = _slicedToArray(_useStore, 8),
init = _useStore2[0],
displayMode = _useStore2[1],
enableHistoryCount = _useStore2[2],
historyCount = _useStore2[3],
chatLoadingId = _useStore2[4],
deleteMessage = _useStore2[5],
resendMessage = _useStore2[6],
dispatchMessage = _useStore2[7];
var onActionsClick = useRefFunction(function (action, _ref2) {
var id = _ref2.id,
error = _ref2.error;
switch (action.key) {
case 'del':
{
var _chatItemRenderConfig, _chatItemRenderConfig2;
// 执行删除消息回调
chatItemRenderConfig === null || chatItemRenderConfig === void 0 || (_chatItemRenderConfig = chatItemRenderConfig.actionsCallbacks) === null || _chatItemRenderConfig === void 0 || (_chatItemRenderConfig2 = _chatItemRenderConfig.beforeDelFinished) === null || _chatItemRenderConfig2 === void 0 || _chatItemRenderConfig2.call(_chatItemRenderConfig, id);
deleteMessage(id);
break;
}
case 'regenerate':
{
var _chatItemRenderConfig3, _chatItemRenderConfig4;
resendMessage(id);
// if this message is an error message, we need to delete it
if (error) deleteMessage(id);
// 执行重新生成
chatItemRenderConfig === null || chatItemRenderConfig === void 0 || (_chatItemRenderConfig3 = chatItemRenderConfig.actionsCallbacks) === null || _chatItemRenderConfig3 === void 0 || (_chatItemRenderConfig4 = _chatItemRenderConfig3.onRegenerateFinished) === null || _chatItemRenderConfig4 === void 0 || _chatItemRenderConfig4.call(_chatItemRenderConfig3, id, error);
break;
}
}
// TODO: need a custom callback
});
var onMessageChange = useRefFunction(function (id, content) {
var _chatItemRenderConfig5, _chatItemRenderConfig6;
dispatchMessage({
id: id,
key: 'content',
type: 'updateMessage',
value: content
});
// 执行 Message Update Callback
chatItemRenderConfig === null || chatItemRenderConfig === void 0 || (_chatItemRenderConfig5 = chatItemRenderConfig.actionsCallbacks) === null || _chatItemRenderConfig5 === void 0 || (_chatItemRenderConfig6 = _chatItemRenderConfig5.onEditFinished) === null || _chatItemRenderConfig6 === void 0 || _chatItemRenderConfig6.call(_chatItemRenderConfig5, id, content);
});
var textObj = useMemo(function () {
return {
cancel: localeObj.cancel,
confirm: localeObj.confirm,
copy: localeObj.copy,
copySuccess: localeObj.copySuccess,
delete: localeObj.delete,
edit: localeObj.edit,
history: localeObj.history,
regenerate: localeObj.regenerate
};
}, [locale]);
if (!init) return /*#__PURE__*/_jsx(SkeletonList, {});
return /*#__PURE__*/_jsx(ChatList, {
showTitle: showTitle,
data: data,
itemShouldUpdate: itemShouldUpdate,
enableHistoryCount: enableHistoryCount,
historyCount: historyCount,
loadingId: chatLoadingId,
onActionsClick: onActionsClick,
onMessageChange: onMessageChange,
renderActions: renderActions
// need support custom Render
,
renderMessages: renderMessages,
renderErrorMessages: renderErrorMessages,
renderMessagesExtra: renderMessagesExtra,
style: {
marginTop: 24
},
chatItemRenderConfig: chatItemRenderConfig,
text: textObj,
type: displayMode || 'chat',
markdownProps: markdownProps
});
});
export default List;