refactor: more TS code

This commit is contained in:
Tristan Yang
2022-07-08 10:18:23 +08:00
parent be6822e568
commit 678b8515ca
36 changed files with 210 additions and 220 deletions
+2 -2
View File
@@ -24,7 +24,7 @@ const channelsSlice = createSlice({
resetChannels() {
return initialState;
},
fullfillChannels(state, action: PayloadAction<Channel[]>) {
fillChannels(state, action: PayloadAction<Channel[]>) {
const channels = action.payload || [];
state.ids = channels.map(({ gid }) => gid);
channels.forEach((c) => {
@@ -113,7 +113,7 @@ const channelsSlice = createSlice({
export const {
updatePinMessage,
resetChannels,
fullfillChannels,
fillChannels,
addChannel,
updateChannel,
removeChannel
+3 -3
View File
@@ -1,4 +1,4 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
// import BASE_URL from "../config";
// todo: check messages type
@@ -13,7 +13,7 @@ const favoritesSlice = createSlice({
name: `favorites`,
initialState,
reducers: {
fullfillFavorites(state, action: PayloadAction<Favorite[]>) {
fillFavorites(state, action: PayloadAction<Favorite[]>) {
return action.payload;
},
addFavorite(state, action: PayloadAction<Favorite>) {
@@ -36,7 +36,7 @@ const favoritesSlice = createSlice({
}
});
export const { addFavorite, deleteFavorite, fullfillFavorites, populateFavorite } =
export const { addFavorite, deleteFavorite, fillFavorites, populateFavorite } =
favoritesSlice.actions;
export default favoritesSlice.reducer;
+12 -19
View File
@@ -1,13 +1,13 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { MuteChannel, MuteUser } from "../../types/sse";
import { MuteDTO } from "../../types/message";
export interface State {
usersVersion: number;
afterMid: number;
readUsers: { [uid: number]: number };
readChannels: { [gid: number]: number };
muteUsers: { [uid: number]: { expired_at: number } | undefined };
muteChannels: { [gid: number]: { expired_at: number } | undefined };
muteUsers: { [uid: number]: { expired_in: number } | undefined };
muteChannels: { [gid: number]: { expired_in: number } | undefined };
}
const initialState: State = {
@@ -19,13 +19,6 @@ const initialState: State = {
muteChannels: {}
};
interface UpdateMutePayload {
remove_users: number[];
remove_groups: number[];
add_users: MuteUser[];
add_groups: MuteChannel[];
}
const footprintSlice = createSlice({
name: "footprint",
initialState,
@@ -33,7 +26,7 @@ const footprintSlice = createSlice({
resetFootprint() {
return initialState;
},
fullfillFootprint(state, action) {
fillFootprint(state, action) {
const {
usersVersion = 0,
afterMid = 0,
@@ -57,35 +50,35 @@ const footprintSlice = createSlice({
updateAfterMid(state, action: PayloadAction<number>) {
state.afterMid = action.payload;
},
updateMute(state, action: PayloadAction<UpdateMutePayload>) {
updateMute(state, action: PayloadAction<MuteDTO>) {
const payload = action.payload || {};
Object.keys(payload).forEach((key) => {
switch (key) {
case "remove_users": {
const uids = payload.remove_users;
uids.forEach((id) => {
uids?.forEach((id) => {
delete state.muteUsers[id];
});
break;
}
case "remove_groups": {
const gids = payload.remove_groups;
gids.forEach((id) => {
gids?.forEach((id) => {
delete state.muteChannels[id];
});
break;
}
case "add_users": {
const mutes = payload.add_users;
mutes.forEach(({ uid, expired_at }) => {
state.muteUsers[uid] = { expired_at };
mutes?.forEach(({ uid, expired_in }) => {
state.muteUsers[uid] = { expired_in };
});
break;
}
case "add_groups": {
const mutes = payload.add_groups;
mutes.forEach(({ gid, expired_at }) => {
state.muteChannels[gid] = { expired_at };
mutes?.forEach(({ gid, expired_in }) => {
state.muteChannels[gid] = { expired_in };
});
break;
}
@@ -113,7 +106,7 @@ const footprintSlice = createSlice({
export const {
resetFootprint,
fullfillFootprint,
fillFootprint,
updateAfterMid,
updateUsersVersion,
updateReadChannels,
+2 -2
View File
@@ -13,7 +13,7 @@ const channelMsgSlice = createSlice({
resetChannelMsg() {
return initialState;
},
fullfillChannelMsg(state, action: PayloadAction<State>) {
fillChannelMsg(state, action: PayloadAction<State>) {
return action.payload;
},
addChannelMsg(state, action: PayloadAction<{ id: number; mid: number; local_id?: any }>) {
@@ -59,7 +59,7 @@ const channelMsgSlice = createSlice({
export const {
removeChannelSession,
resetChannelMsg,
fullfillChannelMsg,
fillChannelMsg,
addChannelMsg,
removeChannelMsg,
replaceChannelMsg
+3 -3
View File
@@ -1,4 +1,4 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
const initialState: number[] = [];
@@ -9,7 +9,7 @@ const fileMessageSlice = createSlice({
resetFileMessage() {
return initialState;
},
fullfillFileMessage(state, action: PayloadAction<number[]>) {
fillFileMessage(state, action: PayloadAction<number[]>) {
return action.payload || [];
},
addFileMessage(state, action: PayloadAction<number>) {
@@ -33,7 +33,7 @@ const fileMessageSlice = createSlice({
}
});
export const { removeFileMessage, resetFileMessage, fullfillFileMessage, addFileMessage } =
export const { removeFileMessage, resetFileMessage, fillFileMessage, addFileMessage } =
fileMessageSlice.actions;
export default fileMessageSlice.reducer;
+8 -6
View File
@@ -1,10 +1,12 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
// todo: check entity type
export interface State {
[mid: number]: {
[reaction: number]: number[]
} | undefined;
[mid: number]:
| {
[reaction: number]: number[];
}
| undefined;
}
const initialState: State = {};
@@ -15,7 +17,7 @@ const reactionMessageSlice = createSlice({
resetReactionMessage() {
return initialState;
},
fullfillReactionMessage(state, action: PayloadAction<State>) {
fillReactionMessage(state, action: PayloadAction<State>) {
return action.payload;
},
removeReactionMessage(state, action: PayloadAction<number | number[]>) {
@@ -59,7 +61,7 @@ const reactionMessageSlice = createSlice({
export const {
removeReactionMessage,
resetReactionMessage,
fullfillReactionMessage,
fillReactionMessage,
toggleReactionMessage
} = reactionMessageSlice.actions;
+2 -2
View File
@@ -17,7 +17,7 @@ const messageSlice = createSlice({
resetMessage() {
return initialState;
},
fullfillMessage(state, action) {
fillMessage(state, action) {
return Object.assign({ ...initialState }, action.payload);
},
updateMessage(state, action) {
@@ -75,7 +75,7 @@ const messageSlice = createSlice({
export const {
resetMessage,
fullfillMessage,
fillMessage,
setMessage,
updateMessage,
addMessage,
+9 -13
View File
@@ -1,9 +1,9 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
export interface State {
ids: string[];
ids: number[];
// todo: check object type
byId: { [id: number]: any; };
byId: { [id: number]: any };
}
const initialState: State = {
@@ -18,17 +18,16 @@ const userMsgSlice = createSlice({
resetUserMsg() {
return initialState;
},
fullfillUserMsg(state, action: PayloadAction<{ [id: string]: any; }>) {
fillUserMsg(state, action: PayloadAction<{ [id: number]: any }>) {
state.ids = Object.keys(action.payload);
state.byId = action.payload;
},
addUserMsg(state, action) {
addUserMsg(state, action: PayloadAction<{ id: number; mid: number; local_id: number }>) {
const { id, mid, local_id } = action.payload;
if (state.byId[id]) {
const midExsited = state.byId[id].findIndex((id) => id == mid) > -1;
const localMsgExsited = state.byId[id].findIndex((id) => id == local_id) > -1;
if (midExsited || localMsgExsited) return;
const midExisted = state.byId[id].findIndex((id) => id == mid) > -1;
const localMsgExisted = state.byId[id].findIndex((id) => id == local_id) > -1;
if (midExisted || localMsgExisted) return;
state.byId[id].push(+mid);
// 只要有新消息,就检查下
if (state.ids.findIndex((uid) => uid == id) == -1) {
@@ -62,16 +61,13 @@ const userMsgSlice = createSlice({
removeUserSession(state, action) {
const ids = Array.isArray(action.payload) ? action.payload : [action.payload];
state.ids = state.ids.filter((id) => ids.findIndex((i) => i == id) == -1);
// ids.forEach((id) => {
// delete state.byId[id];
// });
}
}
});
export const {
removeUserSession,
resetUserMsg,
fullfillUserMsg,
fillUserMsg,
addUserMsg,
removeUserMsg,
replaceUserMsg
+1 -1
View File
@@ -1,5 +1,5 @@
reset
fullfill
fill
set
add
update
+2 -2
View File
@@ -25,7 +25,7 @@ const serverSlice = createSlice({
resetServer() {
return initialState;
},
fullfillServer(state, action: PayloadAction<StoredServer>) {
fillServer(state, action: PayloadAction<StoredServer>) {
const {
inviteLink = {
link: "",
@@ -51,5 +51,5 @@ const serverSlice = createSlice({
}
});
export const { updateInfo, resetServer, fullfillServer } = serverSlice.actions;
export const { updateInfo, resetServer, fillServer } = serverSlice.actions;
export default serverSlice.reducer;
+6 -3
View File
@@ -39,7 +39,7 @@ const uiSlice = createSlice({
name: "ui",
initialState,
reducers: {
fullfillUI(state, action) {
fillUI(state, action) {
return { ...initialState, ...action.payload };
},
setReady(state) {
@@ -57,7 +57,10 @@ const uiSlice = createSlice({
updateFileListView(state, action) {
state.fileListView = action.payload;
},
updateRememberedNavs(state, action: PayloadAction<{ key?: string; path: string | null }>) {
updateRememberedNavs(
state,
action: PayloadAction<{ key?: "chat" | "user"; path: string | null } | undefined>
) {
const { key = "chat", path = null } = action.payload || {};
state.rememberedNavs[key] = path;
},
@@ -155,7 +158,7 @@ const uiSlice = createSlice({
});
export const {
fullfillUI,
fillUI,
setReady,
updateOnline,
updateInputMode,
+2 -3
View File
@@ -26,7 +26,7 @@ const usersSlice = createSlice({
resetUsers() {
return initialState;
},
fullfillUsers(state, action: PayloadAction<StoredUser[]>) {
fillUsers(state, action: PayloadAction<StoredUser[]>) {
const users = action.payload || [];
state.ids = users.map(({ uid }) => uid);
state.byId = Object.fromEntries(
@@ -99,6 +99,5 @@ const usersSlice = createSlice({
}
});
export const { resetUsers, fullfillUsers, updateUsersByLogs, updateUsersStatus } =
usersSlice.actions;
export const { resetUsers, fillUsers, updateUsersByLogs, updateUsersStatus } = usersSlice.actions;
export default usersSlice.reducer;