diff --git a/clients/solid/src/App.tsx b/clients/solid/src/App.tsx index 3389c59..5f3583c 100644 --- a/clients/solid/src/App.tsx +++ b/clients/solid/src/App.tsx @@ -10,7 +10,7 @@ import { } from "@/components/ui/tabs"; import Home from "@/pages/Home"; import Footer from "@/components/Footer"; -import { Websocket } from "./components/Websocket"; +import { Websocket, WebsocketProvider } from "./components/Websocket"; const Browser = lazy(() => import("@/pages/Browser")); const Game = lazy(() => import("@/pages/Game")); @@ -20,33 +20,34 @@ export default function App(): JSXElement { <> -
-
-

- Cards For Humanity -

-
- - - - Home - Browser - Game - - - - - - - - - - - - -
+ +
+
+

+ Cards For Humanity +

+
+ + + Home + Browser + Game + + + + + + + + + + + + +
+
-
+ ); diff --git a/clients/solid/src/components/Debug.tsx b/clients/solid/src/components/Debug.tsx index d431584..78491fa 100644 --- a/clients/solid/src/components/Debug.tsx +++ b/clients/solid/src/components/Debug.tsx @@ -1,5 +1,38 @@ -import { JSXElement } from "solid-js"; +import { createEffect, createSignal, JSXElement, useContext } from "solid-js"; +import { Button } from "@/components/ui/button"; +import { useWebsocket, WebsocketContext, WebsocketProvider } from "./Websocket"; export function Debug(): JSXElement { - return <>debug; + const connected = useWebsocket; + // const [connected, set_connected] = createSignal(false); + const [user_count, _set_user_count] = createSignal("0"); + const [game_count, _set_game_count] = createSignal("0"); + + // const toggle = () => set_connected(!connected()); + createEffect(() => console.log(connected())); + + return ( +
+

Debug:

+

+ Connection Status: {connected() ? "Connected" : "Disconnected"} +

+

Users Online: {user_count()}

+

Active Games: {game_count()}

+
+ + +
+
+ ); } diff --git a/clients/solid/src/components/Websocket.tsx b/clients/solid/src/components/Websocket.tsx index 6158d39..0f967d4 100644 --- a/clients/solid/src/components/Websocket.tsx +++ b/clients/solid/src/components/Websocket.tsx @@ -1,38 +1,48 @@ import Joi from "joi"; -import { createEffect, createSignal, onCleanup } from "solid-js"; +import { + createEffect, + createSignal, + onCleanup, + createContext, + useContext, +} from "solid-js"; -export function Websocket() { +export const WebsocketContext = createContext(); + +export function WebsocketProvider(props) { const socket = new WebSocket("ws://127.0.0.1:3030/websocket"); // Signals - let [connected, set_connected] = createSignal(false); - let [users_count, set_users_count] = createSignal(null); - let [games_count, set_games_count] = createSignal(null); let [active_games, set_active_games] = createSignal(null); - let [user_update, set_user_update] = createSignal(null); - let [chat_update, set_chat_update] = createSignal(null); - let [judge_round, set_judge_round] = createSignal(null); + let [card_packs_meta, set_card_packs_meta] = createSignal(null); let [chat_message, set_chat_message] = createSignal(null); + let [chat_update, set_chat_update] = createSignal(null); + let [connected, set_connected] = createSignal(false); let [game_meta, set_game_meta] = createSignal(null); let [game_state, set_game_state] = createSignal(null); - let [card_packs_meta, set_card_packs_meta] = createSignal(null); + let [games_count, set_games_count] = createSignal(null); + let [judge_round, set_judge_round] = createSignal(null); + let [user_update, set_user_update] = createSignal(null); + let [users_count, set_users_count] = createSignal(null); // Wipe everything on disconnect createEffect(() => { if (!connected()) { - set_users_count(null); - set_games_count(null); set_active_games(null); - set_user_update(null); - set_chat_update(null); - set_judge_round(null); + set_card_packs_meta(null); set_chat_message(null); + set_chat_update(null); set_game_meta(null); set_game_state(null); - set_card_packs_meta(null); + set_games_count(null); + set_judge_round(null); + set_user_update(null); + set_users_count(null); } }); + const state = [connected]; + // Incoming message types const UserUpdate = Joi.object({ username: Joi.string(), @@ -126,4 +136,14 @@ export function Websocket() { socket.removeEventListener("close", handleClose); socket.close(); }); + + return ( + + {props.children} + + ); +} + +export function useWebsocket() { + return useContext(WebsocketContext); }