Files
picobook/src/client/GamePage.tsx
T

110 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-06-10 21:47:30 -04:00
import { Link, useParams, useSearchParams } from "react-router-dom";
2024-04-03 22:53:11 -07:00
import { useEffect, useRef, useState } from "react";
2024-03-31 19:40:06 -07:00
import { css } from "@emotion/css";
2024-04-03 20:29:27 -07:00
import { useWebsocket } from "./hooks/useWebsocket";
2026-06-10 21:47:30 -04:00
import { Pico8Player } from "@athingperday/react-pico-player";
2024-03-31 19:40:06 -07:00
2026-06-10 21:47:30 -04:00
type Game = {
carts: Parameters<typeof Pico8Player>["0"]["carts"];
};
2024-03-31 19:40:06 -07:00
export const GamePage = () => {
2026-06-10 21:47:30 -04:00
const { author, slug } = useParams();
2024-05-11 18:04:44 -07:00
// const [searchParams, setSearchParams] = useSearchParams();
// const room = searchParams.get('room');
2026-06-10 21:47:30 -04:00
// const picoRef = useRef<Pico8ConsoleImperatives>(null);
2024-05-11 18:04:44 -07:00
// const socket = useWebsocket({
// url: `/api/ws/room?room=${room}`,
// // url: "wss://echo.websocket.org",
// onMessage({message}) {
// // const msg = message as any;
// // if (msg.type === "gpio") {
// // if (picoRef.current) {
// // const handle = picoRef.current.getPicoConsoleHandle();
// // if (handle) {
// // console.log("updating pico gpio");
// // (handle.gpio as any).dontSend = true;
// // handle.gpio.length = 0;
// // handle.gpio.push(...msg.gpio);
// // (handle.gpio as any).dontSend = false;
// // }
// // }
// // }
// console.log('message', message);
// }
// })
2024-04-02 19:04:34 -07:00
// const version = searchParams.get('v');
2026-06-10 21:47:30 -04:00
const [game, setGame] = useState<Game | null>(null);
2024-04-02 19:04:34 -07:00
2024-03-31 19:40:06 -07:00
useEffect(() => {
const fetchInfo = async () => {
2026-06-10 21:47:30 -04:00
let url = `/api/game?author=${author}&name=${slug ?? ""}`;
2024-03-31 19:40:06 -07:00
const information = await fetch(url);
const json = await information.json();
2026-06-10 21:47:30 -04:00
console.log(json);
setGame(json);
};
2024-03-31 19:40:06 -07:00
fetchInfo();
2026-06-10 21:47:30 -04:00
}, [setGame, slug]);
2024-03-31 19:40:06 -07:00
2026-06-10 21:47:30 -04:00
if (!game) {
return <div>LOADING...</div>;
2024-03-31 19:40:06 -07:00
}
2026-06-10 21:47:30 -04:00
if (!game.carts) {
return <div>NOT FOUND</div>;
2024-03-31 19:40:06 -07:00
}
return (
2026-06-10 21:47:30 -04:00
<div
className={css`
2024-03-31 19:48:23 -07:00
margin: auto;
2026-06-10 21:47:30 -04:00
width: max-content;
max-inline-size: 66ch;
padding: 1.5em;
display: flex;
flex-direction: column;
gap: 1em;
`}
>
<div>
<h1>{slug}</h1>
<h2>
by <Link to={`/u/${author}`}>{author}</Link>
</h2>
</div>
<div
className={css`
width: 512px;
max-width: 100%;
margin: auto;
`}
>
<div
className={css`
border: 2px solid transparent;
&:focus-within {
border: 2px solid limegreen;
}
`}
>
<Pico8Player
// ref={picoRef}
carts={game.carts}
2024-04-04 08:58:39 -07:00
// onGpioChange={(gpio: number[]) => {
// console.log("sending gpio");
// socket.sendMessage({
// type: "gpio",
// gpio,
// });
// }}
/>
2024-04-01 00:22:52 -07:00
</div>
2024-03-31 19:48:23 -07:00
</div>
2024-04-02 19:04:34 -07:00
{/* <div>
<p>This is a paragraph about this game. It is a cool game. And a cool website to play it on. It automagically connects from GitHub.</p>
</div> */}
2024-03-31 19:40:06 -07:00
</div>
2026-06-10 21:47:30 -04:00
);
};