|
1 | 1 | import { ChevronRightIcon } from "@chakra-ui/icons";
|
2 | 2 | import {
|
| 3 | + Avatar, |
3 | 4 | Flex,
|
4 | 5 | IconButton,
|
5 | 6 | Input,
|
6 | 7 | InputGroup,
|
7 | 8 | InputRightElement,
|
8 |
| - VStack, |
9 | 9 | Text,
|
10 |
| - Spacer, |
| 10 | + VStack, |
11 | 11 | HStack,
|
12 |
| - Tag, |
| 12 | + Spacer, |
13 | 13 | } from "@chakra-ui/react";
|
14 | 14 | import { useSharedEditor } from "../../contexts/sharededitor.context";
|
15 | 15 | import { useSelector } from "react-redux";
|
16 | 16 | import { selectUser } from "../../reducers/authSlice";
|
17 |
| -import { useState } from "react"; |
| 17 | +import { useState, useRef, useEffect } from "react"; |
18 | 18 | import { useMatchmake } from "../../contexts/matchmake.context";
|
19 | 19 |
|
20 | 20 | const ChatBox = () => {
|
21 | 21 | const { matchedRoom } = useMatchmake();
|
22 |
| - |
23 | 22 | const { chat, sendToChat } = useSharedEditor();
|
24 | 23 | const user = useSelector(selectUser);
|
25 | 24 | 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 | + |
26 | 39 | const submitMsg = () => {
|
27 | 40 | if (!msg.trim()) return;
|
28 | 41 | sendToChat(msg);
|
29 | 42 | setMsg("");
|
30 | 43 | };
|
| 44 | + |
31 | 45 | if (!matchedRoom) return <></>;
|
32 | 46 | 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 | + > |
35 | 67 | {chat.map((entry, i) => (
|
36 | 68 | <HStack
|
37 | 69 | 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} |
41 | 74 | >
|
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 | + )} |
51 | 107 | </HStack>
|
52 | 108 | ))}
|
53 |
| - </VStack> |
| 109 | + </div> |
54 | 110 | <Spacer />
|
55 | 111 | <InputGroup>
|
56 | 112 | <Input
|
57 | 113 | placeholder="Enter message"
|
58 | 114 | onChange={(e) => setMsg(e.target.value)}
|
59 | 115 | value={msg}
|
| 116 | + variant="filled" |
60 | 117 | />
|
61 | 118 | <InputRightElement>
|
62 | 119 | <IconButton
|
|
0 commit comments