-
Notifications
You must be signed in to change notification settings - Fork 3
/
Lobby.tsx
158 lines (145 loc) · 5.65 KB
/
Lobby.tsx
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { useEffect, useReducer, useRef, useState } from "react";
import { Box, Button, CircularProgress, Grid, Typography } from "@mui/material";
import { NewRoomButtom } from "./NewRoom";
import { useNavigate } from "react-router-dom";
import { getRoomPath } from "./routes";
import { usePeer } from "@peerbit/react";
import { Lobby as LobbyDB, Room } from "@peerbit/example-many-chat-rooms";
import { SearchRequest } from "@peerbit/document";
export const Lobby = () => {
const { loading: loadingPeer } = usePeer();
const navigate = useNavigate();
const { peer } = usePeer();
const [lobby, setLobby] = useState<LobbyDB>();
const rooms = useRef<Room[]>([]);
const [_, forceUpdate] = useReducer((x) => x + 1, 0);
const [peerCount, setPeerCount] = useState(1);
useEffect(() => {
console.log("???", peer?.identity.publicKey.hashcode());
if (!peer?.identity.publicKey.hashcode()) {
return;
}
peer.open(
new LobbyDB({
id: new Uint8Array(32), // 0,0,....0 choose this dynamically instead? Now it is static, => same lobby for all
}),
{ args: { sync: () => true }, existing: "reuse" }
)
.then(async (lobby) => {
console.log("OPEN LOBBY", lobby);
setLobby(lobby);
const addToLobby = (toAdd: Room[], reset?: Boolean) => {
if (reset) {
rooms.current = [];
}
for (const room of toAdd) {
const ix = rooms.current.findIndex(
(x) => x.id === room.id
);
if (ix === -1) {
rooms.current.push(room);
} else {
rooms.current[ix] = room;
}
}
if (toAdd.length > 0) {
forceUpdate();
}
};
lobby.rooms.index
.search(new SearchRequest(), {})
.then((results) => {
addToLobby(results, true);
});
lobby.rooms.events.addEventListener("change", async (e) => {
// additions
e.detail.added && addToLobby(e.detail.added);
// removals
e.detail.removed?.forEach((p) => {
const ix = rooms.current.findIndex(
(x) => x.id === p.id
);
if (ix !== -1) {
rooms.current.splice(ix, 1);
}
});
e.detail.removed && forceUpdate();
});
lobby.rooms.events.addEventListener("join", () => {
lobby.rooms
.getReady()
.then((set) => setPeerCount(set.size + 1));
});
lobby.rooms.events.addEventListener("leave", () => {
lobby.rooms
.getReady()
.then((set) => setPeerCount(set.size + 1));
});
})
.catch((e) => {
console.error(e);
});
}, [peer?.identity.publicKey.hashcode()]);
const goToRoom = (room: Room) => {
navigate(getRoomPath(room));
};
return (
<Box>
<Grid container>
<Grid item mb={2}>
<Typography variant="subtitle1">A P2P chat app</Typography>
</Grid>
<Grid
container
direction="row"
item
alignItems="left"
spacing={2}
mb={2}
>
<Grid item>
<Typography variant="h4">Rooms</Typography>
</Grid>
<Grid item>
<NewRoomButtom lobby={lobby} />
</Grid>
<Grid
item
sx={{
display: "flex",
textAlign: "center",
alignItems: "center",
}}
>
<Typography>Peers in lobby: {peerCount}</Typography>
</Grid>
</Grid>
{loadingPeer ? (
<Grid item>
<></> <CircularProgress size={20} />
</Grid>
) : (
<Grid item>
{rooms.current?.length > 0 ? (
<Box>
{rooms.current.map((room, ix) => (
<Typography key={ix} variant="h5">
<Button
variant="text"
onClick={() => goToRoom(room)}
>
{" "}
{room.name}
</Button>
</Typography>
))}
</Box>
) : (
<Typography>No rooms found</Typography>
)}
</Grid>
)}
</Grid>
</Box>
);
};