Skip to content
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
31 changes: 31 additions & 0 deletions e2e/classroom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@ test.describe('lobby', () => {
await expect(page.getByRole('button', { name: 'Lock Board' })).toHaveCount(0);
});

test('typing a room code in the lobby changes neither the address, the tab title nor the connection', async ({ page }) => {
const room = uniqueRoom('typed');
// A socket.io handshake (a new connection) is a polling request with no session id yet.
let handshakes = 0;
page.on('request', (req) => {
const url = req.url();
if (url.includes('/socket.io/') && !url.includes('sid=')) handshakes += 1;
});
await page.goto('/');
await expect(page.getByLabel('Room Code')).toBeVisible();
await expect.poll(() => handshakes).toBeGreaterThan(0);
const before = handshakes;

// The box starts out holding "main"; clear it, then type one letter at a time like a person would.
await page.getByLabel('Room Code').fill('');
await page.getByLabel('Room Code').pressSequentially(room);
await expect(page.getByLabel('Room Code')).toHaveValue(room);
await page.waitForTimeout(500);
expect(new URL(page.url()).searchParams.get('room')).toBeNull();
await expect(page).toHaveTitle('AllAboard');
expect(handshakes).toBe(before);

// Joining is what moves the address and the title to the room.
await page.getByLabel('Your Display Name').fill('Alex');
await page.getByRole('button', { name: 'Join Classroom' }).click();
await expect(page.locator('.konvajs-content').first()).toBeVisible();
await expect(page).toHaveURL(new RegExp(`room=${room}`));
await expect(page).toHaveTitle(`#${room} · AllAboard`);
await expect(page.getByPlaceholder('Message the class...')).toBeVisible();
});

test('a returning user is rejoined automatically after a reload', async ({ page }) => {
const room = uniqueRoom('reload');
await joinRoom(page, room, 'Alex');
Expand Down
32 changes: 21 additions & 11 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,30 @@ export default function App() {
}, [breakoutConfig.chatScope, activeBoardId, subBoards, messages]);

const effectiveRoomId = normalizeRoomCode(roomId);
/**
* The room the socket, the address bar and the tab title follow: the room you're in. In the lobby it holds still
* while the Room Code box is typed in. Following the box made a new server connection, and (with the title) a
* browser-history entry, for every letter typed.
*/
const [liveRoomId, setLiveRoomId] = useState<string>(effectiveRoomId);
useEffect(() => {
if (isJoined) setLiveRoomId(effectiveRoomId);
}, [isJoined, effectiveRoomId]);

useEffect(() => {
document.title = effectiveRoomId !== 'main' ? `#${effectiveRoomId} · AllAboard` : 'AllAboard';
document.title = liveRoomId !== 'main' ? `#${liveRoomId} · AllAboard` : 'AllAboard';
try {
const url = new URL(window.location.href);
if (effectiveRoomId === 'main') {
if (liveRoomId === 'main') {
url.searchParams.delete('room');
} else {
url.searchParams.set('room', effectiveRoomId);
url.searchParams.set('room', liveRoomId);
}
window.history.replaceState({}, '', url.toString());
if (url.toString() !== window.location.href) window.history.replaceState({}, '', url.toString());
} catch {
// Ignore
}
}, [effectiveRoomId]);
}, [liveRoomId]);

useEffect(() => {
const newSocket = io({
Expand Down Expand Up @@ -383,15 +392,15 @@ export default function App() {

try {
savedName = localStorage.getItem(STORAGE_KEYS.USER_NAME) || savedName;
savedPin = localStorage.getItem(STORAGE_KEYS.TEACHER_PIN_PREFIX + effectiveRoomId) || undefined;
savedPin = localStorage.getItem(STORAGE_KEYS.TEACHER_PIN_PREFIX + liveRoomId) || undefined;
} catch {
// Ignore storage errors
}

if (savedName && savedName.trim() && savedJoined) {
const cleanSavedPin = savedPin && savedPin.trim().length >= 3 ? savedPin.trim() : undefined;
newSocket.emit('join-room', {
roomId: effectiveRoomId,
roomId: liveRoomId,
userId,
userKey,
name: savedName.trim(),
Expand Down Expand Up @@ -456,7 +465,7 @@ export default function App() {
// Ignore
}
}
recordRoomHistory(initRoom?.roomId || effectiveRoomId);
recordRoomHistory(initRoom?.roomId || liveRoomId);
setHistoryVersion((v) => v + 1);
setKickNotice(null);
}
Expand Down Expand Up @@ -924,7 +933,7 @@ export default function App() {
incomingInk.clear();
newSocket.close();
};
}, [userId, userColor, effectiveRoomId]);
}, [userId, userColor, liveRoomId]);

// Teachers get live traffic only for the board they're looking at; tell the server whenever that changes
// (and after every reconnect, since the server forgets per-socket state).
Expand Down Expand Up @@ -1334,7 +1343,8 @@ export default function App() {
// Ignore
}
setUserName(trimmedName);
if (socket && targetRoomId === effectiveRoomId) {
// Same room the socket is on: join now. Otherwise the socket reconnects for the new room and joins on connect.
if (socket && targetRoomId === liveRoomId) {
socket.emit('join-room', {
roomId: targetRoomId,
userId,
Expand All @@ -1353,7 +1363,7 @@ export default function App() {
setHasEditedLobbyRoom(false);
setIsJoined(true);
},
[roomId, effectiveRoomId, socket, userId, userKey, userColor]
[roomId, liveRoomId, socket, userId, userKey, userColor]
);

const handleSwitchRoom = useCallback(
Expand Down
Loading