fixed closing drawer + reconnect

This commit is contained in:
MasterGordon 2025-06-28 16:41:30 +02:00
parent 59ec036380
commit 118ae220ee
3 changed files with 30 additions and 13 deletions

View File

@ -40,6 +40,6 @@ export const removeGems = async (
if (count - gems < 0) throw new Error("Not enough gems"); if (count - gems < 0) throw new Error("Not enough gems");
await db await db
.update(Gems) .update(Gems)
.set({ count: count - gems, totalCount: totalCount - gems }) .set({ count: count - gems, totalCount: totalCount })
.where(eq(Gems.user, user)); .where(eq(Gems.user, user));
}; };

View File

@ -57,7 +57,14 @@ const Shell: React.FC<PropsWithChildren> = ({ children }) => {
animate={{ x }} animate={{ x }}
transition={{ type: "tween" }} transition={{ type: "tween" }}
> >
<div className="w-full p-2 flex flex-col gap-6"> <div
className="w-full p-2 flex flex-col gap-6"
onClick={() => {
if (isMobile) {
setIsOpen(false);
}
}}
>
<h2 className="[background:var(--bg-brand)] [-webkit-text-fill-color:transparent] font-black [-webkit-background-clip:text!important] font-mono text-3xl"> <h2 className="[background:var(--bg-brand)] [-webkit-text-fill-color:transparent] font-black [-webkit-background-clip:text!important] font-mono text-3xl">
Business Business
<br /> <br />
@ -101,7 +108,10 @@ const Shell: React.FC<PropsWithChildren> = ({ children }) => {
<Button <Button
className="absolute left-4 bg-black border-white/10 border-y-1 border-r-1 rounded-l-none" className="absolute left-4 bg-black border-white/10 border-y-1 border-r-1 rounded-l-none"
variant="ghost" variant="ghost"
onClick={() => setIsOpen((isOpen) => !isOpen)} onClick={(e) => {
e.stopPropagation();
setIsOpen((isOpen) => !isOpen);
}}
aria-label="Menu" aria-label="Menu"
> >
<Menu /> <Menu />

View File

@ -25,6 +25,17 @@ const createWSClient = () => {
let reconnectAttempts = 0; let reconnectAttempts = 0;
const maxReconnectAttempts = 5; const maxReconnectAttempts = 5;
const tryReconnect = () => {
if (reconnectAttempts < maxReconnectAttempts) {
setTimeout(() => {
reconnectAttempts++;
connect();
}, 1000 * reconnectAttempts);
} else {
console.error("Max reconnect attempts reached");
}
};
const connect = () => { const connect = () => {
ws = new WebSocket(connectionString); ws = new WebSocket(connectionString);
@ -42,16 +53,7 @@ const createWSClient = () => {
ws.onmessage = emitMessage; ws.onmessage = emitMessage;
ws.onclose = () => { ws.onclose = tryReconnect;
if (reconnectAttempts < maxReconnectAttempts) {
setTimeout(() => {
reconnectAttempts++;
connect();
}, 1000 * reconnectAttempts);
} else {
console.error("Max reconnect attempts reached");
}
};
ws.onerror = (err) => { ws.onerror = (err) => {
console.error("WebSocket error", err); console.error("WebSocket error", err);
@ -60,6 +62,11 @@ const createWSClient = () => {
}; };
connect(); connect();
document.addEventListener("focus", () => {
if (ws.readyState != ws.OPEN) {
tryReconnect();
}
});
addMessageListener((event: MessageEvent) => { addMessageListener((event: MessageEvent) => {
const data = JSON.parse(event.data) as Events; const data = JSON.parse(event.data) as Events;