Fix issues with browser history
This commit is contained in:
1 parent
2c0e5bb4dc
commit
f5ce3af186
2 files changed
+15
-4
No files matched your search
+11
-3
@@ -75,8 +75,16 @@ export default function App() {
|
|||||||
return () => window.removeEventListener('popstate', onPopState)
|
return () => window.removeEventListener('popstate', onPopState)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const navigate = (tab, room = null, noguard = false) => {
|
// `replace` swaps the current entry instead of adding one. Deliberate joins
|
||||||
window.history.pushState({}, '', pathFor(tab, room, noguard))
|
// use it so the URL that pointed at the room (and the guard page behind it)
|
||||||
|
// does not stay in history — otherwise going back would land on it and rejoin.
|
||||||
|
const navigate = (tab, room = null, noguard = false, replace = false) => {
|
||||||
|
const path = pathFor(tab, room, noguard)
|
||||||
|
if (replace) {
|
||||||
|
window.history.replaceState({}, '', path)
|
||||||
|
} else {
|
||||||
|
window.history.pushState({}, '', path)
|
||||||
|
}
|
||||||
setRoute({ tab, room: tab === 'rooms' ? room || null : null, noguard })
|
setRoute({ tab, room: tab === 'rooms' ? room || null : null, noguard })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +141,7 @@ export default function App() {
|
|||||||
<RoomsPage
|
<RoomsPage
|
||||||
room={route.room}
|
room={route.room}
|
||||||
noguard={route.noguard}
|
noguard={route.noguard}
|
||||||
onNavigate={(room, noguard) => navigate('rooms', room, noguard)}
|
onNavigate={(room, noguard, replace) => navigate('rooms', room, noguard, replace)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{activeTab === 'profile' && <ProfilePage user={user} onUserChange={setUser} />}
|
{activeTab === 'profile' && <ProfilePage user={user} onUserChange={setUser} />}
|
||||||
|
|||||||
@@ -76,7 +76,10 @@ export default function RoomsPage({ room: routeRoom, noguard, onNavigate }) {
|
|||||||
if (!target) return
|
if (!target) return
|
||||||
entered.current = target
|
entered.current = target
|
||||||
setPending(null)
|
setPending(null)
|
||||||
onNavigate(target, true)
|
// If a room URL is already open (the guard page, or a previously visited
|
||||||
|
// room), replace that history entry so going back does not land on it and
|
||||||
|
// join again. From a room-less URL, push so the pre-join page is kept.
|
||||||
|
onNavigate(target, true, Boolean(routeRoom))
|
||||||
enter(target)
|
enter(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user