@@ -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
0 commit comments