refactor: voice data structure

This commit is contained in:
Tristan Yang
2023-03-26 22:04:28 +08:00
parent 137d045c85
commit b201d5d06d
10 changed files with 83 additions and 68 deletions
+12 -9
View File
@@ -22,7 +22,7 @@ import {
} from "../../types/server";
import { Channel } from "../../types/channel";
import { ContentTypeKey } from "../../types/message";
import { updateVoiceInfo } from "../slices/voice";
import { updateVoiceList } from "../slices/voice";
const defaultExpireDuration = 2 * 24 * 60 * 60;
@@ -128,14 +128,17 @@ export const serverApi = createApi({
try {
const { data: resp } = await queryFulfilled;
const { success } = resp;
if (success && resp.data.channels.length > 0) {
const [channel] = resp.data.channels;
const [id] = channel.channel_name.split(":").slice(-1);
// todo
dispatch(updateVoiceInfo({
id: +id,
context: "channel"
}));
if (success) {
const arr = resp.data.channels.map(data => {
const [id] = data.channel_name.split(":").slice(-1);
const count = data.user_count;
return {
id: +id,
context: "channel" as const,
memberCount: count
};
});
dispatch(updateVoiceList(arr));
}
} catch {
console.error("get voice list error");
+22 -27
View File
@@ -1,22 +1,27 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
export type VoiceInfo = {
export type VoiceBasicInfo = {
context: "channel" | "dm"
id: number | null,
id: number,
}
export type VoicingInfo = {
members: number[]
}
} & VoiceBasicInfo
export type VoiceInfo = {
memberCount: number
} & VoiceBasicInfo
interface State {
joined: boolean,
voicing: VoiceInfo
voicing: VoicingInfo | null,
list: VoiceInfo[]
}
const initialInfo = {
context: "channel" as const,
id: null,
members: []
};
// const initialInfo = {
// context: "channel" as const,
// id: 0,
// members: []
// };
const initialState: State = {
joined: false,
voicing: initialInfo
voicing: null,
list: []
};
@@ -25,21 +30,11 @@ const voiceSlice = createSlice({
name: "voice",
initialState,
reducers: {
updateJoinStatus(state, { payload }: PayloadAction<boolean>) {
state.joined = payload;
updateVoicingInfo(state, { payload }: PayloadAction<VoicingInfo | null>) {
state.voicing = payload;
},
updateVoiceInfo(state, { payload }: PayloadAction<VoiceInfo | null>) {
if (!payload) {
// reset
state.voicing = initialInfo;
} else {
if (state.voicing) {
state.voicing = { ...state.voicing, ...payload };
} else {
state.voicing = payload;
}
}
updateVoiceList(state, { payload }: PayloadAction<VoiceInfo[]>) {
state.list = payload;
},
addVoiceMember(state, { payload }: PayloadAction<number>) {
if (state.voicing) {
@@ -59,5 +54,5 @@ const voiceSlice = createSlice({
},
});
export const { updateJoinStatus, updateVoiceInfo, addVoiceMember, removeVoiceMember } = voiceSlice.actions;
export const { addVoiceMember, removeVoiceMember, updateVoiceList, updateVoicingInfo } = voiceSlice.actions;
export default voiceSlice.reducer;