feat: style participants list

This commit is contained in:
Alexander Daichendt 2025-02-13 13:53:31 +01:00
parent 86c1409d3f
commit ef521dfc47
4 changed files with 38 additions and 4 deletions

View file

@ -5,10 +5,11 @@ function ParticipantList() {
return ( return (
<> <>
<h3 className="text-lg font-semibold">Participants</h3>
<ul> <ul>
{state.participants.map((participant) => ( {state.participants.map((participant) => (
<li key={participant.userId}>{participant.name}</li> <li key={participant.userId} className="p-4 border-b border-gray-200">
{participant.name}
</li>
))} ))}
</ul> </ul>
</> </>

View file

@ -25,7 +25,7 @@ function Room() {
); );
} }
// is connected // View when is connected to the room
return ( return (
<Layout> <Layout>
<section className="w-full"> <section className="w-full">

View file

@ -6,6 +6,7 @@ import type {
ServerMessageEditedMessage, ServerMessageEditedMessage,
ServerRegistrationConfirmed, ServerRegistrationConfirmed,
ServerUserJoinedMessage, ServerUserJoinedMessage,
ServerUserListMessage,
User, User,
} from "../../shared"; } from "../../shared";
import type { Room, WebSocketData } from "./types"; import type { Room, WebSocketData } from "./types";
@ -45,6 +46,19 @@ export default function handleClientMessage(
socket.send(JSON.stringify(joinMessage)); socket.send(JSON.stringify(joinMessage));
} }
// send updated participants list
const userArray = Array.from(room.userConnections.values());
const userListMessage: ServerUserListMessage = {
type: "USER_LIST",
payload: {
users: userArray,
},
};
for (const [socket] of room.userConnections) {
socket.send(JSON.stringify(userListMessage));
}
break; break;
case "CHAT_MESSAGE": case "CHAT_MESSAGE":

View file

@ -1,4 +1,8 @@
import type { ClientMessage, ServerUserLeftMessage } from "../../shared.ts"; import type {
ClientMessage,
ServerUserLeftMessage,
ServerUserListMessage,
} from "../../shared.ts";
import handleClientMessage from "./handleClientMessage.ts"; import handleClientMessage from "./handleClientMessage.ts";
import type { Room, WebSocketData } from "./types.ts"; import type { Room, WebSocketData } from "./types.ts";
@ -78,6 +82,21 @@ const server = Bun.serve<WebSocketData>({
} }
} }
room.userConnections.delete(ws);
// send updated user list
const userArray = Array.from(room.userConnections.values());
const userListMessage: ServerUserListMessage = {
type: "USER_LIST",
payload: {
users: userArray,
},
};
for (const [socket] of room.userConnections) {
socket.send(JSON.stringify(userListMessage));
}
if (room.userConnections.size === 0) { if (room.userConnections.size === 0) {
rooms.delete(roomId); rooms.delete(roomId);
console.log("Cleaned up room", roomId); console.log("Cleaned up room", roomId);