From 686a52ce0bf007ff3961b3629ccfbf3ceb34e028 Mon Sep 17 00:00:00 2001 From: jbock Date: Sat, 10 Aug 2024 12:09:23 +0200 Subject: [PATCH] chat: fix vertical overflow --- src/main/client/src/Game.jsx | 2 +- src/main/client/src/component/Input.jsx | 3 +- src/main/client/src/feature/GameChat.jsx | 58 +++++++++++---------- src/main/client/src/feature/GamePanel.jsx | 48 ++++++++--------- src/main/client/src/index.css | 9 +--- src/main/java/com/bernd/ChatController.java | 18 ++----- src/main/java/com/bernd/Chats.java | 5 +- src/main/java/com/bernd/model/Chat.java | 2 + 8 files changed, 67 insertions(+), 78 deletions(-) diff --git a/src/main/client/src/Game.jsx b/src/main/client/src/Game.jsx index 14f1424..e10e3f2 100644 --- a/src/main/client/src/Game.jsx +++ b/src/main/client/src/Game.jsx @@ -212,7 +212,7 @@ export const Game = () => { }) setGameState(game) }, () => navigate(base + "/lobby")) - }, [setGameState, queueStatus, auth, gameId]) + }, [setGameState, queueStatus, auth, gameId, navigate]) useEffect(() => { if (initialized.current) { diff --git a/src/main/client/src/component/Input.jsx b/src/main/client/src/component/Input.jsx index 0785d0b..260c744 100644 --- a/src/main/client/src/component/Input.jsx +++ b/src/main/client/src/component/Input.jsx @@ -11,8 +11,9 @@ export const Input = ({ type, ...rest }) => { - let { register } = useFormContext() + let {register} = useFormContext() let classes = twJoin( + "border border-white rounded-lg p-2 bg-stone-800 text-stone-100", className, ) return ( diff --git a/src/main/client/src/feature/GameChat.jsx b/src/main/client/src/feature/GameChat.jsx index 06d315b..38a7843 100644 --- a/src/main/client/src/feature/GameChat.jsx +++ b/src/main/client/src/feature/GameChat.jsx @@ -18,17 +18,22 @@ import { } from "./../util.js" export const GameChat = () => { - let [messages, setMessages] = useState([]); - let divRef = useRef(null) + let [messages, setMessages] = useState([]) + let divRef = useRef() let stompClient = useContext(StompContext) - let { gameId } = useParams() + let {gameId} = useParams() let auth = useAuthStore(state => state.auth) useEffect(() => { - stompClient.subscribe("/topic/chat/" + gameId, (message) => { - let newMessage = JSON.parse(message.body) - setMessages(previous => [...previous, newMessage]) + stompClient.subscribe("/topic/chat/" + gameId, (m) => { + let message = JSON.parse(m.body) + setMessages(previous => { + if (previous.length && previous[previous.length - 1].n === message.n) { + return previous + } + return [...previous, message] + }) }) doTry(async () => { @@ -44,13 +49,13 @@ export const GameChat = () => { }, [stompClient, auth, gameId]) useEffect(() => { - divRef.current?.scrollIntoView({behavior: 'smooth'}); - }, [messages]); + divRef.current?.scrollIntoView({behavior: "smooth"}) + }, [messages]) let onSendMessage = useCallback((event) => doTry(async () => { event.preventDefault() let data = new FormData(event.target) - document.getElementById("sending").reset(); + event.target.reset() stompClient.publish({ destination: "/app/chat/send/", body: JSON.stringify({ @@ -58,25 +63,22 @@ export const GameChat = () => { id: gameId, }), }) - }), [ stompClient, gameId ]) - - return ( + }), [stompClient, gameId]) -
-
-
- {messages.map(message => ( -

{message.user + ": " + message.message}

- ))} -
-
-
-
- -
+ return <> +
+ {messages.map(message => ( +

{message.user + ": " + message.message}

+ ))} +
- ); +
+ +
+ } diff --git a/src/main/client/src/feature/GamePanel.jsx b/src/main/client/src/feature/GamePanel.jsx index a0ed055..4dd1f2e 100644 --- a/src/main/client/src/feature/GamePanel.jsx +++ b/src/main/client/src/feature/GamePanel.jsx @@ -37,8 +37,8 @@ import { export const GamePanel = ({zoom, setZoom}) => { return ( -
-
+
+
@@ -93,7 +93,7 @@ function Panel({zoom, setZoom}) { let result = gameHasEnded ? getScore(board) : undefined return ( <> -
+
+ }}> + Zoom: {zoom + 0} + +
- -
+
{white}
vs
{black}
-
Move {queueLength}
-
+
+ Move {queueLength} +
+
{counting && <> -
+
-
+