Skip to content

Commit

Permalink
narrow: Update hash for moved message even if hash change in progress.
Browse files Browse the repository at this point in the history
Adds a new trigger string to use for narrow.activate opts when
it is called due detecting a message move for the targeted message
ID: "retarget message location".

Updates narrow.save_narrow and narrow.hashchange to accept the
trigger as a parameter so that, even if the narrow was started via
a hash change in the web app, the URL and browser history is
updated for the current location of the targeted message.
  • Loading branch information
laurynmm authored and timabbott committed Apr 30, 2024
1 parent d32d443 commit d512652
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 14 additions & 6 deletions web/src/narrow.js
Expand Up @@ -74,20 +74,25 @@ export function reset_ui_state() {
compose_banner.clear_message_sent_banners();
}

export function changehash(newhash) {
export function changehash(newhash, trigger) {
if (browser_history.state.changing_hash) {
// If we retargeted the narrow operation because a message was moved,
// we want to have the current narrow hash in the browser history.
if (trigger === "retarget message location") {
window.location.replace(newhash);
}
return;
}
message_viewport.stop_auto_scrolling();
browser_history.set_hash(newhash);
}

export function save_narrow(terms) {
if (browser_history.state.changing_hash) {
export function save_narrow(terms, trigger) {
if (browser_history.state.changing_hash && trigger !== "retarget message location") {
return;
}
const new_hash = hash_util.search_terms_to_hash(terms);
changehash(new_hash);
changehash(new_hash, trigger);
}

export function activate(raw_terms, opts) {
Expand Down Expand Up @@ -282,6 +287,7 @@ export function activate(raw_terms, opts) {
...opts,
// Update the URL fragment to reflect the redirect.
change_hash: true,
trigger: "retarget message location",
});
return;
}
Expand Down Expand Up @@ -314,6 +320,7 @@ export function activate(raw_terms, opts) {
...opts,
// Update the URL fragment to reflect the redirect.
change_hash: true,
trigger: "retarget message location",
});
return;
}
Expand Down Expand Up @@ -587,9 +594,10 @@ export function activate(raw_terms, opts) {

// Put the narrow terms in the URL fragment.
// Disabled when the URL fragment was the source
// of this narrow.
// of this narrow, but not if the fragment had
// a target message ID that has been moved.
if (opts.change_hash) {
save_narrow(terms);
save_narrow(terms, opts.trigger);
}

handle_post_view_change(msg_list);
Expand Down
5 changes: 3 additions & 2 deletions web/src/reload_setup.js
Expand Up @@ -15,6 +15,7 @@ export function initialize() {
return;
}
const hash_fragment = location.hash.slice("#".length);
const trigger = "reload";

// Using the token, recover the saved pre-reload data from local
// storage. Afterwards, we clear the reload entry from local
Expand All @@ -27,7 +28,7 @@ export function initialize() {
// exist, but be log it so that it's available for future
// debugging if an exception happens later.
blueslip.info("Invalid hash change reload token");
narrow.changehash("");
narrow.changehash("", trigger);
return;
}
ls.remove(hash_fragment);
Expand Down Expand Up @@ -82,5 +83,5 @@ export function initialize() {
});

activity.set_new_user_input(false);
narrow.changehash(vars.oldhash);
narrow.changehash(vars.oldhash, trigger);
}

0 comments on commit d512652

Please sign in to comment.