-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlobby.tsx
More file actions
69 lines (65 loc) · 2.24 KB
/
lobby.tsx
File metadata and controls
69 lines (65 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { NonIdealState, Spinner } from "@blueprintjs/core";
import { SetupBase } from "./setup-base";
import { useNavigate, Navigate } from "react-router-dom";
import { GameStartedMessage } from "../../common/message/game-message";
import { useSocket, useEffectQuery, get } from "../api";
import { ClientType } from "../../common/client-types";
import { ThemeButtons } from "./setup";
/**
* check for an active game and waits for one or forwards to setup
*
* @returns a setup base with the loading screen or a redirect to /game
*/
export function Lobby() {
const { isPending, data } = useEffectQuery("client-information", () =>
get("/client-information"),
);
const navigate = useNavigate();
// use a socket to wait for game started
useSocket((message) => {
if (message instanceof GameStartedMessage) {
navigate("/game");
}
});
// show a loading screen if the message is pending
if (isPending) {
return (
<SetupBase>
<NonIdealState
icon={<Spinner intent="primary" />}
title="Loading..."
/>
</SetupBase>
);
}
// navigate to setup if the client is a host, or show a loading screen if waiting
if (data.clientType === ClientType.HOST) {
return <Navigate to="/setup" />;
} else {
return (
<SetupBase>
<>
<NonIdealState
title={
data.clientType === ClientType.CLIENT ?
"Waiting For Host to Start"
: "Waiting in line"
}
icon={<Spinner intent="primary" />}
/>
<div
style={{
alignItems: "center",
display: "flex",
flex: "1 0 auto",
flexDirection: "column",
justifyContent: "space-around",
}}
>
<ThemeButtons />
</div>
</>
</SetupBase>
);
}
}