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';
|
2024-04-14 14:55:01 +02:00
|
|
|
import {getRandomTheme, useStoredTheme} from "./theme.ts";
|
2024-04-19 13:29:51 +02:00
|
|
|
import {EmptyGuid} from "./Guid.ts";
|
2024-04-14 11:16:34 +02:00
|
|
|
|
|
|
|
const getNewNameId = () => ({
|
2024-04-19 13:29:51 +02:00
|
|
|
id: EmptyGuid,
|
2024-04-14 11:16:34 +02:00
|
|
|
name: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
export default function App() {
|
2024-04-14 14:55:01 +02:00
|
|
|
const [theme, setTheme] = useStoredTheme();
|
2024-04-14 11:16:34 +02:00
|
|
|
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);
|
2024-04-14 21:10:21 +02:00
|
|
|
setLoggedIn(result.ok);
|
2024-04-14 11:16:34 +02:00
|
|
|
}, [nameId, isLoggedIn])();
|
|
|
|
|
2024-04-14 12:57:45 +02:00
|
|
|
return <Column className='flex-grow'>
|
2024-04-21 21:37:38 +02:00
|
|
|
|
|
|
|
<ClientScreen logout={logout} theme={theme} playerId={nameId.id}/>
|
|
|
|
|
2024-04-14 11:16:34 +02:00
|
|
|
<Row>
|
2024-04-14 12:57:45 +02:00
|
|
|
<h1 className='flex-grow'>CCCB-Tanks!</h1>
|
2024-04-14 15:16:32 +02:00
|
|
|
<Button text='change colors' onClick={() => setTheme(_ => getRandomTheme())}/>
|
2024-04-14 12:57:45 +02:00
|
|
|
<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>
|
2024-04-21 21:37:38 +02:00
|
|
|
|
2024-04-14 11:16:34 +02:00
|
|
|
{nameId.name === '' && <JoinForm setNameId={setNameId} clientId={nameId.id}/>}
|
2024-04-21 21:37:38 +02:00
|
|
|
|
2024-04-14 21:10:21 +02:00
|
|
|
<Row className='GadgetRows'>
|
2024-04-17 23:09:01 +02:00
|
|
|
{isLoggedIn && <Controls playerId={nameId.id}/>}
|
|
|
|
{isLoggedIn && <PlayerInfo playerId={nameId.id}/>}
|
2024-04-14 11:16:34 +02:00
|
|
|
<Scoreboard/>
|
|
|
|
</Row>
|
2024-04-21 21:37:38 +02:00
|
|
|
|
2024-04-14 11:16:34 +02:00
|
|
|
</Column>;
|
|
|
|
}
|