Skip to content

Commit 14c9813

Browse files
committed
wip chat users
1 parent efd04f3 commit 14c9813

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

src/main/client/src/component/Chat.jsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import {
55
useContext,
66
useRef,
77
} from "react"
8-
import {
9-
useParams,
10-
} from "react-router-dom"
118
import {
129
useAuthStore,
1310
} from "../store.js"
@@ -19,7 +16,6 @@ import {
1916

2017
export const Chat = ({chatId}) => {
2118
let [messages, setMessages] = useState([])
22-
let divRef = useRef()
2319
let messageRef = useRef()
2420
let needsScroll = useRef(false)
2521
let stompClient = useContext(StompContext)
@@ -28,8 +24,7 @@ export const Chat = ({chatId}) => {
2824
useEffect(() => {
2925
stompClient.subscribe("/topic/chat/" + chatId, (m) => {
3026
let message = JSON.parse(m.body)
31-
let msg = messageRef.current
32-
needsScroll.current = msg.scrollHeight <= msg.scrollTop + msg.offsetHeight
27+
needsScroll.current = isLastChildVisible(messageRef.current)
3328
setMessages(previous => {
3429
if (previous.length && previous[previous.length - 1].n === message.n) {
3530
return previous
@@ -54,7 +49,7 @@ export const Chat = ({chatId}) => {
5449
if (!needsScroll.current) {
5550
return
5651
}
57-
window.setTimeout(() => divRef.current?.scrollIntoView({behavior: "smooth"}), 0)
52+
window.setTimeout(() => messageRef.current?.lastChild?.scrollIntoView({behavior: "smooth"}), 0)
5853
}, [messages])
5954

6055
let onSendMessage = useCallback((event) => doTry(async () => {
@@ -71,14 +66,22 @@ export const Chat = ({chatId}) => {
7166
}), [stompClient, chatId])
7267

7368
return <>
74-
<div ref={messageRef}
75-
className="grow border border-gray-500 bg-gray-900 rounded-lg p-1 overflow-y-scroll">
76-
{messages.map(message => (
77-
<p key={message.n}>{message.user + ": " + message.message}</p>
78-
))}
79-
<div ref={divRef} />
69+
<div
70+
className="grow border border-gray-500 bg-gray-900 rounded-lg flex flex-col overflow-y-hidden">
71+
<div className="px-1 flex-none h-14 overflow-y-scroll">
72+
<p>Radolf!</p>
73+
<p>Radolf!</p>
74+
<p>Radolf!</p>
75+
<p>Radolf!</p>
76+
</div>
77+
<div className="w-full flex-none h-[2px] bg-gray-500" />
78+
<div ref={messageRef} className="px-1 overflow-y-scroll">
79+
{messages.map(message => (
80+
<p key={message.n}>{message.user + ": " + message.message}</p>
81+
))}
82+
</div>
8083
</div>
81-
<form className="flex-0 mb-2" onSubmit={onSendMessage}>
84+
<form className="flex-none mb-2" onSubmit={onSendMessage}>
8285
<input
8386
className="w-full rounded-lg p-2 border border-gray-500 bg-stone-800 text-stone-100"
8487
type="text"
@@ -87,3 +90,11 @@ export const Chat = ({chatId}) => {
8790
</form>
8891
</>
8992
}
93+
94+
function isLastChildVisible(container) {
95+
let lastChild = container.lastChild
96+
let lastChildTop = lastChild.offsetTop
97+
let containerTop = container.scrollTop + container.offsetTop
98+
let containerBottom = containerTop + container.clientHeight
99+
return lastChildTop < containerBottom
100+
}

src/main/client/src/feature/lobby/LobbyPanel.jsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111
import {
1212
useAuthStore,
1313
} from "../../store.js"
14+
import {
15+
Chat,
16+
} from "../../component/Chat.jsx"
1417
import {
1518
SideBar,
1619
} from "../../component/SideBar.jsx"
1720

1821
export const LobbyPanel = () => {
1922
return (
2023
<SideBar page="lobby">
21-
<div className="pt-2">
24+
<div className="pr-3 pt-4 pl-2 h-full flex flex-col gap-y-1">
2225
<Panel />
2326
</div>
2427
</SideBar>
@@ -48,18 +51,14 @@ function Panel() {
4851
sub1.unsubscribe()
4952
}
5053
}, [setUsers, stompClient])
51-
return (
52-
<div className="mt-2">
53-
<div className="pl-2 pb-2 border-b border-b-slate-700">
54-
{auth.name}
55-
</div>
56-
<div className="pl-2 mt-2">
57-
{users.map(user => (
58-
<div key={user}>
59-
{user}
60-
</div>
61-
))}
62-
</div>
54+
return <>
55+
<div className="flex-none h-12 border border-gray-500 bg-gray-900 rounded-lg p-1 overflow-y-scroll">
56+
{users.map(user => (
57+
<div key={user}>
58+
{user}
59+
</div>
60+
))}
6361
</div>
64-
)
62+
<Chat chatId="Lobby"/>
63+
</>
6564
}

0 commit comments

Comments
 (0)