Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored useChatRoom hook #18

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/ScoreBoardAndToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default function ScoreBoardAndToggle(props: {
</thead>
<tbody>
{
Array.from(props.scoreBoard.entries()).sort(([p1, s1], [p2, s2]) => s2 - s1).map(([player, score], i) =>
Array.from(props.scoreBoard.entries()).sort(([, s1], [, s2]) => s2 - s1).map(([player, score], i) =>
<tr key={i}>
<td><PlayerAvatar playerId={player}/></td>
<td>{props.names.get(player) ?? '-'}</td>
Expand Down
40 changes: 4 additions & 36 deletions src/lib/useChatRoom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {useEffect, useState} from "react";
import {Chat} from "./setup";
import {ChatRoomEvents} from "./ChatRoom";
import {EventListener} from "./types";
import useNames from "./useNames";
import useMessages from "./useMessages";

export interface Message {
type: 'message';
Expand All @@ -21,41 +22,8 @@ export interface ChatRoomLike {
export default function useChatRoom(
chatRoom: ChatRoomLike = Chat,
) {
const [messages, setMessages] = useState<Messages>([]);

useEffect(() => {
const textListener = (text: string, whose: string) => {
setMessages(prev => [
...prev,
{
type: 'message',
text,
whose,
timestamp: Date.now(),
},
]);
};
chatRoom.listener.on('text', textListener);
return () => {
chatRoom.listener.off('text', textListener);
}
}, [chatRoom.listener]);

const [names, setNames] = useState(new Map<string, string>());

useEffect(() => {
const nameListener = (name: string, whose: string) => {
setNames(prev => {
const next = new Map(prev);
next.set(whose, name);
return next;
});
};
chatRoom.listener.on('name', nameListener);
return () => {
chatRoom.listener.off('name', nameListener);
}
}, [chatRoom.listener]);
const messages = useMessages(chatRoom);
const names = useNames(chatRoom);

const setMyName = (name: string) => {
chatRoom.setMyName(name);
Expand Down
28 changes: 28 additions & 0 deletions src/lib/useMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {renderHook, waitFor} from "@testing-library/react";
import useMessages, {ChatRoomLike} from "./useMessages";
import EventEmitter from "eventemitter3";
import {ChatRoomEvents} from "./ChatRoom";

describe('useMessages', () => {
test('messages are returned', async () => {
const listener = new EventEmitter<ChatRoomEvents>();
const mockChatRoom: ChatRoomLike = {
listener,
};
const { result } = renderHook(() => useMessages(mockChatRoom));

listener.emit('text', 'ABC', 'player1');
listener.emit('text', 'XYZ', 'player2');

await waitFor(() => {
expect(result.current.length).toBe(2);
});
const messages = result.current;
expect(messages[0].text).toBe('ABC');
expect(messages[0].whose).toBe('player1');
expect(messages[0].timestamp).toBeDefined();
expect(messages[1].text).toBe('XYZ');
expect(messages[1].whose).toBe('player2');
expect(messages[1].timestamp).toBeDefined();
});
});
43 changes: 43 additions & 0 deletions src/lib/useMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {useEffect, useState} from "react";
import {Chat} from "./setup";
import {EventListener} from "./types";
import {ChatRoomEvents} from "./ChatRoom";

export interface Message {
type: 'message';
text: string;
whose: string;
timestamp: number;
}

export type Messages = Message[];

export interface ChatRoomLike {
listener: EventListener<ChatRoomEvents>;
}

export default function useMessages(
chatRoom: ChatRoomLike = Chat,
) {
const [messages, setMessages] = useState<Messages>([]);

useEffect(() => {
const textListener = (text: string, whose: string) => {
setMessages(prev => [
...prev,
{
type: 'message',
text,
whose,
timestamp: Date.now(),
},
]);
};
chatRoom.listener.on('text', textListener);
return () => {
chatRoom.listener.off('text', textListener);
}
}, [chatRoom.listener]);

return messages;
}
25 changes: 25 additions & 0 deletions src/lib/useNames.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {renderHook, waitFor} from "@testing-library/react";
import EventEmitter from "eventemitter3";
import {ChatRoomEvents} from "./ChatRoom";
import useNames, {ChatRoomLike} from "./useNames";

describe('useNames', () => {
test('names are updated', async () => {
const listener = new EventEmitter<ChatRoomEvents>();
const mockChatRoom: ChatRoomLike = {
listener,
};
const { result } = renderHook(() => useNames(mockChatRoom));

listener.emit('name', 'Alice', 'player1')
listener.emit('name', 'Bob', 'player2');

await waitFor(() => {
expect(result.current.size).toBe(2);
});

const names = result.current;
expect(names.get('player1')).toBe('Alice');
expect(names.get('player2')).toBe('Bob');
});
});
30 changes: 30 additions & 0 deletions src/lib/useNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {useEffect, useState} from "react";
import {Chat} from "./setup";
import {ChatRoomEvents} from "./ChatRoom";
import {EventListener} from "./types";

export interface ChatRoomLike {
listener: EventListener<ChatRoomEvents>;
}

export default function useNames(
chatRoom: ChatRoomLike = Chat,
) {
const [names, setNames] = useState(new Map<string, string>());

useEffect(() => {
const nameListener = (name: string, whose: string) => {
setNames(prev => {
const next = new Map(prev);
next.set(whose, name);
return next;
});
};
chatRoom.listener.on('name', nameListener);
return () => {
chatRoom.listener.off('name', nameListener);
}
}, [chatRoom.listener]);

return names;
}
Loading