2024-04-14 11:16:34 +02:00
|
|
|
import {useCallback, useState} from 'react';
|
|
|
|
import ClientScreen from './ClientScreen';
|
|
|
|
import Controls from './Controls.tsx';
|
|
|
|
import JoinForm from './JoinForm.tsx';
|
|
|
|
import PlayerInfo from './PlayerInfo.tsx';
|
|
|
|
import {useStoredObjectState} from './useStoredState.ts';
|
|
|
|
import {NameId, postPlayer} from './serverCalls.tsx';
|
|
|
|
import Column from "./components/Column.tsx";
|
|
|
|
import Row from "./components/Row.tsx";
|
|
|
|
import Scoreboard from "./Scoreboard.tsx";
|
|
|
|
import Button from "./components/Button.tsx";
|
|
|
|
import './App.css';
|
|
|
|
|
|
|
|
const getNewNameId = () => ({
|
|
|
|
id: crypto.randomUUID(),
|
|
|
|
name: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
const [nameId, setNameId] = useStoredObjectState<NameId>('access', getNewNameId);
|
|
|
|
|
|
|
|
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])();
|
|
|
|
|
2024-04-14 12:57:45 +02:00
|
|
|
return <Column className='flex-grow'>
|
2024-04-14 11:16:34 +02:00
|
|
|
<Row>
|
2024-04-14 12:57:45 +02:00
|
|
|
<h1 className='flex-grow'>CCCB-Tanks!</h1>
|
|
|
|
<Button
|
|
|
|
onClick={() => window.open('https://github.com/kaesaecracker/cccb-tanks-cs', '_blank')?.focus()}
|
|
|
|
text='GitHub'/>
|
2024-04-14 11:16:34 +02:00
|
|
|
{nameId.name !== '' &&
|
2024-04-14 12:57:45 +02:00
|
|
|
<Button onClick={() => setNameId(getNewNameId)} text='logout'/>}
|
2024-04-14 11:16:34 +02:00
|
|
|
</Row>
|
|
|
|
<ClientScreen logout={logout}/>
|
|
|
|
{nameId.name === '' && <JoinForm setNameId={setNameId} clientId={nameId.id}/>}
|
|
|
|
{isLoggedIn && <Row className='GadgetRows'>
|
|
|
|
<Controls playerId={nameId.id} logout={logout}/>
|
|
|
|
<PlayerInfo playerId={nameId.id} logout={logout}/>
|
|
|
|
<Scoreboard/>
|
|
|
|
</Row>
|
|
|
|
}
|
|
|
|
</Column>;
|
|
|
|
}
|