Skip to content

Commit efd04f3

Browse files
committed
chat component
1 parent aa089d7 commit efd04f3

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/main/client/src/feature/game/GameChat.jsx renamed to src/main/client/src/component/Chat.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@ import {
1010
} from "react-router-dom"
1111
import {
1212
useAuthStore,
13-
} from "../../store.js"
13+
} from "../store.js"
1414
import {
1515
StompContext,
1616
tfetch,
1717
doTry,
18-
} from "../../util.js"
18+
} from "../util.js"
1919

20-
export const GameChat = () => {
20+
export const Chat = ({chatId}) => {
2121
let [messages, setMessages] = useState([])
2222
let divRef = useRef()
2323
let messageRef = useRef()
2424
let needsScroll = useRef(false)
2525
let stompClient = useContext(StompContext)
26-
let {gameId} = useParams()
2726
let auth = useAuthStore(state => state.auth)
2827

2928
useEffect(() => {
30-
stompClient.subscribe("/topic/chat/" + gameId, (m) => {
29+
stompClient.subscribe("/topic/chat/" + chatId, (m) => {
3130
let message = JSON.parse(m.body)
3231
let msg = messageRef.current
3332
needsScroll.current = msg.scrollHeight <= msg.scrollTop + msg.offsetHeight
@@ -40,7 +39,7 @@ export const GameChat = () => {
4039
})
4140

4241
doTry(async () => {
43-
let chat = await tfetch("/api/chat/" + gameId, {
42+
let chat = await tfetch("/api/chat/" + chatId, {
4443
method: "GET",
4544
headers: {
4645
"Authorization": "Bearer " + auth.token,
@@ -49,7 +48,7 @@ export const GameChat = () => {
4948
})
5049
setMessages(chat.messages || [])
5150
})
52-
}, [stompClient, auth, gameId])
51+
}, [stompClient, auth, chatId])
5352

5453
useEffect(() => {
5554
if (!needsScroll.current) {
@@ -66,10 +65,10 @@ export const GameChat = () => {
6665
destination: "/app/chat/send/",
6766
body: JSON.stringify({
6867
message: data.get("message"),
69-
id: gameId,
68+
id: chatId,
7069
}),
7170
})
72-
}), [stompClient, gameId])
71+
}), [stompClient, chatId])
7372

7473
return <>
7574
<div ref={messageRef}

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
useContext,
44
} from "react"
55
import {
6+
useParams,
67
useNavigate,
78
} from "react-router-dom"
89
import {
@@ -34,8 +35,8 @@ import {
3435
useLayoutStore,
3536
} from "../../layout.js"
3637
import {
37-
GameChat,
38-
} from "./GameChat.jsx"
38+
Chat,
39+
} from "../../component/Chat.jsx"
3940
import {
4041
SideBar,
4142
} from "../../component/SideBar.jsx"
@@ -51,6 +52,7 @@ export const GamePanel = () => {
5152
}
5253

5354
function Panel() {
55+
let {gameId} = useParams()
5456
let zoom = useLayoutStore(state => state.zoom)
5557
let setZoom = useLayoutStore(state => state.setZoom)
5658
let stompClient = useContext(StompContext)
@@ -174,7 +176,7 @@ function Panel() {
174176
{(result.w > result.b ? "W+" : "B+") + Math.abs(result.b - result.w)}
175177
</div>
176178
)}
177-
<GameChat />
179+
<Chat chatId={gameId}/>
178180
</>
179181
)
180182
}

0 commit comments

Comments
 (0)