Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashchange: Select the new near id if it is already rendered. #29875

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions web/src/hash_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,16 @@ export function validate_group_settings_hash(hash: string): string {
}
return hash;
}

export function is_same_narrow(hash1: string, hash2: string): boolean {
if (hash1 === hash2) {
return true;
}

// Remove elements from hash that don't effect the narrow.
// TODO: Expand the regex to include more items we can ignore.
hash1 = hash1.replace(/near\/\d+$/, "");
hash2 = hash2.replace(/near\/\d+$/, "");
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be calculated using the filter.ts module helpers -- regexs will always be inaccurate, but we can check that they have the same set of sorted operators and operands, with perhaps the exception of a near operator in one or the other.


return hash1 === hash2;
}
22 changes: 20 additions & 2 deletions web/src/hashchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import * as admin from "./admin";
import * as blueslip from "./blueslip";
import * as browser_history from "./browser_history";
import * as drafts_overlay_ui from "./drafts_overlay_ui";
import {Filter} from "./filter";
import * as hash_parser from "./hash_parser";
import * as hash_util from "./hash_util";
import {$t_html} from "./i18n";
import * as inbox_ui from "./inbox_ui";
import * as inbox_util from "./inbox_util";
import * as info_overlay from "./info_overlay";
import * as message_fetch from "./message_fetch";
import * as message_lists from "./message_lists";
import * as message_viewport from "./message_viewport";
import * as modals from "./modals";
import * as narrow from "./narrow";
Expand Down Expand Up @@ -126,7 +128,7 @@ function show_home_view() {
}

// Returns true if this function performed a narrow
function do_hashchange_normal(from_reload) {
function do_hashchange_normal(from_reload, old_hash) {
message_viewport.stop_auto_scrolling();

// NB: In Firefox, window.location.hash is URI-decoded.
Expand Down Expand Up @@ -157,6 +159,22 @@ function do_hashchange_normal(from_reload) {
show_home_view();
return false;
}

// Just select the message if the narrow is the same as the current narrow and
// we already have the new message we want to select rendered.
// TODO: Implement a filter comparison function so that we do it for any two narrows
// when calling narrow.activate.
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree with the thought that we should be doing this in narrow.activate if possible, so that if we add some helper click handler it still works as designed.

if (old_hash && hash_util.is_same_narrow(window.location.hash, old_hash)) {
const filter = new Filter(terms);
if (filter.has_operator("near")) {
const target_id = Number.parseInt(filter.operands("near")[0], 10);
if (message_lists.current.get(target_id)) {
message_lists.current.select_id(target_id, {then_scroll: true});
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if the narrow.activate call had a then_select_offset calculated, we might want to additionally scroll to that offset?

return true;
}
}
}

const narrow_opts = {
change_hash: false, // already set
trigger: "hash change",
Expand Down Expand Up @@ -473,7 +491,7 @@ function hashchanged(from_reload, e) {
popovers.hide_all();
modals.close_active_if_any();
browser_history.state.changing_hash = true;
const ret = do_hashchange_normal(from_reload);
const ret = do_hashchange_normal(from_reload, old_hash);
browser_history.state.changing_hash = false;
return ret;
}
Expand Down