Skip to content

Commit 9c8d14f

Browse files
committed
Refactored messages state from an Array to a Map
1 parent 153c7e1 commit 9c8d14f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

client/app/components/chat/MessageChannel.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import Message from "./ChatMessage";
55
import { MessageChannelProps } from "../../types/Props";
66
import { TouchableOpacity } from "react-native";
77
import SheetModal from "./SheetModal";
8+
import { UserProfile } from "../../types/User";
89

910
const MessageChannel: React.FC<MessageChannelProps> = ({
1011
nearbyUsers,
1112
messages,
1213
}) => {
1314
const [isModalVisible, setModalVisible] = useState(false);
14-
const reverseMessages = [...messages].reverse();
1515

16+
const messagesArray = Array.from(messages.values())
17+
.sort((a, b) => b.timestamp - a.timestamp);
1618

1719
const handleLongPress = () => {
1820
setModalVisible(true);
@@ -28,11 +30,17 @@ const MessageChannel: React.FC<MessageChannelProps> = ({
2830
contentContainerStyle={{
2931
width: "100%",
3032
}}
31-
data={reverseMessages}
33+
data={messagesArray}
3234
renderItem={({ item }) => {
33-
const user = nearbyUsers[item.author];
34-
// console.log(nearbyUsers);
35-
if (user === undefined) return null;
35+
const user: UserProfile = nearbyUsers[item.author];
36+
// Mock data for testing when socket server isn't working
37+
// const user: UserProfile = { displayName: "You", profilePicture: 1 };
38+
if (user === undefined) {
39+
console.log(
40+
`Message recieved from user not in nearbyUsers (${item.author})`
41+
);
42+
return null;
43+
}
3644
return (
3745
<TouchableOpacity onLongPress={ handleLongPress }>
3846
<Message

client/app/screens/chat/ChatScreen.tsx

+16-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const ChatScreen = () => {
3535
// Note: To prevent complexity, all user information is grabbed from different contexts and services. If we wanted most information inside of UserContext, we would have to import contexts within contexts and have state change as certain things mount, which could cause errors that are difficult to pinpoint.
3636

3737
// Message loading and sending logic
38-
const [messages, setMessages] = useState<Message[]>([]);
38+
const [messages, setMessages] = useState<Map<string, Message>>(new Map());
3939
const [messageContent, setMessageContent] = useState<string>("");
4040

4141
useEffect(() => {
@@ -50,7 +50,11 @@ const ChatScreen = () => {
5050
);
5151
await refreshNearbyUsers(socket);
5252
}
53-
setMessages((prevMessages) => [...prevMessages, message]);
53+
setMessages((prevMessages) => {
54+
const newMessages = new Map(prevMessages);
55+
newMessages.set(message.id, message);
56+
return newMessages;
57+
});
5458
if (ack) console.log("Server acknowledged message:", ack);
5559
};
5660

@@ -67,9 +71,9 @@ const ChatScreen = () => {
6771
if (socket === null) return;
6872

6973
const newMessage: Message = {
74+
id: Crypto.randomUUID(),
7075
author: String(userAuth.userAuthInfo?.uid),
71-
//msgId: Crypto.randomUUID(),
72-
timestamp: -1, // timestamp will be overridden by socket server
76+
timestamp: Date.now(), // timestamp will be overridden by server
7377
content: { text: messageContent.trim() },
7478
location: {
7579
lat: Number(location?.lat),
@@ -78,9 +82,17 @@ const ChatScreen = () => {
7882
replyTo: undefined,
7983
reactions: {},
8084
};
85+
console.log(`[LOG] New message: ${newMessage.author
86+
} - ${newMessage.content.text} (${newMessage.id})`);
8187
sendMessage(socket, newMessage);
8288

8389
setMessageContent("");
90+
// Optimistic UI update for testing when socket server isn't working
91+
// setMessages((prevMessages) => {
92+
// const newMessages = new Map(prevMessages);
93+
// newMessages.set(newMessage.id, newMessage);
94+
// return newMessages;
95+
// });
8496
};
8597

8698
return (

client/app/types/Props.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type MessageProps = {
2828

2929
export type MessageChannelProps = {
3030
nearbyUsers: { [uid: string]: UserProfile };
31-
messages: Message[];
31+
messages: Map<string, Message>;
3232
};
3333

3434
export type SafeAreaWrapperProps = {

0 commit comments

Comments
 (0)