Skip to content

Commit 8cfcbec

Browse files
feat: select new chat on creation
1 parent 2018251 commit 8cfcbec

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build:packages": "pnpm run --filter {./packages/crypto/**} -r build",
1111
"build": "pnpm run --filter {./packages/**} -r build",
1212
"test": "pnpm run --filter {./packages/**} -r test",
13+
"test:e2e": "pnpm --filter @app/chat-e2ee test:e2e",
1314
"typecheck": "pnpm run --filter {./packages/**} -r typecheck"
1415
},
1516
"devDependencies": {

packages/e2ee-chat/frontend/src/App.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export default function App() {
145145
[],
146146
);
147147
const [activeRoomId, setActiveRoomId] = useState<string | null>(null);
148+
const [pendingRoomSelection, setPendingRoomSelection] =
149+
useState<string | null>(null);
148150
const [mirrorsStarted, setMirrorsStarted] = useState(false);
149151
const [passwordResetPending, setPasswordResetPending] = useState(false);
150152
const [guestSupported, setGuestSupported] = useState(() =>
@@ -335,6 +337,9 @@ export default function App() {
335337

336338
useEffect(() => {
337339
if (!rooms.length) {
340+
if (pendingRoomSelection) {
341+
return;
342+
}
338343
if (activeRoomId && roomKeys.has(activeRoomId)) {
339344
return;
340345
}
@@ -343,10 +348,25 @@ export default function App() {
343348
}
344349
return;
345350
}
351+
352+
if (pendingRoomSelection) {
353+
const pendingExists = rooms.some(
354+
(room) => room.id === pendingRoomSelection,
355+
);
356+
if (!pendingExists) {
357+
return;
358+
}
359+
if (activeRoomId !== pendingRoomSelection) {
360+
setActiveRoomId(pendingRoomSelection);
361+
}
362+
setPendingRoomSelection(null);
363+
return;
364+
}
365+
346366
if (!activeRoomId || !rooms.some((room) => room.id === activeRoomId)) {
347367
setActiveRoomId(rooms[0].id);
348368
}
349-
}, [rooms, activeRoomId, roomKeys]);
369+
}, [rooms, activeRoomId, roomKeys, pendingRoomSelection]);
350370

351371
const { data: messagesData } = useQuery(
352372
`SELECT * FROM ${MESSAGES_MIRROR_TABLE} WHERE room_id = ? ORDER BY sent_at ASC`,
@@ -450,6 +470,7 @@ export default function App() {
450470
setRoomKeys(new Map());
451471
setOptimisticMessages([]);
452472
setActiveRoomId(null);
473+
setPendingRoomSelection(null);
453474
setPasswordResetPending(false);
454475
};
455476

@@ -501,6 +522,8 @@ export default function App() {
501522
return next;
502523
});
503524

525+
setPendingRoomSelection(id);
526+
504527
try {
505528
const roomCrypto = createDEKCrypto(roomKey);
506529
await insertEncrypted(
@@ -548,6 +571,7 @@ export default function App() {
548571
return id;
549572
} catch (err: any) {
550573
teardownProvisionalKey();
574+
setPendingRoomSelection(null);
551575
throw new Error(err?.message ?? "Failed to create room.");
552576
}
553577
};
@@ -718,13 +742,19 @@ export default function App() {
718742
);
719743
}
720744

745+
const handleSelectRoom = (roomId: string) => {
746+
const exists = rooms.some((room) => room.id === roomId);
747+
setPendingRoomSelection(exists ? null : roomId);
748+
setActiveRoomId(roomId);
749+
};
750+
721751
return (
722752
<ChatLayout
723753
userId={userId}
724754
mirrorsStarted={mirrorsStarted}
725755
rooms={rooms}
726756
activeRoomId={activeRoomId}
727-
onSelectRoom={setActiveRoomId}
757+
onSelectRoom={handleSelectRoom}
728758
onCreateRoom={handleCreateRoom}
729759
messages={messages}
730760
members={members

packages/e2ee-chat/frontend/tests/guest-two-party-chat.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ test("two guests can exchange messages in real time", async ({ browser }) => {
219219
if (!Array.isArray(rows)) return -1;
220220
return rows.length;
221221
}, roomId);
222-
console.log("Owner membership rows", membershipRowCount);
223222

224223
// Guest B should receive the first message.
225224
const messageFromAOnB = pageB

0 commit comments

Comments
 (0)