Spaces:
Paused
Paused
File size: 6,494 Bytes
a0fda44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
import React, { useEffect, useRef } from "react";
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import useSocket from "../../../hooks/useSocket";
import useChatBot from "../../../hooks/useChatBot";
import { chatListActions } from "../../../store/chatListSlice";
import { chatActions } from "../../../store/chatSlice";
import { userActions } from "../../../store/userSlice";
import CTAIconWrapper from "../../globals/CTAIconWrapper";
import DayMessages from "./DayMessages";
function MessageList({ messageHistory }) {
const [scrolledUp, setScrolledUp] = useState(false);
const [chatUpdated, setChatUpdated] = useState(false);
const [chatRoomChanged, setChatRoomChanged] = useState(true);
const currentChatRoomId = useSelector(
(state) => state.chatReducer.currentChatRoom._id
);
const chatWithBot = useSelector(
(state) =>
state.chatReducer?.currentChatRoom.chatProfile.username ===
process.env.REACT_APP_BOT_USERNAME
);
const chatActive = useSelector((state) => state.chatReducer.active);
const messageListRef = useRef();
const { socketListen, userId, socketEmit, socket } = useSocket();
const dispatch = useDispatch();
const { getResponseFromChatBot } = useChatBot();
// When user leaves chat modal, deactivate scrolledUp icon appearance and set intial
useEffect(() => {
setScrolledUp(false);
}, [chatRoomChanged]);
// Listen to incoming messages and emit delivered on receiving
useEffect(() => {
socketListen(
"user:message",
({ chatRoomId, message }, acknowledgeReceiving) => {
// Acknowledge receiving message by sending userId back to server
acknowledgeReceiving(userId);
dispatch(chatActions.updateMessageHistory({ chatRoomId, message }));
dispatch(
chatListActions.setLatestMessage({
chatRoomId,
latestMessage: message,
})
);
// Set message mode to initial
dispatch(chatActions.resetMode());
setChatUpdated(true);
}
);
}, []);
// Listen to when a message can be read
useEffect(() => {
setChatRoomChanged(true);
socketListen("user:messageCanBeRead", ({ chatRoomId, day, message }) => {
// If message was sent to chat room that belongs with chat bot, get response from chat bot
if (chatWithBot && message.sender === userId) {
getResponseFromChatBot({ chatRoomId, userMessage: message.message });
}
// If user is the sender of the message
if (userId === message.sender) return;
// If message chatRoom is the currentChatRoom being displayed, emit message as being read
if (chatRoomId === currentChatRoomId && chatActive) {
socketEmit("user:messageRead", {
messageId: message._id,
chatRoomId,
day,
userId,
});
} else {
dispatch(
userActions.setUnreadMessage({
chatRoomId,
day,
messageId: message._id,
})
);
dispatch(
chatListActions.IncreaseMessageCountInChatRoom({ chatRoomId })
);
}
});
return () => {
socket.off("user:messageCanBeRead");
};
}, [currentChatRoomId, chatActive]);
// Listen to messages that has been delivered or read by all members
useEffect(() => {
socketListen(
"user:messageDelivered",
({ chatRoomId, messageId, senderId, day }) => {
dispatch(
chatListActions.updateMessageStatus({
messageId,
chatRoomId,
status: "deliveredStatus",
})
);
// If message wasn't sent by user, there's no need to update
if (userId !== senderId) return;
dispatch(
chatActions.updateMessageStatus({
chatRoomId,
messageId,
day,
status: "deliveredStatus",
})
);
}
);
socketListen(
"user:messageReadByAllMembers",
({ chatRoomId, messageId, senderId, day }) => {
dispatch(
chatListActions.updateMessageStatus({
messageId,
chatRoomId,
status: "readStatus",
})
);
// If message wasn't sent by user, there's no need to update
if (userId !== senderId) return;
dispatch(
chatActions.updateMessageStatus({
chatRoomId,
messageId,
day,
status: "readStatus",
})
);
}
);
}, []);
// Manages message scrolling down to the bottom
useEffect(() => {
const behavior = chatRoomChanged ? "auto" : "smooth";
if (chatUpdated || chatRoomChanged) {
messageListRef.current.scrollTo({
top: messageListRef.current.scrollHeight,
left: 0,
behavior,
});
}
if (chatRoomChanged) {
setChatRoomChanged(false);
}
if (chatUpdated) {
setChatUpdated(false);
}
}, [chatUpdated, chatRoomChanged]);
return (
<div
ref={messageListRef}
onScroll={({ currentTarget }) => {
if (currentTarget.scrollHeight - currentTarget.scrollTop >= 1000) {
setScrolledUp(true);
} else {
setScrolledUp(false);
}
}}
className="w-full overflow-y-scroll no-scrollbar py-[1rem]"
>
{!!messageHistory.length &&
messageHistory.map((messagesData) => (
<DayMessages key={messagesData.day} messagesData={messagesData} />
))}
{/* Scroll down */}
{scrolledUp && (
<CTAIconWrapper
onClick={() =>
messageListRef.current.scrollTo({
top: messageListRef.current.scrollHeight,
left: 0,
behavior: "smooth",
})
}
className="absolute !bg-primary bottom-[8.5rem] right-0"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
height="1em"
preserveAspectRatio="xMidYMid meet"
viewBox="0 0 24 24"
>
<path
fill="none"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M12 20V4m-7 9l7 7l7-7"
className="fill-transparent"
/>
</svg>
</CTAIconWrapper>
)}
</div>
);
}
export default MessageList;
|