@droppii-org/chat-sdk
Version:
Droppii React Chat SDK
132 lines (131 loc) • 5.28 kB
JavaScript
import { SessionType, } from "@openim/wasm-client-sdk";
import { create } from "zustand";
import { DChatSDK } from "../constants/sdk";
import { conversationSort, isGroupSession } from "../utils/imCommon";
import useUserStore from "./user";
import { markConversationMessageAsRead } from "../hooks/conversation/useConversation";
const CONVERSATION_SPLIT_COUNT = 500;
const useConversationStore = create((set, get) => ({
conversationData: null,
setConversationData: (data, dataSearchClientMsgID) => set({
conversationData: data,
selectedSourceId: (data === null || data === void 0 ? void 0 : data.conversationType) === SessionType.Group
? data === null || data === void 0 ? void 0 : data.groupID
: data === null || data === void 0 ? void 0 : data.userID,
searchClientMsgID: dataSearchClientMsgID,
}),
selectedConversationId: "",
setSelectedConversationId: (threadId) => set({ selectedConversationId: threadId }),
selectedSourceId: "",
// conversation
conversationList: [],
currentConversation: undefined,
unreadCount: 0,
currentGroupInfo: undefined,
currentMemberInGroup: undefined,
getConversationListByReq: async (isOffset) => {
let tmpConversationList = [];
try {
const { data } = await DChatSDK.getConversationListSplit({
offset: isOffset ? get().conversationList.length : 0,
count: CONVERSATION_SPLIT_COUNT,
});
tmpConversationList = data;
}
catch (error) {
console.error("Error fetching conversation list:", error);
return true;
}
set((state) => ({
conversationList: [
...(isOffset ? state.conversationList : []),
...tmpConversationList,
],
}));
return tmpConversationList.length === CONVERSATION_SPLIT_COUNT;
},
updateConversationList: (list, type) => {
const idx = list.findIndex((c) => { var _a; return c.conversationID === ((_a = get().currentConversation) === null || _a === void 0 ? void 0 : _a.conversationID); });
if (idx > -1) {
get().updateCurrentConversation(list[idx]);
if (type === "filter" && list[idx].unreadCount > 0) {
markConversationMessageAsRead(list[idx].conversationID);
}
}
if (type === "filter") {
set((state) => ({
conversationList: conversationSort([...list, ...state.conversationList], state.conversationList),
}));
return;
}
let filterArr = [];
const chids = list.map((ch) => ch.conversationID);
filterArr = get().conversationList.filter((tc) => !chids.includes(tc.conversationID));
set(() => ({
conversationList: conversationSort([...list, ...filterArr]),
}));
},
updateCurrentConversation: async (conversation) => {
if (!conversation) {
set({ currentConversation: undefined });
return;
}
const prevConversation = get().currentConversation;
const toggleNewConversation = conversation.conversationID !== (prevConversation === null || prevConversation === void 0 ? void 0 : prevConversation.conversationID);
if (toggleNewConversation &&
isGroupSession(conversation.conversationType)) {
get().getCurrentGroupInfoByReq(conversation.groupID);
await get().getCurrentMemberInGroupByReq(conversation.groupID);
}
set(() => ({ currentConversation: Object.assign({}, conversation) }));
},
getCurrentGroupInfoByReq: async (groupID) => {
let groupInfo;
try {
const { data } = await DChatSDK.getSpecifiedGroupsInfo([groupID]);
groupInfo = data[0];
}
catch (error) {
console.error("Error fetching group info:", error);
return;
}
set(() => ({ currentGroupInfo: Object.assign({}, groupInfo) }));
},
getCurrentMemberInGroupByReq: async (groupID) => {
let memberInfo;
const selfID = useUserStore.getState().selfInfo.id;
try {
const { data } = await DChatSDK.getSpecifiedGroupMembersInfo({
groupID,
userIDList: [selfID],
});
memberInfo = data[0];
}
catch (error) {
set(() => ({ currentMemberInGroup: undefined }));
console.error("Error fetching group members:", error);
return;
}
set(() => ({
currentMemberInGroup: memberInfo ? Object.assign({}, memberInfo) : undefined,
}));
},
resetConversationStore: () => {
set({
conversationData: null,
selectedConversationId: "",
selectedSourceId: "",
conversationList: [],
currentConversation: undefined,
unreadCount: 0,
currentGroupInfo: undefined,
currentMemberInGroup: undefined,
});
},
//search
searchClientMsgID: "",
setSearchClientMsgID: (clientMsgID) => {
set({ searchClientMsgID: clientMsgID });
},
}));
export default useConversationStore;