refactor: 当接受到远程消息时,更新本地未读状态

pull/13/head
moonrailgun 4 years ago
parent 0988a4387d
commit 1ace22201d

@ -1,4 +1,4 @@
import { useCallback } from 'react'; import { useCallback, useEffect } from 'react';
import { ensureDMConverse } from '../../helper/converse-helper'; import { ensureDMConverse } from '../../helper/converse-helper';
import { useAsync } from '../../hooks/useAsync'; import { useAsync } from '../../hooks/useAsync';
import { showErrorToasts } from '../../manager/ui'; import { showErrorToasts } from '../../manager/ui';
@ -24,6 +24,14 @@ export function useConverseMessage(context: ConverseContext) {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const messages = converse?.messages ?? []; const messages = converse?.messages ?? [];
useEffect(() => {
dispatch(chatActions.updateCurrentConverseId(converseId));
return () => {
dispatch(chatActions.updateCurrentConverseId(null));
};
}, [converseId]);
// NOTICE: 该hook只会在converseId变化时执行 // NOTICE: 该hook只会在converseId变化时执行
const { loading, error } = useAsync(async () => { const { loading, error } = useAsync(async () => {
if (!converse) { if (!converse) {

@ -3,6 +3,8 @@ import type { ChatConverseInfo } from '../../model/converse';
import type { ChatMessage } from '../../model/message'; import type { ChatMessage } from '../../model/message';
import _uniqBy from 'lodash/uniqBy'; import _uniqBy from 'lodash/uniqBy';
import _orderBy from 'lodash/orderBy'; import _orderBy from 'lodash/orderBy';
import _last from 'lodash/last';
import { isValidStr } from '../../utils/string-helper';
export interface ChatConverseState extends ChatConverseInfo { export interface ChatConverseState extends ChatConverseInfo {
messages: ChatMessage[]; messages: ChatMessage[];
@ -10,12 +12,14 @@ export interface ChatConverseState extends ChatConverseInfo {
} }
interface ChatState { interface ChatState {
currentConverseId: string | null; // 当前活跃的会话id
converses: Record<string, ChatConverseState>; // <会话Id, 会话信息> converses: Record<string, ChatConverseState>; // <会话Id, 会话信息>
ack: Record<string, string>; // <会话Id, 本地最后一条会话Id> ack: Record<string, string>; // <会话Id, 本地最后一条会话Id>
lastMessageMap: Record<string, string>; // <会话Id, 远程最后一条会话Id> lastMessageMap: Record<string, string>; // <会话Id, 远程最后一条会话Id>
} }
const initialState: ChatState = { const initialState: ChatState = {
currentConverseId: null,
converses: {}, converses: {},
ack: {}, ack: {},
lastMessageMap: {}, lastMessageMap: {},
@ -25,6 +29,10 @@ const chatSlice = createSlice({
name: 'chat', name: 'chat',
initialState, initialState,
reducers: { reducers: {
updateCurrentConverseId(state, action: PayloadAction<string | null>) {
state.currentConverseId = action.payload;
},
/** /**
* *
*/ */
@ -58,11 +66,18 @@ const chatSlice = createSlice({
const newMessages = _orderBy( const newMessages = _orderBy(
_uniqBy([...state.converses[converseId].messages, ...messages], '_id'), _uniqBy([...state.converses[converseId].messages, ...messages], '_id'),
'createdAt', '_id',
'asc' 'asc'
); );
state.converses[converseId].messages = newMessages; state.converses[converseId].messages = newMessages;
if (state.currentConverseId !== converseId) {
const lastMessageId = _last(messages)?._id;
if (isValidStr(lastMessageId)) {
state.lastMessageMap[converseId] = lastMessageId;
}
}
}, },
initialHistoryMessage( initialHistoryMessage(

Loading…
Cancel
Save