|
|
|
@ -45,7 +45,7 @@ const websocketSlice = (set, get) => ({
|
|
|
|
|
setWebsocketRetrytimes: (retrytimes) => set({ websocketRetrytimes: retrytimes, websocketRetrying: retrytimes > 0 }),
|
|
|
|
|
|
|
|
|
|
connectWebsocket: (userId) => {
|
|
|
|
|
const { setWebsocket, setWebsocketOpened, setWebsocketRetrytimes, addError, handleMessage } = get();
|
|
|
|
|
const { setWebsocket, setWebsocketOpened, setWebsocketRetrytimes, addError, handleMessage, activeConversations } = get();
|
|
|
|
|
|
|
|
|
|
const realtimeAPI = new RealTimeAPI(
|
|
|
|
|
{
|
|
|
|
@ -56,7 +56,15 @@ const websocketSlice = (set, get) => ({
|
|
|
|
|
setWebsocketOpened(true);
|
|
|
|
|
setWebsocketRetrytimes(0);
|
|
|
|
|
},
|
|
|
|
|
() => setWebsocketOpened(false),
|
|
|
|
|
() => {
|
|
|
|
|
setWebsocketOpened(false)
|
|
|
|
|
const newMsgList = Object.keys(activeConversations).reduce((acc, key) => {
|
|
|
|
|
const newMsgList = activeConversations[key].slice(-10);
|
|
|
|
|
acc[key] = newMsgList;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
set({ activeConversations: newMsgList });
|
|
|
|
|
},
|
|
|
|
|
(n) => setWebsocketRetrytimes(n)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
@ -108,6 +116,8 @@ const websocketSlice = (set, get) => ({
|
|
|
|
|
if (permission === 'granted') {
|
|
|
|
|
const notification = new Notification(`${msgRender.senderName}`, {
|
|
|
|
|
body: msgRender?.text || `[ ${msgRender.type} ]`,
|
|
|
|
|
requireInteraction: true, // 设置手动关闭
|
|
|
|
|
tag: 'global-sales-notification', // 通知ID,同类通知建议设置相同ID,避免通知过多遮挡桌面
|
|
|
|
|
...(msgRender.type === 'photo' ? { image: msgRender.data.uri } : {}),
|
|
|
|
|
});
|
|
|
|
|
notification.onclick = function () {
|
|
|
|
@ -149,7 +159,8 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
|
|
|
|
|
return set((state) => ({
|
|
|
|
|
conversationsList: [...newConversations, ...state.conversationsList],
|
|
|
|
|
activeConversations: { ...activeConversations, ...newConversationsMapped }
|
|
|
|
|
activeConversations: { ...activeConversations, ...newConversationsMapped },
|
|
|
|
|
totalNotify: state.totalNotify + newConversations.map(ele => ele.unread_msg_count).reduce((acc, cur) => acc + cur, 0),
|
|
|
|
|
}));
|
|
|
|
|
},
|
|
|
|
|
delConversationitem: (conversation) => {
|
|
|
|
@ -158,15 +169,15 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
|
|
|
|
|
conversationsList.splice(targetIndex, 1);
|
|
|
|
|
|
|
|
|
|
return set((state) => ({
|
|
|
|
|
return set({
|
|
|
|
|
conversationsList: [...conversationsList],
|
|
|
|
|
activeConversations: { ...activeConversations, [`${targetId}`]: [] },
|
|
|
|
|
currentConversation: {},
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
setCurrentConversation: (conversation) => {
|
|
|
|
|
// 清空未读
|
|
|
|
|
const { conversationsList } = get();
|
|
|
|
|
const { conversationsList, totalNotify } = get();
|
|
|
|
|
const targetId = conversation.sn;
|
|
|
|
|
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
|
|
|
|
|
targetIndex !== -1 ? conversationsList.splice(targetIndex, 1, {
|
|
|
|
@ -174,11 +185,12 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
unread_msg_count: 0,
|
|
|
|
|
}) : null;
|
|
|
|
|
|
|
|
|
|
return set({ currentConversation: conversation, referenceMsg: {}, conversationsList: [...conversationsList] });
|
|
|
|
|
return set({ totalNotify: totalNotify - conversation.unread_msg_count, currentConversation: conversation, referenceMsg: {}, conversationsList: [...conversationsList] });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const messageSlice = (set, get) => ({
|
|
|
|
|
totalNotify: 0,
|
|
|
|
|
msgListLoading: false,
|
|
|
|
|
activeConversations: {},
|
|
|
|
|
setMsgLoading: (msgListLoading) => set({ msgListLoading }),
|
|
|
|
@ -216,13 +228,13 @@ const messageSlice = (set, get) => ({
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return set((state) => ({
|
|
|
|
|
return set({
|
|
|
|
|
activeConversations: { ...activeConversations, [String(targetId)]: targetMsgs },
|
|
|
|
|
conversationsList: [...conversationsList],
|
|
|
|
|
}));
|
|
|
|
|
// conversationsList: [...conversationsList],
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
sentOrReceivedNewMessage: (targetId, message) => { // msgRender:
|
|
|
|
|
const { activeConversations, conversationsList, currentConversation } = get();
|
|
|
|
|
const { activeConversations, conversationsList, currentConversation, totalNotify } = get();
|
|
|
|
|
const targetMsgs = activeConversations[String(targetId)] || [];
|
|
|
|
|
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
|
|
|
|
|
const lastReceivedTime = message.type !== 'system' && message.sender !== 'me' ? message.date : null;
|
|
|
|
@ -244,14 +256,15 @@ const messageSlice = (set, get) => ({
|
|
|
|
|
};
|
|
|
|
|
conversationsList.splice(targetIndex, 1);
|
|
|
|
|
conversationsList.unshift(newConversation);
|
|
|
|
|
return set((state) => ({
|
|
|
|
|
return set({
|
|
|
|
|
totalNotify: totalNotify+newConversation.unread_msg_count,
|
|
|
|
|
activeConversations: { ...activeConversations, [String(targetId)]: [...targetMsgs, message] },
|
|
|
|
|
conversationsList: [...conversationsList],
|
|
|
|
|
currentConversation: {
|
|
|
|
|
...currentConversation,
|
|
|
|
|
...(String(targetId) === String(currentConversation.sn) ? { last_received_time: message.date } : {}),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|