feat: clear msg from channel (draft)
This commit is contained in:
@@ -95,6 +95,28 @@ export const channelApi = createApi({
|
||||
return `${location.origin}${invite.pathname}${invite.hash}${invite.search}`;
|
||||
}
|
||||
}),
|
||||
clearChannelMessage: builder.query<void, number>({
|
||||
query: (id) => ({
|
||||
url: `/group/${id}/clear`,
|
||||
method: "DELETE"
|
||||
}),
|
||||
async onQueryStarted(id, { dispatch, getState, queryFulfilled }) {
|
||||
const {
|
||||
channelMessage,
|
||||
} = getState() as RootState;
|
||||
try {
|
||||
await queryFulfilled;
|
||||
// 删掉该channel下的所有消息&reaction
|
||||
const mids = channelMessage[id];
|
||||
if (mids) {
|
||||
dispatch(removeMessage(mids));
|
||||
dispatch(removeReactionMessage(mids));
|
||||
}
|
||||
} catch {
|
||||
console.error("clear channel msg error");
|
||||
}
|
||||
}
|
||||
}),
|
||||
removeChannel: builder.query<void, number>({
|
||||
query: (id) => ({
|
||||
url: `/group/${id}`,
|
||||
@@ -204,5 +226,6 @@ export const {
|
||||
useSendChannelMsgMutation,
|
||||
useAddMembersMutation,
|
||||
useRemoveMembersMutation,
|
||||
useUpdateIconMutation
|
||||
useUpdateIconMutation,
|
||||
useLazyClearChannelMessageQuery
|
||||
} = channelApi;
|
||||
|
||||
@@ -13,6 +13,10 @@ const channelMsgSlice = createSlice({
|
||||
resetChannelMsg() {
|
||||
return initialState;
|
||||
},
|
||||
clearChannelMessage(state, action: PayloadAction<number>) {
|
||||
const id = action.payload;
|
||||
state[id] = [];
|
||||
},
|
||||
fillChannelMsg(state, action: PayloadAction<State>) {
|
||||
return action.payload;
|
||||
},
|
||||
@@ -23,7 +27,7 @@ const channelMsgSlice = createSlice({
|
||||
const localMsgExisted = state[id]!.findIndex((id) => id == local_id) > -1;
|
||||
if (midExisted || localMsgExisted) return;
|
||||
// 每次入库,都排序
|
||||
const newArr = [...state[id], +mid].sort((a, b) => a - b);
|
||||
const newArr = [...state[id] as number[], +mid].sort((a, b) => a - b);
|
||||
state[id] = newArr;
|
||||
} else {
|
||||
state[id] = [+mid];
|
||||
@@ -67,7 +71,8 @@ export const {
|
||||
fillChannelMsg,
|
||||
addChannelMsg,
|
||||
removeChannelMsg,
|
||||
replaceChannelMsg
|
||||
replaceChannelMsg,
|
||||
clearChannelMessage
|
||||
} = channelMsgSlice.actions;
|
||||
|
||||
export default channelMsgSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user