Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_view: Select `near` message without rerender.
Aman Agrawal6999d84ddc2c
diff --git a/web/src/filter.ts b/web/src/filter.tsindex 85aecdbc56a..7b005039956 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -726,6 +726,28 @@ export class Filter {return true;}+ equals(filter: Filter, excluded_operators?: string[]): boolean {+ return _.isEqual(+ filter.sorted_terms(excluded_operators),+ this.sorted_terms(excluded_operators),+ );+ }++ sorted_terms(excluded_operators?: string[]): NarrowTerm[] {+ let filter_terms = this._terms;+ if (excluded_operators) {+ filter_terms = this._terms.filter(+ (term) => !excluded_operators.includes(term.operator),+ );+ }++ return filter_terms.sort((a, b) => {+ const a_joined = `${a.negated ? "0" : "1"}-${a.operator}-${a.operand}`;+ const b_joined = `${b.negated ? "0" : "1"}-${b.operator}-${b.operand}`;+ return util.strcmp(a_joined, b_joined);+ });+ }+predicate(): (message: Message) => boolean {if (this._predicate === undefined) {this._predicate = this._build_predicate();diff --git a/web/src/message_view.js b/web/src/message_view.jsindex e46f30c8ce1..e5d3fad2b52 100644--- a/web/src/message_view.js+++ b/web/src/message_view.js@@ -219,6 +219,50 @@ function handle_post_message_list_change(compose_recipient.handle_middle_pane_transition();}+function try_rendering_locally_for_same_narrow(filter, opts) {+ if (opts.then_select_id || opts.then_select_offset) {+ // This function is designed to navigate user to a target message+ // specified via `near` operator, which doesn't handle+ // `then_select_id` or `then_select_offset` to avoid targeting+ // the wrong message.+ return false;+ }++ const current_filter = narrow_state.filter();+ if (!current_filter) {+ return false;+ }++ // If the difference between the current filter and the new filter+ // is just a `near` operator, or just the value of a `near` operator,+ // we can render the new filter without a rerender of the message list+ // if the target message in the `near` operator is already rendered.+ const excluded_operators = ["near"];+ if (!filter.equals(current_filter, excluded_operators)) {+ return false;+ }++ if (filter.has_operator("near")) {+ const target_id = Number.parseInt(filter.operands("near")[0], 10);+ if (!message_lists.current?.get(target_id)) {+ return false;+ }++ const currently_selected_id = message_lists.current?.selected_id();+ if (currently_selected_id !== target_id) {+ message_lists.current.select_id(target_id, {+ then_scroll: true,+ });+ }++ message_lists.current.data.filter = filter;+ update_hash_to_match_filter(filter);+ return true;+ }++ return false;+}+export function show(raw_terms, opts) {/* Main entry point for switching to a new view / message list.@@ -256,6 +300,11 @@ export function show(raw_terms, opts) {raw_terms = [{operator: "is", operand: "home"}];}const filter = new Filter(raw_terms);++ if (try_rendering_locally_for_same_narrow(filter, opts)) {+ return;+ }+const is_combined_feed_global_view = filter.is_in_home();const is_narrowed_to_combined_feed_view = narrow_state.filter()?.is_in_home();if (is_narrowed_to_combined_feed_view && is_combined_feed_global_view) {diff --git a/web/tests/filter.test.js b/web/tests/filter.test.jsindex 193f14a0e4e..8abc942d0cf 100644--- a/web/tests/filter.test.js+++ b/web/tests/filter.test.js@@ -2165,3 +2165,42 @@ run_test("is_in_home", () => {]);assert.ok(!filter3.is_in_home());});++run_test("equals", () => {+ let terms = [{operator: "channel", operand: "Foo"}];+ let filter = new Filter(terms);++ assert.ok(filter.equals(new Filter(terms)));+ assert.ok(!filter.equals(new Filter([])));+ assert.ok(!filter.equals(new Filter([{operand: "Bar", operator: "channel"}])));+ assert.ok(!filter.equals(new Filter([...terms, {operator: "topic", operand: "Bar"}])));++ terms = [+ {operator: "channel", operand: "Foo"},+ {operator: "topic", operand: "Bar"},+ ];+ filter = new Filter(terms);+ assert.ok(+ filter.equals(+ new Filter([+ {operator: "topic", operand: "Bar"},+ {operator: "channel", operand: "Foo"},+ ]),+ ),+ );+ assert.ok(!filter.equals(new Filter([...terms, {operator: "near", operand: "10"}])));++ // Exclude `near` operator from comparison.+ assert.ok(filter.equals(new Filter([...terms, {operator: "near", operand: "10"}]), ["near"]));+ assert.ok(+ filter.equals(+ new Filter([+ {operator: "near", operand: "10"},+ {operator: "topic", operand: "Bar"},+ {operator: "channel", operand: "Foo"},+ {operator: "near", operand: "101"},+ ]),+ ["near"],+ ),+ );+});