Skip to content

Commit 3a6d724

Browse files
committed
add usernames, fix chat to bottom
1 parent e5d64ce commit 3a6d724

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

Frontend/src/components/collab/Chat.jsx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import React, { useState } from 'react';
2-
import { Accordion, ListGroup, ListGroupItem, Form, Button } from 'react-bootstrap';
1+
import React, {useState, useRef, useEffect} from 'react';
2+
import {Accordion, ListGroup, ListGroupItem, Form, Button, Badge} from 'react-bootstrap';
33

44
const Chat = ({ currentUser, messages, sendMessage }) => {
55
const [text, setText] = useState('');
6+
const chatContainerRef = useRef(null);
7+
const [isAccordionOpen, setIsAccordionOpen] = useState(true);
68

79
const handleSend = () => {
810
console.log("I am sending")
@@ -12,13 +14,27 @@ const Chat = ({ currentUser, messages, sendMessage }) => {
1214
}
1315
};
1416

17+
useEffect(() => {
18+
if (chatContainerRef.current) {
19+
chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight;
20+
}
21+
}, [messages]);
22+
1523
return (
16-
<Accordion defaultActiveKey="0" className='mt-3'>
17-
<Accordion.Item eventKey="0">
18-
<Accordion.Header>
19-
Chat
20-
</Accordion.Header>
21-
<Accordion.Body className="scrollable-accordion-body">
24+
<Accordion defaultActiveKey="0" className='mt-3' onSelect={(eventKey) => setIsAccordionOpen(eventKey === "0")}>
25+
<Accordion.Item eventKey="0">
26+
<Accordion.Header>
27+
Chat
28+
</Accordion.Header>
29+
<Accordion.Body>
30+
<div
31+
ref={chatContainerRef}
32+
style={{
33+
maxHeight: '300px',
34+
overflowY: 'auto',
35+
marginBottom: '1rem',
36+
}}
37+
>
2238
<ListGroup>
2339
{messages.map((msg, idx) => (
2440
<ListGroupItem
@@ -28,11 +44,13 @@ const Chat = ({ currentUser, messages, sendMessage }) => {
2844
textAlign: msg.sender === currentUser ? "right" : "left"
2945
}}
3046
>
31-
{msg.text}
47+
<Badge bg="secondary">{msg.sender}</Badge>: {msg.text}
3248
</ListGroupItem>
3349
))}
3450
</ListGroup>
35-
<Form className="mt-3" onSubmit={(e) => { e.preventDefault(); handleSend(); }}>
51+
</div>
52+
{isAccordionOpen && ( // Only show message input when accordion is open
53+
<Form onSubmit={(e) => { e.preventDefault(); handleSend(); }}>
3654
<Form.Group controlId="messageInput">
3755
<Form.Control
3856
type="text"
@@ -45,8 +63,9 @@ const Chat = ({ currentUser, messages, sendMessage }) => {
4563
Send
4664
</Button>
4765
</Form>
48-
</Accordion.Body>
49-
</Accordion.Item>
66+
)}
67+
</Accordion.Body>
68+
</Accordion.Item>
5069
</Accordion>
5170
);
5271
};

0 commit comments

Comments
 (0)