Skip to content

Commit 14726cb

Browse files
authored
Merge pull request #56 from Gavzzz/master
Nice to have: Edited ChatBox.component.tsx
2 parents 87e4747 + c2e6571 commit 14726cb

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

frontend/src/components/ChatBox/ChatBox.component.tsx

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,119 @@
11
import { ChevronRightIcon } from "@chakra-ui/icons";
22
import {
3+
Avatar,
34
Flex,
45
IconButton,
56
Input,
67
InputGroup,
78
InputRightElement,
8-
VStack,
99
Text,
10-
Spacer,
10+
VStack,
1111
HStack,
12-
Tag,
12+
Spacer,
1313
} from "@chakra-ui/react";
1414
import { useSharedEditor } from "../../contexts/sharededitor.context";
1515
import { useSelector } from "react-redux";
1616
import { selectUser } from "../../reducers/authSlice";
17-
import { useState } from "react";
17+
import { useState, useRef, useEffect } from "react";
1818
import { useMatchmake } from "../../contexts/matchmake.context";
1919

2020
const ChatBox = () => {
2121
const { matchedRoom } = useMatchmake();
22-
2322
const { chat, sendToChat } = useSharedEditor();
2423
const user = useSelector(selectUser);
2524
const [msg, setMsg] = useState("");
25+
const chatContainerRef = useRef<HTMLDivElement | null>(null);
26+
27+
useEffect(() => {
28+
const chatContainer = chatContainerRef.current;
29+
if (chatContainer) {
30+
// Set scrollTop to the maximum value to keep the scroll bar at the bottom
31+
chatContainer.scrollTop = chatContainer.scrollHeight;
32+
33+
// Set scrollLeft to the maximum value to keep the scroll bar at the right
34+
chatContainer.scrollLeft =
35+
chatContainer.scrollWidth - chatContainer.clientWidth;
36+
}
37+
}, [chat]);
38+
2639
const submitMsg = () => {
2740
if (!msg.trim()) return;
2841
sendToChat(msg);
2942
setMsg("");
3043
};
44+
3145
if (!matchedRoom) return <></>;
3246
return (
33-
<Flex flexDirection="column" width="100%" height="100%" alignItems="start">
34-
<VStack flexDirection={"column-reverse"} flex={1} width="100%">
47+
<Flex
48+
flexDirection="column"
49+
width="400px"
50+
height="400px"
51+
alignItems="start"
52+
border="1px solid #ccc"
53+
borderRadius="md"
54+
padding="10px"
55+
>
56+
<div
57+
ref={chatContainerRef}
58+
style={{
59+
overflowY: "auto",
60+
overflowX: "hidden",
61+
backgroundColor: "gray.100",
62+
borderRadius: "md",
63+
padding: "10px",
64+
width: "100%", // Use 100% width
65+
}}
66+
>
3567
{chat.map((entry, i) => (
3668
<HStack
3769
w="100%"
38-
direction={
39-
entry.nickname === user?.username ? "column-reverse" : "column"
40-
}
70+
justifyContent={
71+
entry.nickname === user?.username ? "flex-end" : "flex-start"
72+
} // Align based on the sender
73+
key={i}
4174
>
42-
<Tag>
43-
<Text>{entry.nickname}</Text>
44-
</Tag>
45-
46-
<Tag>
47-
<Text>{entry.msg}</Text>
48-
</Tag>
49-
50-
<Spacer />
75+
{entry.nickname !== user?.username && (
76+
<Avatar
77+
name={entry.nickname}
78+
w="40px"
79+
h="40px"
80+
alignSelf="flex-start"
81+
/>
82+
)}
83+
<Text
84+
backgroundColor={
85+
entry.nickname === user?.username ? "blue.400" : "gray.200"
86+
}
87+
color={entry.nickname === user?.username ? "white" : "black"}
88+
borderRadius="lg"
89+
paddingX={2}
90+
paddingY={1}
91+
style={{
92+
whiteSpace: "pre-wrap",
93+
wordWrap: "break-word",
94+
maxWidth: "70%",
95+
}}
96+
>
97+
{entry.msg}
98+
</Text>
99+
{entry.nickname === user?.username && (
100+
<Avatar
101+
name={entry.nickname}
102+
w="40px"
103+
h="40px"
104+
alignSelf="flex-start"
105+
/>
106+
)}
51107
</HStack>
52108
))}
53-
</VStack>
109+
</div>
54110
<Spacer />
55111
<InputGroup>
56112
<Input
57113
placeholder="Enter message"
58114
onChange={(e) => setMsg(e.target.value)}
59115
value={msg}
116+
variant="filled"
60117
/>
61118
<InputRightElement>
62119
<IconButton

0 commit comments

Comments
 (0)