From d8ee5808173084af859c33e6d7c5b73019107ee2 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 7 Dec 2024 16:36:25 +0200 Subject: [PATCH] web/mainscreen: handle url hashes and update states consistently --- web/src/ui/MainScreen.tsx | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/web/src/ui/MainScreen.tsx b/web/src/ui/MainScreen.tsx index f3e7bc8..8bb10e8 100644 --- a/web/src/ui/MainScreen.tsx +++ b/web/src/ui/MainScreen.tsx @@ -149,7 +149,7 @@ class ContextFields implements MainScreenContextFields { const SYNC_ERROR_HIDE_DELAY = 30 * 1000 -const handleURLHash = (client: Client, context: ContextFields) => { +const handleURLHash = (client: Client) => { if (!location.hash.startsWith("#/uri/")) { if (location.search) { const currentETag = ( @@ -181,28 +181,31 @@ const handleURLHash = (client: Client, context: ContextFields) => { newURL.hash = "" newURL.search = "" if (uri.identifier.startsWith("@")) { - const right_panel = { - type: "user", - userID: uri.identifier, - } as RightPanelProps - history.replaceState({ right_panel }, "", newURL.toString()) - context.setRightPanel(right_panel, false) + const newState = { + right_panel: { + type: "user", + userID: uri.identifier, + }, + } + history.replaceState(newState, "", newURL.toString()) + return newState } else if (uri.identifier.startsWith("!")) { - history.replaceState({ room_id: uri.identifier }, "", newURL.toString()) - context.setActiveRoom(uri.identifier, false) + const newState = { room_id: uri.identifier } + history.replaceState(newState, "", newURL.toString()) + return newState } else if (uri.identifier.startsWith("#")) { // TODO loading indicator or something for this? client.rpc.resolveAlias(uri.identifier).then( res => { - history.replaceState({ room_id: res.room_id }, "", newURL.toString()) - context.setActiveRoom(res.room_id, false) + history.pushState({ room_id: res.room_id }, "", newURL.toString()) }, err => window.alert(`Failed to resolve room alias ${uri.identifier}: ${err}`), ) + return null } else { console.error("Invalid matrix URI", uri) } - return null + return history.state } const MainScreen = () => { @@ -227,7 +230,7 @@ const MainScreen = () => { } window.addEventListener("popstate", listener) const initHandle = () => { - const state = handleURLHash(client, context) + const state = handleURLHash(client) listener({ state } as PopStateEvent) } let cancel = () => {}