Skip to content

Commit 81b43be

Browse files
committed
Fix scrolling and trim input
1 parent 33554ec commit 81b43be

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

frontend/src/components/Chat/index.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ enum CommunicationEvents {
3030
DISCONNECTED = "disconnected",
3131
}
3232

33-
const Chat: React.FC = () => {
33+
type ChatProps = {
34+
isActive: boolean;
35+
};
36+
37+
const Chat: React.FC<ChatProps> = ({ isActive }) => {
3438
const [messages, setMessages] = useState<Message[]>([]);
3539
const [inputValue, setInputValue] = useState("");
3640
const match = useMatch();
3741
const auth = useAuth();
38-
const endOfMessagesRef = useRef<HTMLDivElement>(null);
42+
const messagesRef = useRef<HTMLDivElement>(null);
3943

4044
if (!match) {
4145
throw new Error(USE_MATCH_ERROR_MESSAGE);
@@ -87,8 +91,14 @@ const Chat: React.FC = () => {
8791
}, []);
8892

8993
useEffect(() => {
90-
endOfMessagesRef.current?.scrollIntoView({ behavior: "smooth" });
91-
}, [messages]);
94+
if (messagesRef.current) {
95+
const nodes = messagesRef.current.querySelectorAll("div > div");
96+
if (nodes.length > 0) {
97+
const lastNode = nodes[nodes.length - 1];
98+
lastNode.scrollIntoView({ behavior: "smooth" });
99+
}
100+
}
101+
}, [messages, isActive]);
92102

93103
return (
94104
<>
@@ -98,6 +108,7 @@ const Chat: React.FC = () => {
98108
overflowY: "auto",
99109
padding: 2,
100110
}}
111+
ref={messagesRef}
101112
>
102113
{messages.map((msg, id) =>
103114
msg.type === "bot_generated" ? (
@@ -166,7 +177,7 @@ const Chat: React.FC = () => {
166177
)
167178
)}
168179
</Box>
169-
<div ref={endOfMessagesRef} />
180+
{/* <div ref={messagesRef} /> */}
170181
<TextField
171182
placeholder="Type message..."
172183
margin="none"
@@ -175,11 +186,12 @@ const Chat: React.FC = () => {
175186
value={inputValue}
176187
onChange={(e) => setInputValue(e.target.value)}
177188
onKeyDown={(e) => {
178-
if (e.key === "Enter" && inputValue !== "") {
189+
const trimmedValue = inputValue.trim();
190+
if (e.key === "Enter" && trimmedValue !== "") {
179191
e.preventDefault();
180192
communicationSocket.emit(CommunicationEvents.SEND_TEXT_MESSAGE, {
181193
roomId: getMatchId(),
182-
message: inputValue,
194+
message: trimmedValue,
183195
username: user?.username,
184196
createdTime: Date.now(),
185197
});

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ const CollabSandbox: React.FC = () => {
195195
<Typography>Tests</Typography>
196196
</TabPanel>
197197
<TabPanel selected={selectedTab} value="chat">
198-
<Chat />
198+
<Chat isActive={selectedTab === "chat"} />
199199
</TabPanel>
200200
</Box>
201201
</Grid2>

0 commit comments

Comments
 (0)