refactor: lots updates

This commit is contained in:
zerosoul
2022-03-17 11:15:13 +08:00
parent 4bc9932d0f
commit f22c7a01cb
69 changed files with 4625 additions and 1640 deletions
+7 -1
View File
@@ -6,6 +6,12 @@ const initialState = {
expireTime: localStorage.getItem(KEY_EXPIRE) || new Date().getTime(),
refreshToken: localStorage.getItem(KEY_REFRESH_TOKEN),
};
const emptyState = {
uid: null,
token: null,
expireTime: new Date().getTime(),
refreshToken: null,
};
const authDataSlice = createSlice({
name: "authData",
initialState,
@@ -37,7 +43,7 @@ const authDataSlice = createSlice({
localStorage.removeItem(KEY_TOKEN);
localStorage.removeItem(KEY_REFRESH_TOKEN);
localStorage.removeItem(KEY_UID);
return initialState;
return emptyState;
},
setUid(state, action) {
const uid = action.payload;
+6
View File
@@ -1,5 +1,6 @@
import { createSlice } from "@reduxjs/toolkit";
import { getNonNullValues } from "../../common/utils";
import BASE_URL from "../config";
const initialState = {
ids: [],
byId: {},
@@ -37,6 +38,11 @@ const contactsSlice = createSlice({
if (state.byId[uid]) {
Object.keys(vals).forEach((k) => {
state.byId[uid][k] = vals[k];
if (k == "avatar_updated_at") {
state.byId[
uid
].avatar = `${BASE_URL}/resource/avatar?uid=${uid}&t=${vals[k]}`;
}
});
}
}
+25 -1
View File
@@ -2,6 +2,8 @@ import { createSlice } from "@reduxjs/toolkit";
const initialState = {
usersVersion: 0,
afterMid: 0,
readUsers: {},
readChannels: {},
};
const footprintSlice = createSlice({
name: "footprint",
@@ -11,7 +13,13 @@ const footprintSlice = createSlice({
return initialState;
},
fullfillFootprint(state, action) {
return action.payload;
const {
usersVersion = 0,
afterMid = 0,
readUsers = {},
readChannels = {},
} = action.payload;
return { usersVersion, afterMid, readUsers, readChannels };
},
updateUsersVersion(state, action) {
const usersVersion = action.payload;
@@ -21,6 +29,20 @@ const footprintSlice = createSlice({
const afterMid = action.payload;
state.afterMid = afterMid;
},
updateReadUsers(state, action) {
const reads = action.payload || [];
if (reads.length == 0) return;
reads.forEach(({ uid, mid }) => {
state.readUsers[uid] = mid;
});
},
updateReadChannels(state, action) {
const reads = action.payload || [];
if (reads.length == 0) return;
reads.forEach(({ gid, mid }) => {
state.readChannels[gid] = mid;
});
},
},
});
export const {
@@ -28,5 +50,7 @@ export const {
fullfillFootprint,
updateAfterMid,
updateUsersVersion,
updateReadChannels,
updateReadUsers,
} = footprintSlice.actions;
export default footprintSlice.reducer;
+2 -2
View File
@@ -15,9 +15,9 @@ const channelMsgSlice = createSlice({
const { id, mid } = action.payload;
if (state[id]) {
if (state[id].findIndex((id) => id == mid) > -1) return;
state[id].push(mid);
state[id].push(+mid);
} else {
state[id] = [mid];
state[id] = [+mid];
}
},
removeChannelMsg(state, action) {
+4
View File
@@ -22,6 +22,10 @@ const reactionMessageSlice = createSlice({
const idx = reactionUids.findIndex((id) => id == from_uid);
if (idx > -1) {
reactionUids.splice(idx, 1);
if (reactionUids.length == 0) {
// 没有表情了
delete state[mid][reaction];
}
} else {
reactionUids.push(from_uid);
}
+3 -3
View File
@@ -18,10 +18,10 @@ const userMsgSlice = createSlice({
const { id, mid } = action.payload;
if (state.byId[id]) {
if (state.byId[id].findIndex((id) => id == mid) > -1) return;
state.byId[id].push(mid);
state.byId[id].push(+mid);
} else {
state.byId[id] = [mid];
state.ids.push(id);
state.byId[id] = [+mid];
state.ids.push(+id);
}
},
removeUserMsg(state, action) {
+35
View File
@@ -0,0 +1,35 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
inviteLink: {
link: "",
expire: 0,
},
};
const serverSlice = createSlice({
name: "server",
initialState,
reducers: {
resetServer() {
return initialState;
},
fullfillServer(state, action) {
const {
inviteLink = {
link: "",
expire: 0,
},
} = action.payload;
return { inviteLink };
},
updateInviteLink(state, action) {
const { link, expire = 7 * 24 * 60 * 60 } = action.payload;
state.inviteLink = { link, expire };
},
},
});
export const {
resetServer,
fullfillServer,
updateInviteLink,
} = serverSlice.actions;
export default serverSlice.reducer;
+5
View File
@@ -1,6 +1,7 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
online: true,
ready: false,
menuExpand: true,
setting: false,
@@ -13,6 +14,9 @@ const uiSlice = createSlice({
setReady(state) {
state.ready = true;
},
updateOnline(state, action) {
state.online = action.payload;
},
toggleMenuExpand(state) {
state.menuExpand = !state.menuExpand;
},
@@ -28,6 +32,7 @@ const uiSlice = createSlice({
});
export const {
setReady,
updateOnline,
toggleSetting,
toggleMenuExpand,
toggleChannelSetting,