From c35a7d211bc327a2f05d1ed2f33130b2e1c0ed1a Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Fri, 31 Mar 2023 12:59:54 +0800 Subject: [PATCH] refactor: upsertVoiceList --- src/app/services/server.ts | 4 ++-- src/app/slices/voice.ts | 16 +++++++++++++--- src/common/component/Voice.tsx | 7 ++++++- src/routes/chat/VoiceChat/index.tsx | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/app/services/server.ts b/src/app/services/server.ts index 6451ea07..d8c189d4 100644 --- a/src/app/services/server.ts +++ b/src/app/services/server.ts @@ -22,7 +22,7 @@ import { } from "../../types/server"; import { Channel } from "../../types/channel"; import { ContentTypeKey } from "../../types/message"; -import { updateVoiceList } from "../slices/voice"; +import { upsertVoiceList } from "../slices/voice"; const defaultExpireDuration = 2 * 24 * 60 * 60; @@ -139,7 +139,7 @@ export const serverApi = createApi({ memberCount: count }; }); - dispatch(updateVoiceList(arr)); + dispatch(upsertVoiceList(arr)); } } catch { console.error("get voice list error"); diff --git a/src/app/slices/voice.ts b/src/app/slices/voice.ts index 34a6567c..8930a5d9 100644 --- a/src/app/slices/voice.ts +++ b/src/app/slices/voice.ts @@ -93,8 +93,18 @@ const voiceSlice = createSlice({ state.voicing.downlinkNetworkQuality = payload; } }, - updateVoiceList(state, { payload }: PayloadAction) { - state.list = payload; + upsertVoiceList(state, { payload }: PayloadAction) { + if (Array.isArray(payload)) { + state.list = payload; + } else { + const { id, context } = payload; + const idx = state.list.findIndex(v => v.id == id && v.context == context); + if (idx > -1) { + state.list.splice(idx, 1, payload); + } else { + state.list.push(payload); + } + } }, addVoiceMember(state, { payload }: PayloadAction) { const notExisted = !state.voicingMembers.ids.includes(payload); @@ -124,5 +134,5 @@ const voiceSlice = createSlice({ }, }); -export const { addVoiceMember, removeVoiceMember, updateVoiceList, updateVoicingInfo, updateVoicingNetworkQuality, updateMuteStatus, updateVoicingMember, updateDeafenStatus } = voiceSlice.actions; +export const { addVoiceMember, removeVoiceMember, upsertVoiceList, updateVoicingInfo, updateVoicingNetworkQuality, updateMuteStatus, updateVoicingMember, updateDeafenStatus } = voiceSlice.actions; export default voiceSlice.reducer; diff --git a/src/common/component/Voice.tsx b/src/common/component/Voice.tsx index 54528c13..2fd8a52f 100644 --- a/src/common/component/Voice.tsx +++ b/src/common/component/Voice.tsx @@ -3,7 +3,7 @@ import { useEffect } from 'react'; import { useDispatch } from 'react-redux'; import { useGetAgoraConfigQuery, useGetAgoraVoicingListQuery, useLazyGetAgoraTokenQuery } from '../../app/services/server'; import { updateChannelVisibleAside } from '../../app/slices/channels'; -import { addVoiceMember, removeVoiceMember, updateDeafenStatus, updateMuteStatus, updateVoicingInfo, updateVoicingMember, updateVoicingNetworkQuality } from '../../app/slices/voice'; +import { addVoiceMember, removeVoiceMember, updateDeafenStatus, updateMuteStatus, updateVoicingInfo, updateVoicingMember, updateVoicingNetworkQuality, upsertVoiceList } from '../../app/slices/voice'; import { useAppSelector } from '../../app/store'; // type Props = {} @@ -187,6 +187,11 @@ const useVoice = ({ id, context = "channel" }: VoiceProps) => { dispatch(updateChannelVisibleAside({ id, aside: null })); + dispatch(upsertVoiceList({ + id, + context, + memberCount: 0 + })); } } }; diff --git a/src/routes/chat/VoiceChat/index.tsx b/src/routes/chat/VoiceChat/index.tsx index 992dd390..90dfc9ef 100644 --- a/src/routes/chat/VoiceChat/index.tsx +++ b/src/routes/chat/VoiceChat/index.tsx @@ -56,7 +56,7 @@ const VoiceChat = ({ id, context = "channel" }: Props) => {
  • {visible ? null : memberCount > 0 ? {memberCount} : }