Skip to content

Commit ed46714

Browse files
committed
fix: 修复在频道中发送消息无法正常带出选中角色信息的bug
1 parent 9edf288 commit ed46714

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import { GroupInfo } from '@redux/types/group';
33

44
/**
@@ -7,3 +7,12 @@ import { GroupInfo } from '@redux/types/group';
77

88
export const GroupInfoContext = React.createContext<GroupInfo | null>(null);
99
GroupInfoContext.displayName = 'GroupInfoContext';
10+
11+
/**
12+
* 获取当前组件所在位置的团UUID
13+
*/
14+
export function useCurrentGroupUUID(): string | undefined {
15+
const groupInfo = useContext(GroupInfoContext);
16+
17+
return groupInfo?.uuid;
18+
}

src/shared/hooks/useMsgSend.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { MsgDataManager } from '@shared/utils/msg-helper';
99
import _isNil from 'lodash/isNil';
1010
import { useMsgContainerContext } from '@shared/context/MsgContainerContext';
1111
import { useSelectedGroupActorInfo } from '@redux/hooks/group';
12-
import { GroupInfoContext } from '@shared/context/GroupInfoContext';
12+
import {
13+
GroupInfoContext,
14+
useCurrentGroupUUID,
15+
} from '@shared/context/GroupInfoContext';
1316

1417
/**
1518
* 消息输入相关事件
@@ -22,11 +25,12 @@ export function useMsgSend(converseUUID: string) {
2225
const converseType = converse?.type;
2326
const dispatch = useTRPGDispatch();
2427
const { replyMsg, clearReplyMsg } = useMsgContainerContext();
25-
const groupInfo = useContext(GroupInfoContext);
26-
const currentGroupUUID = groupInfo?.uuid;
28+
const currentGroupUUID = useCurrentGroupUUID();
2729

2830
// 获取选中团角色的信息 仅group类型会话有用
29-
const selectedGroupActorInfo = useSelectedGroupActorInfo(converseUUID);
31+
const selectedGroupActorInfo = useSelectedGroupActorInfo(
32+
currentGroupUUID ?? ''
33+
);
3034

3135
useEffect(() => {
3236
// 当当前会话发生变化时,清空回复消息

src/shared/redux/actions/chat.ts

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const switchConverse = createAction(
6767
export const switchToConverse = function switchToConverse(
6868
converseUUID: string
6969
): TRPGAction {
70-
return function(dispatch, getState) {
70+
return function (dispatch, getState) {
7171
dispatch(hideProfileCard());
7272
dispatch(switchMenuPannel(0));
7373
dispatch(switchConverse(converseUUID));
@@ -87,7 +87,7 @@ export const updateConversesMsglist = function updateConversesMsglist(
8787
convUUID: string,
8888
list: any[]
8989
): TRPGAction {
90-
return function(dispatch, getState) {
90+
return function (dispatch, getState) {
9191
for (const item of list) {
9292
if (item.sender_uuid) {
9393
checkUser(item.sender_uuid);
@@ -103,10 +103,10 @@ export const updateConversesMsglist = function updateConversesMsglist(
103103

104104
// 获取多人会话
105105
export const getConverses = function getConverses(cb?: () => void): TRPGAction {
106-
return function(dispatch, getState) {
106+
return function (dispatch, getState) {
107107
dispatch({ type: GET_CONVERSES_REQUEST });
108108
// 获取会话列表
109-
return api.emit('chat::getConverses', {}, function(data) {
109+
return api.emit('chat::getConverses', {}, function (data) {
110110
cb && cb();
111111
if (data.result) {
112112
const list = data.list;
@@ -125,7 +125,7 @@ export const getConverses = function getConverses(cb?: () => void): TRPGAction {
125125
api.emit(
126126
'chat::getConverseChatLog',
127127
{ converse_uuid: convUUID },
128-
function(data) {
128+
function (data) {
129129
if (data.result) {
130130
dispatch(updateConversesMsglist(convUUID, data.list));
131131
} else {
@@ -149,15 +149,15 @@ export const createConverse = function createConverse(
149149
type,
150150
isSwitchToConv = true
151151
): TRPGAction {
152-
return function(dispatch, getState) {
152+
return function (dispatch, getState) {
153153
if (!!getState().chat.converses[uuid]) {
154154
console.log('已存在该会话');
155155
if (isSwitchToConv) {
156156
dispatch(switchToConverse(uuid));
157157
}
158158
return;
159159
}
160-
return api.emit('chat::createConverse', { uuid, type }, function(data) {
160+
return api.emit('chat::createConverse', { uuid, type }, function (data) {
161161
console.log('chat::createConverse', data);
162162
if (data.result) {
163163
const conv = data.data;
@@ -172,7 +172,7 @@ export const createConverse = function createConverse(
172172
api.emit(
173173
'chat::getConverseChatLog',
174174
{ converse_uuid: convUUID },
175-
function(data) {
175+
function (data) {
176176
if (data.result) {
177177
const list = data.list;
178178
dispatch(updateConversesMsglist(convUUID, list));
@@ -195,8 +195,8 @@ export const createConverse = function createConverse(
195195
export const removeConverse = function removeConverse(
196196
converseUUID: string
197197
): TRPGAction {
198-
return function(dispatch, getState) {
199-
return api.emit('chat::removeConverse', { converseUUID }, function(data) {
198+
return function (dispatch, getState) {
199+
return api.emit('chat::removeConverse', { converseUUID }, function (data) {
200200
if (data.result) {
201201
dispatch({ type: REMOVE_CONVERSES_SUCCESS, converseUUID });
202202
} else {
@@ -229,7 +229,7 @@ export const removeUserConverse = (userConverseUUID: string): TRPGAction => {
229229
export const addUserConverse = function addUserConverse(
230230
senders: string[]
231231
): TRPGAction {
232-
return function(dispatch, getState) {
232+
return function (dispatch, getState) {
233233
if (typeof senders === 'string') {
234234
senders = [senders];
235235
}
@@ -280,7 +280,7 @@ export const addUserConverse = function addUserConverse(
280280
});
281281

282282
// 更新消息列表
283-
api.emit('chat::getUserChatLog', { user_uuid: uuid }, function(data) {
283+
api.emit('chat::getUserChatLog', { user_uuid: uuid }, function (data) {
284284
if (data.result) {
285285
const list = data.list;
286286
dispatch(updateConversesMsglist(uuid, list));
@@ -291,7 +291,7 @@ export const addUserConverse = function addUserConverse(
291291
}
292292

293293
// 更新系统消息
294-
api.emit('chat::getUserChatLog', { user_uuid: 'trpgsystem' }, function(
294+
api.emit('chat::getUserChatLog', { user_uuid: 'trpgsystem' }, function (
295295
data
296296
) {
297297
if (data.result) {
@@ -307,8 +307,10 @@ export const addUserConverse = function addUserConverse(
307307
export const getOfflineUserConverse = function getOfflineUserConverse(
308308
lastLoginDate: string
309309
): TRPGAction {
310-
return function(dispatch, getState) {
311-
api.emit('chat::getOfflineUserConverse', { lastLoginDate }, function(data) {
310+
return function (dispatch, getState) {
311+
api.emit('chat::getOfflineUserConverse', { lastLoginDate }, function (
312+
data
313+
) {
312314
if (data.result === true) {
313315
dispatch(addUserConverse(data.senders));
314316
} else {
@@ -319,8 +321,8 @@ export const getOfflineUserConverse = function getOfflineUserConverse(
319321
};
320322

321323
export const getAllUserConverse = function getAllUserConverse(): TRPGAction {
322-
return function(dispatch, getState) {
323-
api.emit('chat::getAllUserConverse', {}, function(data) {
324+
return function (dispatch, getState) {
325+
api.emit('chat::getAllUserConverse', {}, function (data) {
324326
if (data.result === true) {
325327
dispatch(addUserConverse(data.senders));
326328
} else {
@@ -335,12 +337,12 @@ export const getAllUserConverse = function getAllUserConverse(): TRPGAction {
335337
export const reloadConverseList = function reloadConverseList(
336338
cb?: () => void
337339
): TRPGAction {
338-
return function(dispatch, getState) {
340+
return function (dispatch, getState) {
339341
const userInfo = getState().user.info;
340342
const userUUID = userInfo.uuid!;
341343

342344
dispatch(getConverses(cb)); // 从服务端获取多人会话列表
343-
rnStorage.get(getUserConversesHash(userUUID)).then(function(converse) {
345+
rnStorage.get(getUserConversesHash(userUUID)).then(function (converse) {
344346
console.log('缓存中的用户会话列表:', converse);
345347
if (converse && converse.length > 0) {
346348
// 如果本地缓存有存在用户会话,则根据上次登录时间获取这段时间内新建的用户会话
@@ -416,7 +418,7 @@ export const sendMsg = function sendMsg(
416418
toUUID: string | null,
417419
payload: SendMsgPayload
418420
): TRPGAction {
419-
return function(dispatch, getState) {
421+
return function (dispatch, getState) {
420422
const info = getState().user.info;
421423
const localUUID = getLocalUUID();
422424
const pkg = {
@@ -432,11 +434,10 @@ export const sendMsg = function sendMsg(
432434
data: payload.data,
433435
uuid: localUUID,
434436
};
435-
console.log('send msg pkg:', pkg);
436437

437438
const converseUUID = payload.converse_uuid || toUUID;
438439
dispatch(addMsg(converseUUID, pkg));
439-
return api.emit('chat::message', pkg, function(data) {
440+
return api.emit('chat::message', pkg, function (data) {
440441
if (data.result) {
441442
// TODO: 待实现SEND_MSG_COMPLETED的数据处理方法(用于送达提示)
442443
dispatch({
@@ -457,10 +458,10 @@ export const sendMsg = function sendMsg(
457458
export const sendFile = function sendFile(toUUID, payload, file): TRPGAction {
458459
if (!file) {
459460
console.error('发送文件错误。没有找到要发送的文件');
460-
return function(dispatch, getState) {};
461+
return function (dispatch, getState) {};
461462
}
462463

463-
return function(dispatch, getState) {
464+
return function (dispatch, getState) {
464465
const info = getState().user.info;
465466
const selfUserUUID = info.uuid!;
466467
const localUUID = getLocalUUID();
@@ -494,7 +495,7 @@ export const sendFile = function sendFile(toUUID, payload, file): TRPGAction {
494495
const filedata = Object.assign({}, pkg.data, fileinfo, { progress: 1 });
495496
pkg = Object.assign({}, pkg, { data: filedata });
496497
// 文件上传完毕。正式发送文件消息
497-
api.emit('chat::message', pkg, function(data) {
498+
api.emit('chat::message', pkg, function (data) {
498499
dispatch({
499500
type: SEND_MSG_COMPLETED,
500501
payload: data,
@@ -543,7 +544,7 @@ export const addFakeMsg = function addFakeMsg(
543544
pkg: Partial<MsgPayload>,
544545
callback?: (localUUID: string) => void
545546
): TRPGAction {
546-
return function(dispatch, getState) {
547+
return function (dispatch, getState) {
547548
const info = getState().user.info;
548549
const localUUID = getLocalUUID();
549550
pkg.uuid = localUUID;
@@ -587,7 +588,7 @@ export const addLoadingMsg = function addLoadingMsg(
587588
converseUUID: string,
588589
cb: (event: LoadingCallbackEvent) => void
589590
): TRPGAction {
590-
return function(dispatch, getState) {
591+
return function (dispatch, getState) {
591592
const fakeMsgPayload: Partial<MsgPayload> = {
592593
message: '[处理中...]',
593594
type: 'loading',
@@ -630,12 +631,12 @@ export const getMoreChatLog = function getMoreChatLog(
630631
offsetDate: string,
631632
isUserChat = true
632633
): TRPGAction {
633-
return function(dispatch, getState) {
634+
return function (dispatch, getState) {
634635
if (isUserChat) {
635636
api.emit(
636637
'chat::getUserChatLog',
637638
{ user_uuid: converseUUID, offsetDate },
638-
function(data) {
639+
function (data) {
639640
if (data.result === true) {
640641
dispatch(updateConversesMsglist(converseUUID, data.list));
641642
dispatch({
@@ -652,7 +653,7 @@ export const getMoreChatLog = function getMoreChatLog(
652653
api.emit(
653654
'chat::getConverseChatLog',
654655
{ converse_uuid: converseUUID, offsetDate },
655-
function(data) {
656+
function (data) {
656657
if (data.result === true) {
657658
dispatch(updateConversesMsglist(converseUUID, data.list));
658659
dispatch({
@@ -669,35 +670,37 @@ export const getMoreChatLog = function getMoreChatLog(
669670
};
670671
};
671672

672-
export const updateCardChatData = function(chatUUID, newData): TRPGAction {
673-
return function(dispatch, getState) {
674-
return api.emit('chat::updateCardChatData', { chatUUID, newData }, function(
675-
data
676-
) {
677-
if (data.result) {
678-
dispatch({
679-
type: UPDATE_SYSTEM_CARD_CHAT_DATA,
680-
chatUUID,
681-
payload: data.log,
682-
});
683-
} else {
684-
console.error(data.msg);
673+
export const updateCardChatData = function (chatUUID, newData): TRPGAction {
674+
return function (dispatch, getState) {
675+
return api.emit(
676+
'chat::updateCardChatData',
677+
{ chatUUID, newData },
678+
function (data) {
679+
if (data.result) {
680+
dispatch({
681+
type: UPDATE_SYSTEM_CARD_CHAT_DATA,
682+
chatUUID,
683+
payload: data.log,
684+
});
685+
} else {
686+
console.error(data.msg);
687+
}
685688
}
686-
});
689+
);
687690
};
688691
};
689692

690693
const getWriteHash = (type: string, uuid: string, groupUUID?: string) => {
691694
return [type, uuid, groupUUID].join('#');
692695
};
693-
export const startWriting = function(
696+
export const startWriting = function (
694697
type = 'user',
695698
uuid: string,
696699
groupUUID?: string,
697700
channelUUID?: string,
698701
currentText?: string
699702
): TRPGAction {
700-
return function(dispatch, getState) {
703+
return function (dispatch, getState) {
701704
dispatch({
702705
type: UPDATE_WRITING_STATUS,
703706
payload: {
@@ -712,14 +715,14 @@ export const startWriting = function(
712715

713716
renewableDelayTimer(
714717
getWriteHash(type, uuid, groupUUID),
715-
function() {
718+
function () {
716719
dispatch(stopWriting(type, uuid, groupUUID)); // 如果在规定时间后没有再次收到正在输入的信号,则视为已经停止输入了
717720
},
718721
config.chat.isWriting.timeout
719722
);
720723
};
721724
};
722-
export const stopWriting = function(
725+
export const stopWriting = function (
723726
type = 'user',
724727
uuid: string,
725728
groupUUID?: string,
@@ -739,9 +742,11 @@ export const stopWriting = function(
739742
};
740743
};
741744

742-
export const getUserEmotion = function(): TRPGAction {
743-
return function(dispatch, getState) {
744-
return api.emit('chatemotion::getUserEmotionCatalog', null, function(data) {
745+
export const getUserEmotion = function (): TRPGAction {
746+
return function (dispatch, getState) {
747+
return api.emit('chatemotion::getUserEmotionCatalog', null, function (
748+
data
749+
) {
745750
if (data.result) {
746751
dispatch({
747752
type: UPDATE_USER_CHAT_EMOTION_CATALOG,
@@ -758,17 +763,17 @@ export const getUserEmotion = function(): TRPGAction {
758763
* 根据表情包暗号添加用户表情包
759764
* @param {string} code 表情包暗号
760765
*/
761-
export const addUserEmotionCatalogWithSecretSignal = function(
766+
export const addUserEmotionCatalogWithSecretSignal = function (
762767
code: string
763768
): TRPGAction {
764-
return function(dispatch, getState) {
769+
return function (dispatch, getState) {
765770
code = String(code).toUpperCase();
766771
return api.emit(
767772
'chatemotion::addUserEmotionWithSecretSignal',
768773
{
769774
code,
770775
},
771-
function(data) {
776+
function (data) {
772777
if (data.result) {
773778
const catalog = data.catalog;
774779
dispatch({
@@ -789,7 +794,7 @@ export const addUserEmotionCatalogWithSecretSignal = function(
789794
* 设定某一会话为已读
790795
* @param converseUUID 会话UUID
791796
*/
792-
export const setConverseIsRead = function(converseUUID: string): TRPGAction {
797+
export const setConverseIsRead = function (converseUUID: string): TRPGAction {
793798
return { type: SET_CONVERSE_ISREAD, converseUUID };
794799
};
795800

0 commit comments

Comments
 (0)