automatic rejoin on reload or server restart
This commit is contained in:
parent
feaf96c10e
commit
64a61ef2b3
1 changed files with 141 additions and 58 deletions
|
|
@ -1,24 +1,39 @@
|
|||
import React, { useState } from 'react';
|
||||
import React, {useCallback, useState} from 'react';
|
||||
import './index.css';
|
||||
import ClientScreen from './ClientScreen';
|
||||
import Controls from './Controls.tsx';
|
||||
import JoinForm from './JoinForm.tsx';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import {createRoot} from 'react-dom/client';
|
||||
import PlayerInfo from './PlayerInfo.tsx';
|
||||
import {useStoredObjectState} from './useStoredState.ts';
|
||||
import {NameId, postPlayer} from './serverCalls.tsx';
|
||||
|
||||
function App() {
|
||||
const [id, setId] = useState<string | null>(null);
|
||||
const [nameId, setNameId] = useStoredObjectState<NameId>('access', () => ({
|
||||
id: crypto.randomUUID(),
|
||||
name: ''
|
||||
}));
|
||||
|
||||
const [isLoggedIn, setLoggedIn] = useState<boolean>(false);
|
||||
const logout = () => setLoggedIn(false);
|
||||
|
||||
useCallback(async () => {
|
||||
if (isLoggedIn)
|
||||
return;
|
||||
const result = await postPlayer(nameId);
|
||||
setLoggedIn(result !== null);
|
||||
}, [nameId, isLoggedIn])();
|
||||
|
||||
return <>
|
||||
{id === null && <JoinForm onDone={name => setId(name)} />}
|
||||
{id == null || <PlayerInfo playerId={id} />}
|
||||
<ClientScreen />
|
||||
{id == null || <Controls playerId={id} />}
|
||||
{nameId.name === '' && <JoinForm setNameId={setNameId} clientId={nameId.id}/>}
|
||||
{isLoggedIn && <PlayerInfo playerId={nameId.id} logout={logout}/>}
|
||||
<ClientScreen logout={logout}/>
|
||||
{isLoggedIn && <Controls playerId={nameId.id} logout={logout}/>}
|
||||
</>;
|
||||
}
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
<App/>
|
||||
</React.StrictMode>
|
||||
);
|
||||
|
|
|
|||
Reference in a new issue