general-assistant
Version:
General Assistant components
227 lines (226 loc) • 9.54 kB
JavaScript
import { RandomString } from '../_utils/tools';
import set from 'lodash/set';
import get from 'lodash/get';
import extend from 'lodash/extend';
import defaultsDeep from 'lodash/defaultsDeep';
const _set = set, _get = get, _defaultsDeep = defaultsDeep;
export default (defaultConfig, customConfig, set, get) => {
const appOption = _defaultsDeep(customConfig, defaultConfig);
function getAreaCenter() {
return _get(appOption, 'ui.areaCenter');
}
function updateMyMsgComps(key, value) {
set((state) => {
const _oldcomps = state.myComponents;
const _newcomps = Object.assign({}, _oldcomps, { [key]: value });
return { myComponents: _newcomps };
});
}
function updateChatCtxs2(opt) {
set((state) => ({ chatContexts: extend({}, state.chatContexts, opt) }));
}
const list = _get(getAreaLeft(), 'center.history.list');
const loadAreaLeft = getAreaLeft() !== false;
const initList = loadAreaLeft ? (Array.isArray(list) ? list : []) : [_getNewT()];
const create = _get(getAreaLeft(), 'center.history.create');
function getAreaLeft() {
return _get(appOption, 'ui.areaLeft');
}
function _getTemCreate(opt) {
return Object.assign({}, opt, create, {
async onClick() {
let _flag = true;
if (typeof create?.onClick === 'function') {
_flag = await create.onClick({ data: _getNewT(), ctx: get().GlobalCtxs });
}
if (_flag)
myListOperate.open(myListOperate.prepend(_getNewT()).id);
},
});
}
function _getNewT() {
return {
id: RandomString(16),
title: '新话题',
time: new Date().toLocaleString().replace(/\//g, '-'),
};
}
function newTopic(_topic) {
const _id = _topic && _topic.id ? _topic.id : RandomString(16);
const _title = _topic && _topic.title ? _topic.title : '新话题';
const _time = _topic && _topic.time ? _topic.time : new Date().toLocaleString().replace(/\//g, '-');
return Object.assign({}, _topic, { id: _id, title: _title, time: _time });
}
const myListOperate = {
getList: () => get().myHistoryList,
find: (_id) => get().myHistoryList.find((e) => e.id === _id),
open(id) {
const _recordlist = get().chatMessages;
if (Array.isArray(_recordlist) && _recordlist.length > 0) {
updateHistoryMsgList(get().myTopicOpened, _recordlist);
}
set((state) => {
return { myTopicOpened: id };
});
return true;
},
del(id) {
set((state) => {
const index = state.myHistoryList.findIndex((e) => e.id === id);
let indexNew = 0;
if (index === state.myHistoryList.length - 1) {
indexNew = index - 1;
}
else {
indexNew = index;
}
return {
myHistoryList: [...state.myHistoryList.filter((e) => e.id !== id)],
myTopicOpened: state.myHistoryList[indexNew]?.id,
};
});
},
prepend(_topic) {
const _newTopic = newTopic(_topic);
const _find = get().myHistoryList.find((e) => e.id === _newTopic.id);
if (_find)
return null;
set((state) => {
if (state._myListContainer?.current)
state._myListContainer.current.scrollTop = 0;
return { myHistoryList: [_newTopic].concat(state.myHistoryList), };
});
return _newTopic;
},
append(_topic) {
const _newTopic = newTopic(_topic);
const _find = get().myHistoryList.find((e) => e.id === _newTopic.id);
if (_find)
return null;
set((state) => {
if (state._myListContainer?.current)
state._myListContainer.current.scrollTop = state._myListContainer.current.scrollHeight;
return { myHistoryList: state.myHistoryList.concat([_newTopic]) };
});
return _newTopic;
},
update(_topic) {
const { id } = _topic || {};
if (id) {
set((state) => {
const _newlist = state.myHistoryList.map((e) => e.id === id ? Object.assign({}, e, _topic) : e);
return { myHistoryList: _newlist };
});
}
},
reset(arr) {
set(() => ({ myHistoryList: Array.isArray(arr) ? arr : [] }));
},
updateHistoryComposer,
updateHistoryMsgList,
updateHistoryMsg,
};
function updateHistoryMsgList(_cid, _msgs) {
console.log('———— 批量更新消息?', _cid, '_msgs?', _msgs);
if (typeof _cid !== 'string')
return false;
const cID = _cid.replace(' ', '');
if (cID === '')
return false;
if (!Array.isArray(_msgs))
return false;
const _originList = get().historyMsg.filter((e) => e[0] !== _cid);
const _newList = [[_cid, _msgs]].concat(_originList);
console.log('>>> 批量更新消息', _newList);
set(() => ({ historyMsg: optimizeList(_newList) }));
}
function updateHistoryMsg(_cid, _msg) {
console.log('———— 更新消息?', _cid, '_msg?', _msg);
if (typeof _cid !== 'string')
return false;
const cID = _cid.replace(' ', '');
if (typeof _msg !== 'object' || _msg === null)
return false;
const mID = _msg._id.replace(' ', '');
if (cID === '')
return false;
if (mID === '')
return false;
const _originList = get().historyMsg;
let _findList = _originList.find((e) => e[0] === _cid);
if (_findList === undefined) {
_findList = [_cid, []];
}
const _filterMsgs = _findList[1].filter((e) => e._id !== _msg._id);
const _findMsg = _findList[1].find((e) => e._id === _msg._id);
const _newMsgs = _filterMsgs.concat(Object.assign({ position: 'left' }, _findMsg, _msg));
const _filterlist = _originList.filter((e) => e[0] !== _cid);
const _newList = ([[_cid, _newMsgs]]).concat(_filterlist);
console.log('>>> 更新消息', _newList);
set(() => ({ historyMsg: optimizeList(_newList) }));
}
function optimizeList(_list) {
if (Array.isArray(_list)) {
const _maxLen = 5;
if (_list.length > _maxLen) {
const _num = _list.length - _maxLen;
new Array(_num).fill(1).filter(e => (_list.pop()));
}
}
return _list;
}
function updateHistoryComposer(_cid, _obj) {
if (typeof _cid !== 'string')
return false;
const cID = _cid.replace(' ', '');
if (cID === '')
return false;
const _originObj = get().historyComposer;
const _newComp = Object.assign({}, _originObj[cID], _obj);
set(() => ({ historyComposer: Object.assign({}, _originObj, { [cID]: _newComp }) }));
}
return {
getAreaCenter: getAreaCenter,
MyHandlers: _get(appOption, 'handlers'),
myComponents: _get(appOption, 'components'),
updateMyMsgComps: updateMyMsgComps,
chatMessages: [],
setChatMessages: (opt) => { set((state) => { return { chatMessages: opt }; }); },
getChatMessages: () => get().chatMessages,
historyMsg: [],
updateHistoryMsg: (_cid, _msg) => updateHistoryMsg(_cid, _msg),
updateHistoryMsgList: (_cid, _msgs) => updateHistoryMsgList(_cid, _msgs),
findHistoryMsg: (_cid) => get().historyMsg.find((e) => e[0] === _cid),
historyComposer: {},
updateHistoryComposer: (_cid, _obj) => updateHistoryComposer(_cid, _obj),
findHistoryComposer: (_cid) => get().historyComposer[_cid],
chatContexts: {},
updateChatCtxs: updateChatCtxs2,
GlobalCtxs: {},
setGlobalCtxs: (opt) => { set((state) => { return { GlobalCtxs: opt }; }); },
citationInfo: null,
setCitationInfo: (opt) => { set(() => ({ citationInfo: opt })); },
getCitation: () => get().citationInfo,
selectedAite: null,
setSelectedAite: (opt) => { set(() => ({ selectedAite: opt })); },
confirmAite: null,
setConfirmAite: (opt) => { set(() => ({ confirmAite: opt })); },
getConfirmAite: () => get().confirmAite,
isShowAite: false,
getShowAite: () => get().isShowAite,
setShowAite: (opt) => { set(() => ({ isShowAite: opt })); },
myHistoryList: initList,
setMyHistoryList: (_arr) => set(() => ({ myHistoryList: _arr })),
isOpenFirst: _get(getAreaLeft(), 'center.history.isOpenFirst', true),
myTopicOpened: null,
setMyTopicOpened: (_id) => set(() => ({ myTopicOpened: _id })),
_myListContainer: null,
_getTemCreate: _getTemCreate,
topic: myListOperate,
getAreaLeft: getAreaLeft,
hasMoreUp: true,
setHasMoreUp: (_boo) => set(() => ({ hasMoreUp: _boo === true })),
hasMoreDown: false,
setHasMoreDown: (_boo) => set(() => ({ hasMoreDown: _boo === true })),
};
};