|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import { create } from 'zustand';
|
|
|
|
|
import { RealTimeAPI } from '@/channel/realTimeAPI';
|
|
|
|
|
import { olog, isEmpty } from '@/utils/commons';
|
|
|
|
|
import { olog, isEmpty, groupBy } from '@/utils/commons';
|
|
|
|
|
import { receivedMsgTypeMapped, handleNotification } from '@/channel/bubbleMsgUtils';
|
|
|
|
|
import { fetchConversationsList, fetchTemplates, fetchConversationsSearch, UNREAD_MARK, fetchTags } from '@/actions/ConversationActions';
|
|
|
|
|
import { devtools } from 'zustand/middleware';
|
|
|
|
@ -40,6 +40,8 @@ const initialConversationState = {
|
|
|
|
|
|
|
|
|
|
closedConversationsList: [], // 已关闭的对话列表
|
|
|
|
|
conversationsList: [], // 对话列表
|
|
|
|
|
topList: [],
|
|
|
|
|
pageList: [],
|
|
|
|
|
currentConversation: {}, // 当前对话
|
|
|
|
|
|
|
|
|
|
activeConversations: {}, // 激活的对话的消息列表: { [conversationId]: <messageItem>[] }
|
|
|
|
@ -207,6 +209,9 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
currentConversation: {},
|
|
|
|
|
closedConversationsList: [],
|
|
|
|
|
|
|
|
|
|
topList: [],
|
|
|
|
|
pageList: [],
|
|
|
|
|
|
|
|
|
|
setConversationsListLoading: (conversationsListLoading) => set({ conversationsListLoading }),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -214,9 +219,18 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
* 搜索结果
|
|
|
|
|
*/
|
|
|
|
|
setConversationsList: (conversationsList) => {
|
|
|
|
|
const { activeConversations, } = get();
|
|
|
|
|
const { activeConversations, } = get();
|
|
|
|
|
const conversationsMapped = conversationsList.reduce((r, v) => ({ ...r, [`${v.sn}`]: [] }), {});
|
|
|
|
|
return set({ conversationsList, activeConversations: {...conversationsMapped, ...activeConversations,} });
|
|
|
|
|
|
|
|
|
|
const conversationsTopStateMapped = groupBy(conversationsList, 'top_state');
|
|
|
|
|
|
|
|
|
|
return set({
|
|
|
|
|
topList: conversationsTopStateMapped[1] || [],
|
|
|
|
|
// conversationsList: conversationsTopStateMapped[0],
|
|
|
|
|
pageList: conversationsTopStateMapped[0] || [],
|
|
|
|
|
conversationsList,
|
|
|
|
|
activeConversations: { ...conversationsMapped, ...activeConversations }
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
setClosedConversationList: (closedConversationsList) => {
|
|
|
|
|
const { activeConversations, } = get();
|
|
|
|
@ -224,7 +238,7 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
return set({ closedConversationsList, activeConversations: { ...activeConversations, ...listMapped } });
|
|
|
|
|
},
|
|
|
|
|
addToConversationList: (newList, position='top') => {
|
|
|
|
|
const { activeConversations, conversationsList } = get();
|
|
|
|
|
const { activeConversations, conversationsList, } = get();
|
|
|
|
|
// const conversationsIds = Object.keys(activeConversations);
|
|
|
|
|
const conversationsIds = conversationsList.map((chatItem) => `${chatItem.sn}`);
|
|
|
|
|
const newConversations = newList.filter((conversation) => !conversationsIds.includes(`${conversation.sn}`));
|
|
|
|
@ -236,9 +250,13 @@ const conversationSlice = (set, get) => ({
|
|
|
|
|
const updateList = replaceObjectsByKey(conversationsList, newList, 'sn');
|
|
|
|
|
|
|
|
|
|
const mergedList = position==='top' ? [...newList, ...withoutNew] : [...updateList, ...newConversations];
|
|
|
|
|
const mergedListMapped = groupBy(mergedList, 'top_state');
|
|
|
|
|
|
|
|
|
|
const refreshTotalNotify = mergedList.reduce((r, c) => r+(c.unread_msg_count === UNREAD_MARK ? 0 : c.unread_msg_count), 0);
|
|
|
|
|
|
|
|
|
|
return set((state) => ({
|
|
|
|
|
topList: mergedListMapped[1] || [],
|
|
|
|
|
pageList: mergedListMapped[0] || [],
|
|
|
|
|
conversationsList: mergedList,
|
|
|
|
|
activeConversations: { ...activeConversations, ...newConversationsMapped },
|
|
|
|
|
totalNotify: refreshTotalNotify,
|
|
|
|
@ -444,17 +462,11 @@ export const useConversationStore = create(
|
|
|
|
|
|
|
|
|
|
// side effects
|
|
|
|
|
fetchInitialData: async ({userId, userIds, whatsAppBusiness, ...loginUser}) => {
|
|
|
|
|
const { addToConversationList, setTemplates, setInitial, setClosedConversationList, setTags } = get();
|
|
|
|
|
|
|
|
|
|
// const conversationsList = await fetchConversationsList({ opisn: userIds });
|
|
|
|
|
// addToConversationList(conversationsList);
|
|
|
|
|
const { setTemplates, setInitial, setTags } = get();
|
|
|
|
|
|
|
|
|
|
const templates = await fetchTemplates({ waba: whatsAppBusiness });
|
|
|
|
|
setTemplates(templates);
|
|
|
|
|
|
|
|
|
|
// const closedList = await fetchConversationsList({ opisn: userIds, session_enable: 0 });
|
|
|
|
|
// setClosedConversationList(closedList);
|
|
|
|
|
|
|
|
|
|
const myTags = await fetchTags({ opisn: userId});
|
|
|
|
|
setTags(myTags);
|
|
|
|
|
|
|
|
|
|