refactor: app store and cache

This commit is contained in:
zerosoul
2022-03-18 16:30:38 +08:00
parent ad994f1eaf
commit 7bb8e9a914
26 changed files with 280 additions and 908 deletions
+9
View File
@@ -27,9 +27,18 @@ const channelMsgSlice = createSlice({
state[id].splice(idx, 1);
}
},
removeChannelSession(state, action) {
const ids = Array.isArray(action.payload)
? action.payload
: [action.payload];
ids.forEach((id) => {
delete state[id];
});
},
},
});
export const {
removeChannelSession,
resetChannelMsg,
fullfillChannelMsg,
addChannelMsg,
+20 -5
View File
@@ -1,5 +1,5 @@
import { createSlice } from "@reduxjs/toolkit";
import BASE_URL from "../config";
const initialState = {
replying: {},
};
@@ -28,14 +28,29 @@ const messageSlice = createSlice({
});
},
addMessage(state, action) {
const { mid, sending } = action.payload;
const data = action.payload;
const { mid, sending, content_type, content } = data;
// 如果是正发送,并且已存在,则不覆盖
if (sending && state[mid]) return;
state[mid] = action.payload;
const isImage = content_type.startsWith("image");
if (!sending && isImage) {
data.image_id = content;
data.content = `${BASE_URL}/resource/image?id=${encodeURIComponent(
data.image_id
)}`;
data.thumbnail = `${BASE_URL}/resource/thumbnail?id=${encodeURIComponent(
data.image_id
)}`;
}
state[mid] = data;
},
removeMessage(state, action) {
const mid = action.payload;
delete state[mid];
const mids = Array.isArray(action.payload)
? action.payload
: [action.payload];
mids.forEach((id) => {
delete state[id];
});
},
addReplyingMessage(state, action) {
const { id, mid } = action.payload;
+9
View File
@@ -11,6 +11,14 @@ const reactionMessageSlice = createSlice({
fullfillReactionMessage(state, action) {
return action.payload;
},
removeReactionMessage(state, action) {
const mids = Array.isArray(action.payload)
? action.payload
: [action.payload];
mids.forEach((id) => {
delete state[id];
});
},
toggleReactionMessage(state, action) {
const { from_uid, mid, action: reaction } = action.payload;
console.log("msg reaction", mid, from_uid, reaction);
@@ -36,6 +44,7 @@ const reactionMessageSlice = createSlice({
},
});
export const {
removeReactionMessage,
resetReactionMessage,
fullfillReactionMessage,
toggleReactionMessage,
+14 -2
View File
@@ -1,5 +1,8 @@
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
name: "",
description: "",
logo: "",
inviteLink: {
link: "",
expire: 0,
@@ -18,8 +21,16 @@ const serverSlice = createSlice({
link: "",
expire: 0,
},
} = action.payload;
return { inviteLink };
name = "",
description = "",
} = action.payload || {};
return { name, description, inviteLink };
},
updateInfo(state, action) {
const values = action.payload || {};
Object.keys(values).forEach((_key) => {
state[_key] = values[_key];
});
},
updateInviteLink(state, action) {
const { link, expire = 7 * 24 * 60 * 60 } = action.payload;
@@ -28,6 +39,7 @@ const serverSlice = createSlice({
},
});
export const {
updateInfo,
resetServer,
fullfillServer,
updateInviteLink,