narrow: Refactor logic for getting narrow title for browser/tab.

Creates a new function, `compute_narrow_title`, that returns a
string for the narrow title based on the current filter, so that
`update_narrow_title` can call that function, and then use the
returned string to set the narrow title.

Adds a node test for the new `compute_narrow_title` function for
cases that differ from the title generated by `filter.get_title`.
This commit is contained in:
Lauryn Menard 2022-12-15 15:03:56 +01:00 committed by Tim Abbott
parent b6067b63b8
commit 7686bca385
2 changed files with 89 additions and 33 deletions

View File

@ -4,6 +4,7 @@ const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
@ -813,3 +814,53 @@ run_test("narrow_to_compose_target PMs", ({override, override_rewire}) => {
assert.equal(args.called, true);
assert.deepEqual(args.operators, [{operator: "is", operand: "private"}]);
});
run_test("narrow_compute_title", () => {
// Only tests cases where the narrow title is different from the filter title.
let filter;
// Search & uncommon narrows
filter = new Filter([{operator: "search", operand: "potato"}]);
assert.equal(narrow.compute_narrow_title(filter), "translated: Search results");
filter = new Filter([{operator: "sender", operand: "me"}]);
assert.equal(narrow.compute_narrow_title(filter), "translated: Search results");
// Stream narrows
const sub = {
name: "Foo",
stream_id: 43,
};
stream_data.add_sub(sub);
filter = new Filter([
{operator: "stream", operand: "foo"},
{operator: "topic", operand: "bar"},
]);
assert.equal(narrow.compute_narrow_title(filter), "#Foo > bar");
filter = new Filter([{operator: "stream", operand: "foo"}]);
assert.equal(narrow.compute_narrow_title(filter), "#Foo");
filter = new Filter([{operator: "stream", operand: "Elephant"}]);
assert.equal(narrow.compute_narrow_title(filter), "translated: Unknown stream #Elephant");
// Private messages with narrows
const joe = {
email: "joe@example.com",
user_id: 31,
full_name: "joe",
};
people.add_active_user(joe);
filter = new Filter([{operator: "pm-with", operand: "joe@example.com"}]);
assert.equal(narrow.compute_narrow_title(filter), "joe");
filter = new Filter([{operator: "pm-with", operand: "joe@example.com,sally@doesnotexist.com"}]);
blueslip.expect("warn", "Unknown emails: joe@example.com,sally@doesnotexist.com");
assert.equal(narrow.compute_narrow_title(filter), "translated: Invalid users");
filter = new Filter([{operator: "pm-with", operand: "sally@doesnotexist.com"}]);
blueslip.expect("warn", "Unknown emails: sally@doesnotexist.com");
assert.equal(narrow.compute_narrow_title(filter), "translated: Invalid user");
});

View File

@ -124,42 +124,47 @@ export function set_narrow_title(title) {
notifications.redraw_title();
}
function update_narrow_title(filter) {
export function compute_narrow_title(filter) {
const filter_title = filter.get_title();
const search_default = $t({defaultMessage: "Search results"});
if (filter_title !== undefined) {
if (filter.has_operator("stream")) {
if (!filter._sub) {
// The stream is not set because it does not currently
// exist (possibly due to a stream name change), or it
// is a private stream and the user is not subscribed.
set_narrow_title(filter_title);
} else if (filter.has_operator("topic")) {
const topic_name = filter.operands("topic")[0];
set_narrow_title("#" + filter_title + " > " + topic_name);
} else {
set_narrow_title("#" + filter_title);
}
} else if (filter.has_operator("pm-with")) {
const emails = filter.operands("pm-with")[0];
const user_ids = people.emails_strings_to_user_ids_string(emails);
if (user_ids !== undefined) {
const names = people.get_recipients(user_ids);
set_narrow_title(names);
} else {
if (emails.includes(",")) {
set_narrow_title("Invalid users");
} else {
set_narrow_title("Invalid user");
}
}
} else {
set_narrow_title(filter_title);
}
} else {
set_narrow_title(search_default);
if (filter_title === undefined) {
// Default result for uncommon narrow/search views.
return $t({defaultMessage: "Search results"});
}
if (filter.has_operator("stream")) {
if (!filter._sub) {
// The stream is not set because it does not currently
// exist (possibly due to a stream name change), or it
// is a private stream and the user is not subscribed.
return filter_title;
}
if (filter.has_operator("topic")) {
const topic_name = filter.operands("topic")[0];
return "#" + filter_title + " > " + topic_name;
}
return "#" + filter_title;
}
if (filter.has_operator("pm-with")) {
const emails = filter.operands("pm-with")[0];
const user_ids = people.emails_strings_to_user_ids_string(emails);
if (user_ids !== undefined) {
return people.get_recipients(user_ids);
}
if (emails.includes(",")) {
return $t({defaultMessage: "Invalid users"});
}
return $t({defaultMessage: "Invalid user"});
}
return filter_title;
}
function update_narrow_title(filter) {
const narrow_title_string = compute_narrow_title(filter);
set_narrow_title(narrow_title_string);
}
export function reset_ui_state() {