From 06fc8083ffac73616d897936fd4b9678f787bfb4 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 16 Jul 2021 10:08:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20redux=20chat=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/model/converse.ts | 4 +-- shared/redux/slices/chat.ts | 65 ++++++++++++++++++++++++++++++++++++ shared/redux/slices/index.ts | 3 ++ shared/redux/slices/user.ts | 6 +++- 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 shared/redux/slices/chat.ts diff --git a/shared/model/converse.ts b/shared/model/converse.ts index d5a8de14..ae78c525 100644 --- a/shared/model/converse.ts +++ b/shared/model/converse.ts @@ -1,6 +1,6 @@ import { request } from '../api/request'; -interface ConverseInfo { +export interface ChatConverseInfo { _id: string; name: string; type: 'DM' | 'Group'; @@ -13,7 +13,7 @@ interface ConverseInfo { */ export async function createDMConverse( targetId: string -): Promise { +): Promise { const { data } = await request.post('/api/chat/converse/createDMConverse', { targetId, }); diff --git a/shared/redux/slices/chat.ts b/shared/redux/slices/chat.ts new file mode 100644 index 00000000..79d52275 --- /dev/null +++ b/shared/redux/slices/chat.ts @@ -0,0 +1,65 @@ +import { createSlice, PayloadAction } from '@reduxjs/toolkit'; +import type { ChatConverseInfo } from '../../model/converse'; +import type { ChatMessage } from '../../model/message'; +import _uniqBy from 'lodash/uniqBy'; +import _orderBy from 'lodash/orderBy'; + +interface ChatConverseState extends ChatConverseInfo { + messages: ChatMessage[]; +} + +interface ChatState { + converses: Record; +} + +const initialState: ChatState = { + converses: {}, +}; + +const userSlice = createSlice({ + name: 'chat', + initialState, + reducers: { + /** + * 设置会话信息 + */ + setConverseInfo(state, action: PayloadAction) { + const converseId = action.payload._id; + + state.converses[converseId] = { + messages: [], + ...action.payload, + }; + }, + + /** + * 追加消息 + */ + appendConverseMessage( + state, + action: PayloadAction<{ + converseId: string; + messages: ChatMessage[]; + }> + ) { + const { converseId, messages } = action.payload; + + if (!state.converses[converseId]) { + // 没有会话信息, 请先设置会话信息 + console.error('没有会话信息, 请先设置会话信息'); + return; + } + + const newMessages = _orderBy( + _uniqBy([...state.converses[converseId].messages, ...messages], '_id'), + 'createdAt', + 'asc' + ); + + state.converses[converseId].messages = newMessages; + }, + }, +}); + +export const chatActions = userSlice.actions; +export const chatReducer = userSlice.reducer; diff --git a/shared/redux/slices/index.ts b/shared/redux/slices/index.ts index a6bd1dcc..cdf7a9e5 100644 --- a/shared/redux/slices/index.ts +++ b/shared/redux/slices/index.ts @@ -1,10 +1,13 @@ import { combineReducers } from '@reduxjs/toolkit'; import { userReducer } from './user'; +import { chatReducer } from './chat'; export const appReducer = combineReducers({ user: userReducer, + chat: chatReducer, }); export type AppState = ReturnType; export { userActions } from './user'; +export { chatActions } from './chat'; diff --git a/shared/redux/slices/user.ts b/shared/redux/slices/user.ts index 5c65e7ed..58de21e9 100644 --- a/shared/redux/slices/user.ts +++ b/shared/redux/slices/user.ts @@ -8,7 +8,11 @@ interface UserState { friendRequests: FriendRequest[]; } -const initialState: UserState = { info: null, friends: [], friendRequests: [] }; +const initialState: UserState = { + info: null, + friends: [], + friendRequests: [], +}; const userSlice = createSlice({ name: 'user',