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' ;
3
3
4
4
const Chat = ( { currentUser, messages, sendMessage } ) => {
5
5
const [ text , setText ] = useState ( '' ) ;
6
+ const chatContainerRef = useRef ( null ) ;
7
+ const [ isAccordionOpen , setIsAccordionOpen ] = useState ( true ) ;
6
8
7
9
const handleSend = ( ) => {
8
10
console . log ( "I am sending" )
@@ -12,27 +14,61 @@ const Chat = ({ currentUser, messages, sendMessage }) => {
12
14
}
13
15
} ;
14
16
17
+ useEffect ( ( ) => {
18
+ if ( chatContainerRef . current ) {
19
+ chatContainerRef . current . scrollTop = chatContainerRef . current . scrollHeight ;
20
+ }
21
+ } , [ messages ] ) ;
22
+
15
23
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
+ >
22
38
< ListGroup >
23
39
{ messages . map ( ( msg , idx ) => (
24
- < ListGroupItem
25
- key = { idx }
26
- style = { {
27
- backgroundColor : msg . sender === currentUser ? "#d1e7dd" : "#50d7da" ,
28
- textAlign : msg . sender === currentUser ? "right" : "left"
29
- } }
30
- >
31
- { msg . text }
32
- </ ListGroupItem >
40
+ < >
41
+ < ListGroupItem
42
+ key = { idx }
43
+ style = { {
44
+ backgroundColor : msg . sender === currentUser ? "#d1e7dd" : "#50d7da" ,
45
+ textAlign : msg . sender === currentUser ? "right" : "left"
46
+ } }
47
+ >
48
+ { /* alternative: keep badge within same line*/ }
49
+ { /*{msg.sender !== currentUser && <><Badge bg="secondary">{msg.sender}</Badge> {msg.text}</> }*/ }
50
+ { /*{msg.sender === currentUser && <>{msg.text} <Badge bg="secondary">{msg.sender}</Badge></> }*/ }
51
+
52
+ { msg . text }
53
+ </ ListGroupItem >
54
+ < ListGroupItem
55
+ key = { idx }
56
+ style = { {
57
+ backgroundColor : "white" ,
58
+ textAlign : msg . sender === currentUser ? "right" : "left" ,
59
+ border : "none" ,
60
+ padding : "0" ,
61
+ } } >
62
+ < Badge bg = "secondary" > { msg . sender } </ Badge >
63
+ </ ListGroupItem >
64
+ { /* spacer */ }
65
+ < div style = { { height : '10px' } } > </ div >
66
+ </ >
33
67
) ) }
34
68
</ ListGroup >
35
- < Form className = "mt-3" onSubmit = { ( e ) => { e . preventDefault ( ) ; handleSend ( ) ; } } >
69
+ </ div >
70
+ { isAccordionOpen && ( // Only show message input when accordion is open
71
+ < Form onSubmit = { ( e ) => { e . preventDefault ( ) ; handleSend ( ) ; } } >
36
72
< Form . Group controlId = "messageInput" >
37
73
< Form . Control
38
74
type = "text"
@@ -45,8 +81,9 @@ const Chat = ({ currentUser, messages, sendMessage }) => {
45
81
Send
46
82
</ Button >
47
83
</ Form >
48
- </ Accordion . Body >
49
- </ Accordion . Item >
84
+ ) }
85
+ </ Accordion . Body >
86
+ </ Accordion . Item >
50
87
</ Accordion >
51
88
) ;
52
89
} ;
0 commit comments