Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
hashchange: Extract function to get home view hash.
Aman Agrawalfd441fd3d3a0
diff --git a/web/src/browser_history.ts b/web/src/browser_history.tsindex 349dccc3cff..316c35ba842 100644--- a/web/src/browser_history.ts+++ b/web/src/browser_history.ts@@ -172,3 +172,26 @@ export function update_current_history_state_data(new_data: StateData): void {const state_data = {...current_state_data, ...new_data};window.history.replaceState(state_data, "", window.location.href);}++export function get_home_view_hash(): string {+ let home_view_hash = `#${user_settings.web_home_view}`;+ if (home_view_hash === "#recent_topics") {+ home_view_hash = "#recent";+ }++ if (home_view_hash === "#all_messages") {+ home_view_hash = "#feed";+ }++ return home_view_hash;+}++export function is_current_hash_home_view(): boolean {+ const current_hash = window.location.hash;+ if (current_hash === "") {+ // Empty hash for home view is always valid.+ return true;+ }++ return current_hash === get_home_view_hash();+}diff --git a/web/src/hashchange.js b/web/src/hashchange.jsindex 31f4cfa9d41..b4aab8e4c76 100644--- a/web/src/hashchange.js+++ b/web/src/hashchange.js@@ -91,44 +91,31 @@ function get_user_settings_tab(section) {}export function set_hash_to_home_view(triggered_by_escape_key = false) {- const current_hash = window.location.hash;- if (current_hash === "") {- // Empty hash for home view is always valid.+ if (browser_history.is_current_hash_home_view()) {return;}- let home_view_hash = `#${user_settings.web_home_view}`;- if (home_view_hash === "#recent_topics") {- home_view_hash = "#recent";- }-- if (home_view_hash === "#all_messages") {- home_view_hash = "#feed";+ const hash_before_current = browser_history.old_hash();+ if (+ triggered_by_escape_key &&+ browser_history.get_home_view_hash() === "#feed" &&+ (hash_before_current === "" || hash_before_current === "#feed")+ ) {+ // If the previous view was the user's Combined Feed home+ // view, and this change was triggered by escape keypress,+ // then we simulate the back button in order to reuse+ // existing code for restoring the user's scroll position.+ window.history.back();+ return;}- if (current_hash !== home_view_hash) {- const hash_before_current = browser_history.old_hash();- if (- triggered_by_escape_key &&- home_view_hash === "#feed" &&- (hash_before_current === "" || hash_before_current === "#feed")- ) {- // If the previous view was the user's Combined Feed home- // view, and this change was triggered by escape keypress,- // then we simulate the back button in order to reuse- // existing code for restoring the user's scroll position.- window.history.back();- return;- }-- // We want to set URL with no hash here. It is not possible- // to do so with `window.location.hash` since it will set an empty- // hash. So, we use `pushState` which simply updates the current URL- // but doesn't trigger `hashchange`. So, we trigger hashchange directly- // here to let it handle the whole rendering process for us.- browser_history.set_hash("");- hashchanged(false);- }+ // We want to set URL with no hash here. It is not possible+ // to do so with `window.location.hash` since it will set an empty+ // hash. So, we use `pushState` which simply updates the current URL+ // but doesn't trigger `hashchange`. So, we trigger hashchange directly+ // here to let it handle the whole rendering process for us.+ browser_history.set_hash("");+ hashchanged(false);}function show_home_view() {