Skip to content

Commit 6c70e04

Browse files
committed
Refactor chat components: update unread message handling in ChatItem and ChatList, introduce ChatRoomWithNotifications type, and implement utility for managing unique messages in chatSlice
1 parent 9b89d72 commit 6c70e04

5 files changed

Lines changed: 52 additions & 23 deletions

File tree

frontend/src/components/chats/ChatItem.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,7 @@ export default function ChatRooms({
5454
</div>
5555
<p className="text-sm text-gray-500 truncate">{lastMessage}</p>
5656
</div>
57-
{unread > 0 && (
58-
<Badge
59-
className="rounded-full px-2 py-1"
60-
style={{ width: 23, height: 23 }}
61-
>
62-
{unread}
63-
</Badge>
64-
)}
57+
{unread > 0 && <Badge className="rounded-full px-2 py-1">new</Badge>}
6558
</div>
6659
);
6760
}

frontend/src/components/chats/ChatList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function ChatList() {
2222
fallbackColorGen: chat.id,
2323
},
2424
timestamp: "",
25-
unread: 0,
25+
unread: chat.unreadMessages ?? 0,
2626
};
2727
});
2828
});

frontend/src/statemanagement/chats/chatSlice.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ChatMessage } from "@/types/chatmessage.type";
2-
import { ChatRoom } from "@/types/chatroom.type";
3-
import { unique } from "@/utils/unique_array";
2+
import { ChatRoom, ChatRoomWithNotifications } from "@/types/chatroom.type";
3+
import { createUniqueMessages } from "@/utils/unreadMessages";
44
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
55
export interface ChatState {
6-
chats: ChatRoom[];
6+
chats: ChatRoomWithNotifications[];
77
chatsAreLoading: boolean;
88
chatIsLoading: boolean;
99
messageSending: boolean;
@@ -17,6 +17,21 @@ const initialState: ChatState = {
1717
messageSending: false,
1818
};
1919

20+
const setMessagesForChat = (
21+
chat: ChatRoomWithNotifications,
22+
activeChatId?: string,
23+
messagesToAdd?: ChatMessage[]
24+
) => {
25+
const { messages, noNewMessages } = createUniqueMessages(
26+
chat.messages,
27+
messagesToAdd
28+
);
29+
chat.messages = messages;
30+
if (activeChatId != chat.id) {
31+
chat.unreadMessages = noNewMessages;
32+
}
33+
};
34+
2035
export const getAllChats = createAsyncThunk("chats/getAll", async (_, s) => {
2136
let token = window.sessionStorage.getItem("token");
2237
const response = await fetch(
@@ -30,8 +45,10 @@ export const getAllChats = createAsyncThunk("chats/getAll", async (_, s) => {
3045
if (response.status > 299) {
3146
throw new Error("Request failed with " + response.status);
3247
}
33-
const data = await response.json();
34-
console.log(data);
48+
const data = (await response.json()) as ChatRoomWithNotifications[];
49+
data.forEach((chat) => {
50+
chat.unreadMessages = 0;
51+
});
3552
if (data.length > 0) {
3653
s.dispatch(getChatById({ chatId: data[0].id }));
3754
}
@@ -103,16 +120,21 @@ export const chatSlice = createSlice({
103120
reducers: {
104121
setActiveChatId: (state, action: { payload: string }) => {
105122
state.activeChatId = action.payload;
123+
const chat = state.chats.find((x) => x.id == action.payload);
124+
if (chat != null) {
125+
chat.unreadMessages = 0;
126+
}
106127
},
107128
updateChatRoomMessagesSync: (state, action: { payload: ChatRoom }) => {
108129
const chatRoom = state.chats.find((x) => x.id == action.payload.id);
109130
if (chatRoom != null) {
110-
chatRoom.messages = unique(
111-
[...(chatRoom.messages ?? []), ...(action.payload.messages ?? [])],
112-
(x) => x.id
131+
setMessagesForChat(
132+
chatRoom,
133+
state.activeChatId,
134+
action.payload.messages
113135
);
114136
} else {
115-
state.chats.push(action.payload);
137+
state.chats.push({ ...action.payload, unreadMessages: 1 });
116138
}
117139
},
118140
},
@@ -142,11 +164,7 @@ export const chatSlice = createSlice({
142164
state.chatIsLoading = false;
143165
const chat = state.chats.find((x) => x.id == action.payload.chatId);
144166
if (chat != null) {
145-
const prevMessages = chat.messages ?? [];
146-
chat.messages = unique(
147-
[...prevMessages, ...action.payload.messages],
148-
(x) => x.id
149-
);
167+
setMessagesForChat(chat, state.activeChatId, action.payload.messages);
150168
}
151169
}
152170
);

frontend/src/types/chatroom.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ export interface ChatRoom {
77
messages?: ChatMessage[];
88
isLoading?: boolean;
99
}
10+
11+
export interface ChatRoomWithNotifications extends ChatRoom {
12+
unreadMessages?: number;
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ChatMessage } from "@/types/chatmessage.type";
2+
import { unique } from "./unique_array";
3+
4+
export const createUniqueMessages = (
5+
oldMessages?: ChatMessage[],
6+
newMessages?: ChatMessage[]
7+
) => {
8+
const messages = unique(
9+
[...(oldMessages ?? []), ...(newMessages ?? [])],
10+
(x) => x.id
11+
);
12+
const noNewMessages = messages.length - (oldMessages?.length ?? 0);
13+
return { messages, noNewMessages };
14+
};

0 commit comments

Comments
 (0)