Fix version switching and silent cart

This commit is contained in:
dylan 2024-03-31 20:37:18 -07:00
parent 54ea14af63
commit b88bd7fe1e
2 changed files with 32 additions and 2 deletions

View File

@ -13,6 +13,7 @@ export const GamePage = () => {
const {author, slug, version} = useParams(); const {author, slug, version} = useParams();
const navigate = useNavigate(); const navigate = useNavigate();
const [info, setInfo] = useState<Info | null>(null); const [info, setInfo] = useState<Info | null>(null);
useEffect(() => { useEffect(() => {
const fetchInfo = async () => { const fetchInfo = async () => {
let url = `/api/release?author=${author}&slug=${slug}`; let url = `/api/release?author=${author}&slug=${slug}`;

View File

@ -8,6 +8,7 @@ type Pico8ConsoleImperatives = {
export const Pico8Console = forwardRef((props: { carts: PicoCart[] }, forwardedRef: ForwardedRef<Pico8ConsoleImperatives>) => { export const Pico8Console = forwardRef((props: { carts: PicoCart[] }, forwardedRef: ForwardedRef<Pico8ConsoleImperatives>) => {
const {carts} = props; const {carts} = props;
const [playing, setPlaying] = useState(false);
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const [handle, setHandle] = useState<PicoPlayerHandle | null>(null); const [handle, setHandle] = useState<PicoPlayerHandle | null>(null);
const attachConsole = useCallback(async () => { const attachConsole = useCallback(async () => {
@ -25,8 +26,36 @@ export const Pico8Console = forwardRef((props: { carts: PicoCart[] }, forwardedR
} }
}), [handle]); }), [handle]);
useEffect(() => { useEffect(() => {
if (playing) {
attachConsole(); attachConsole();
}, [attachConsole]); return () => {
if (ref.current) {
ref.current.innerHTML = "";
}
}
}
}, [playing, attachConsole]);
if (!playing) {
return (
<div
ref={ref}
className={css`
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
aspect-ratio: 1;
background-color: black;
color: white;
cursor: pointer;
`}
onClick={() => {setPlaying(true)}}
>
Play!
</div>
)
}
return ( return (
<div ref={ref} className={css` <div ref={ref} className={css`
width: 100%; width: 100%;