Skip to content

Commit

Permalink
narrow: Extract narrow_title module.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <anders@zulip.com>
  • Loading branch information
andersk committed Oct 5, 2023
1 parent b6a2584 commit d5064fc
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 135 deletions.
1 change: 1 addition & 0 deletions tools/test-js-with-node
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ EXEMPT_FILES = make_set(
"web/src/muted_users_ui.js",
"web/src/narrow.js",
"web/src/narrow_history.js",
"web/src/narrow_title.js",
"web/src/navbar_alerts.js",
"web/src/navigate.js",
"web/src/notifications.js",
Expand Down
77 changes: 2 additions & 75 deletions web/src/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import * as condense from "./condense";
import {Filter} from "./filter";
import * as hash_parser from "./hash_parser";
import * as hashchange from "./hashchange";
import {$t} from "./i18n";
import * as inbox_ui from "./inbox_ui";
import * as inbox_util from "./inbox_util";
import * as left_sidebar_navigation_area from "./left_sidebar_navigation_area";
Expand All @@ -32,7 +31,7 @@ import * as message_view_header from "./message_view_header";
import * as narrow_banner from "./narrow_banner";
import * as narrow_history from "./narrow_history";
import * as narrow_state from "./narrow_state";
import * as notifications from "./notifications";
import * as narrow_title from "./narrow_title";
import {page_params} from "./page_params";
import * as people from "./people";
import * as pm_list from "./pm_list";
Expand Down Expand Up @@ -76,78 +75,6 @@ export function save_pre_narrow_offset_for_reload() {

export let has_shown_message_list_view = false;

export function compute_narrow_title(filter) {
if (filter === undefined) {
// "All messages" and "Recent conversations" views have
// an `undefined` filter.
if (recent_view_util.is_visible()) {
return $t({defaultMessage: "Recent conversations"});
}

if (inbox_util.is_visible()) {
return $t({defaultMessage: "Inbox"});
}
}

const filter_title = filter.get_title();

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("dm")) {
const emails = filter.operands("dm")[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"});
}

if (filter.has_operator("sender")) {
const user = people.get_by_email(filter.operands("sender")[0]);
if (user) {
if (people.is_my_user_id(user.user_id)) {
return $t({defaultMessage: "Messages sent by you"});
}
return $t(
{defaultMessage: "Messages sent by {sender}"},
{
sender: user.full_name,
},
);
}
return $t({defaultMessage: "Invalid user"});
}

return filter_title;
}

export let narrow_title = "home";
export function update_narrow_title(filter) {
narrow_title = compute_narrow_title(filter);
notifications.redraw_title();
}

export function reset_ui_state() {
// Resets the state of various visual UI elements that are
// a function of the current narrow.
Expand Down Expand Up @@ -1015,7 +942,7 @@ function handle_post_view_change(msg_list) {
compose_closed_ui.update_reply_recipient_label();

message_view_header.render_title_area();
update_narrow_title(filter);
narrow_title.update_narrow_title(filter);
left_sidebar_navigation_area.handle_narrow_activated(filter);
stream_list.handle_narrow_activated(filter);
pm_list.handle_narrow_activated(filter);
Expand Down
117 changes: 117 additions & 0 deletions web/src/narrow_title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import * as favicon from "./favicon";
import {$t} from "./i18n";
import * as inbox_util from "./inbox_util";
import {page_params} from "./page_params";
import * as people from "./people";
import * as recent_view_util from "./recent_view_util";
import * as unread from "./unread";

export let unread_count = 0;
let pm_count = 0;
export let narrow_title = "home";

export function compute_narrow_title(filter) {
if (filter === undefined) {
// "All messages" and "Recent conversations" views have
// an `undefined` filter.
if (recent_view_util.is_visible()) {
return $t({defaultMessage: "Recent conversations"});
}

if (inbox_util.is_visible()) {
return $t({defaultMessage: "Inbox"});
}
}

const filter_title = filter.get_title();

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("dm")) {
const emails = filter.operands("dm")[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"});
}

if (filter.has_operator("sender")) {
const user = people.get_by_email(filter.operands("sender")[0]);
if (user) {
if (people.is_my_user_id(user.user_id)) {
return $t({defaultMessage: "Messages sent by you"});
}
return $t(
{defaultMessage: "Messages sent by {sender}"},
{
sender: user.full_name,
},
);
}
return $t({defaultMessage: "Invalid user"});
}

return filter_title;
}

export function redraw_title() {
// Update window title to reflect unread messages in current view
const new_title =
(unread_count ? "(" + unread_count + ") " : "") +
narrow_title +
" - " +
page_params.realm_name +
" - " +
"Zulip";

document.title = new_title;
}

export function update_unread_counts(counts) {
const new_unread_count = unread.calculate_notifiable_count(counts);
const new_pm_count = counts.direct_message_count;
if (new_unread_count === unread_count && new_pm_count === pm_count) {
return;
}

unread_count = new_unread_count;
pm_count = new_pm_count;

// Indicate the message count in the favicon
favicon.update_favicon(unread_count, pm_count);

// Notify the current desktop app's UI about the new unread count.
if (window.electron_bridge !== undefined) {
window.electron_bridge.send_event("total_unread_count", unread_count);
}

// TODO: Add a `window.electron_bridge.updateDirectMessageCount(new_pm_count);` call?
redraw_title();
}

export function update_narrow_title(filter) {
narrow_title = compute_narrow_title(filter);
redraw_title();
}
41 changes: 0 additions & 41 deletions web/src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import render_unmute_topic_banner from "../templates/compose_banner/unmute_topic
import * as alert_words from "./alert_words";
import * as blueslip from "./blueslip";
import * as compose_banner from "./compose_banner";
import * as favicon from "./favicon";
import * as hash_util from "./hash_util";
import {$t} from "./i18n";
import * as message_lists from "./message_lists";
import * as message_parser from "./message_parser";
import * as narrow from "./narrow";
import * as narrow_state from "./narrow_state";
import {page_params} from "./page_params";
import * as people from "./people";
import * as spoilers from "./spoilers";
import * as stream_data from "./stream_data";
Expand Down Expand Up @@ -102,45 +100,6 @@ export function permission_state() {
return NotificationAPI.permission;
}

let unread_count = 0;
let pm_count = 0;

export function redraw_title() {
// Update window title to reflect unread messages in current view
const new_title =
(unread_count ? "(" + unread_count + ") " : "") +
narrow.narrow_title +
" - " +
page_params.realm_name +
" - " +
"Zulip";

document.title = new_title;
}

export function update_unread_counts(counts) {
const new_unread_count = unread.calculate_notifiable_count(counts);
const new_pm_count = counts.direct_message_count;
if (new_unread_count === unread_count && new_pm_count === pm_count) {
return;
}

unread_count = new_unread_count;
pm_count = new_pm_count;

// Indicate the message count in the favicon
favicon.update_favicon(unread_count, pm_count);

// Notify the current desktop app's UI about the new unread count.
if (window.electron_bridge !== undefined) {
window.electron_bridge.send_event("total_unread_count", unread_count);
}

// TODO: Add a `window.electron_bridge.updateDirectMessageCount(new_pm_count);` call?

redraw_title();
}

function notify_unmute(muted_narrow, stream_id, topic_name) {
const $unmute_notification = $(
render_unmute_topic_banner({
Expand Down
3 changes: 2 additions & 1 deletion web/src/server_events_dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as message_lists from "./message_lists";
import * as message_live_update from "./message_live_update";
import * as muted_users_ui from "./muted_users_ui";
import * as narrow_state from "./narrow_state";
import * as narrow_title from "./narrow_title";
import * as navbar_alerts from "./navbar_alerts";
import * as notifications from "./notifications";
import * as overlays from "./overlays";
Expand Down Expand Up @@ -224,7 +225,7 @@ export function dispatch_normal_event(event) {
move_messages_within_stream_limit_seconds: message_edit.update_inline_topic_edit_ui,
message_retention_days: noop,
move_messages_between_streams_policy: noop,
name: notifications.redraw_title,
name: narrow_title.redraw_title,
name_changes_disabled: settings_account.update_name_change_display,
notifications_stream_id: stream_ui_updates.update_announce_stream_option,
org_type: noop,
Expand Down
3 changes: 2 additions & 1 deletion web/src/ui_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import * as muted_users from "./muted_users";
import * as narrow from "./narrow";
import * as narrow_history from "./narrow_history";
import * as narrow_state from "./narrow_state";
import * as narrow_title from "./narrow_title";
import * as navbar_alerts from "./navbar_alerts";
import * as navigate from "./navigate";
import * as notifications from "./notifications";
Expand Down Expand Up @@ -383,7 +384,7 @@ function initialize_unread_ui() {
);
unread_ui.register_update_unread_counts_hook(() => topic_list.update());
unread_ui.register_update_unread_counts_hook((counts) =>
notifications.update_unread_counts(counts),
narrow_title.update_unread_counts(counts),
);
unread_ui.register_update_unread_counts_hook(inbox_ui.update);

Expand Down
3 changes: 2 additions & 1 deletion web/src/views_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import $ from "jquery";
import * as message_view_header from "./message_view_header";
import * as narrow from "./narrow";
import * as narrow_state from "./narrow_state";
import * as narrow_title from "./narrow_title";
import * as navigate from "./navigate";
import * as pm_list from "./pm_list";
import * as resize from "./resize";
Expand Down Expand Up @@ -35,7 +36,7 @@ export function show(opts) {
unread_ui.hide_unread_banner();
opts.update_compose();
narrow_state.reset_current_filter();
narrow.update_narrow_title(narrow_state.filter());
narrow_title.update_narrow_title(narrow_state.filter());
message_view_header.render_title_area();
narrow.handle_middle_pane_transition();
search.clear_search_form();
Expand Down
3 changes: 2 additions & 1 deletion web/tests/dispatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const message_events = mock_esm("../src/message_events");
const message_lists = mock_esm("../src/message_lists");
const user_topics_ui = mock_esm("../src/user_topics_ui");
const muted_users_ui = mock_esm("../src/muted_users_ui");
const narrow_title = mock_esm("../src/narrow_title");
const notifications = mock_esm("../src/notifications");
const pm_list = mock_esm("../src/pm_list");
const reactions = mock_esm("../src/reactions");
Expand Down Expand Up @@ -420,7 +421,7 @@ run_test("realm settings", ({override}) => {
override(settings_invites, "update_invite_user_panel", noop);
override(sidebar_ui, "update_invite_user_option", noop);
override(gear_menu, "initialize", noop);
override(notifications, "redraw_title", noop);
override(narrow_title, "redraw_title", noop);

function test_electron_dispatch(event, fake_send_event) {
with_overrides(({override}) => {
Expand Down
Loading

0 comments on commit d5064fc

Please sign in to comment.