From 8039636e5fb5206bacb8b5d73ae100a70d7229fe Mon Sep 17 00:00:00 2001
From: Kalle <38327916+Sendouc@users.noreply.github.com>
Date: Sun, 23 Mar 2025 13:57:01 +0200
Subject: [PATCH] Show connectin status text for chat
---
app/features/chat/components/Chat.tsx | 25 +++++++++++++++----------
locales/en/common.json | 1 +
2 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/app/features/chat/components/Chat.tsx b/app/features/chat/components/Chat.tsx
index 702161b14..5721a65c5 100644
--- a/app/features/chat/components/Chat.tsx
+++ b/app/features/chat/components/Chat.tsx
@@ -68,7 +68,7 @@ export function Chat({
messages,
currentRoom,
setCurrentRoom,
- connected,
+ readyState,
unseenMessages,
} = chat;
@@ -98,7 +98,7 @@ export function Chat({
};
}, [onMount, onUnmount]);
- const sendingMessagesDisabled = disabled || !connected;
+ const sendingMessagesDisabled = disabled || readyState !== "CONNECTED";
const systemMessageText = (msg: ChatMessage) => {
const name = () => {
@@ -202,11 +202,13 @@ export function Chat({
maxLength={MESSAGE_MAX_LENGTH}
/>{" "}
- {typeof connected !== "boolean" ? (
-
- ) : connected ? (
+ {readyState === "CONNECTED" || readyState === "CONNECTING" ? (
- {t("common:chat.connected")}
+ {t(
+ readyState === "CONNECTED"
+ ? "common:chat.connected"
+ : "common:chat.connecting",
+ )}
) : (
@@ -326,7 +328,9 @@ export function useChat({
const user = useUser();
const [messages, setMessages] = React.useState([]);
- const [connected, setConnected] = React.useState(null);
+ const [readyState, setReadyState] = React.useState<
+ "CONNECTING" | "CONNECTED" | "CLOSED"
+ >("CONNECTING");
const [sentMessage, setSentMessage] = React.useState();
const [currentRoom, setCurrentRoom] = React.useState(
rooms[0]?.code,
@@ -356,9 +360,10 @@ export function useChat({
});
ws.current.onopen = () => {
setCurrentRoom(rooms[0].code);
- setConnected(true);
+ setReadyState("CONNECTED");
};
- ws.current.onclose = () => setConnected(false);
+ ws.current.onclose = () => setReadyState("CLOSED");
+ ws.current.onerror = () => setReadyState("CLOSED");
ws.current.onmessage = (e) => {
const message = JSON.parse(e.data);
@@ -463,7 +468,7 @@ export function useChat({
send,
currentRoom,
setCurrentRoom,
- connected,
+ readyState,
unseenMessages,
};
}
diff --git a/locales/en/common.json b/locales/en/common.json
index 80395446d..f2da1340e 100644
--- a/locales/en/common.json
+++ b/locales/en/common.json
@@ -268,6 +268,7 @@
"leaderboard.qualification.info": "Every player of the roster needs 7 sets of SendouQ played during the season. Every unique roster of 4 have their own points.",
"chat.connected": "Connected",
+ "chat.connecting": "Connecting...",
"chat.disconnected": "Disconnected",
"chat.send": "Send",
"chat.input.placeholder": "Press enter to send",