2023-12-22 06:02:47 +01:00
|
|
|
import assert from "minimalistic-assert";
|
|
|
|
|
2023-10-05 23:18:00 +02:00
|
|
|
import * as favicon from "./favicon";
|
2023-12-22 06:02:47 +01:00
|
|
|
import type {Filter} from "./filter";
|
2023-10-05 23:18:00 +02:00
|
|
|
import {$t} from "./i18n";
|
|
|
|
import * as inbox_util from "./inbox_util";
|
|
|
|
import * as people from "./people";
|
|
|
|
import * as recent_view_util from "./recent_view_util";
|
2024-02-13 02:08:24 +01:00
|
|
|
import {realm} from "./state_data";
|
2023-10-05 23:18:00 +02:00
|
|
|
import * as unread from "./unread";
|
2023-12-22 06:02:47 +01:00
|
|
|
import type {FullUnreadCountsData} from "./unread";
|
2023-10-05 23:18:00 +02:00
|
|
|
|
|
|
|
export let unread_count = 0;
|
|
|
|
let pm_count = 0;
|
|
|
|
export let narrow_title = "home";
|
|
|
|
|
2023-12-22 06:02:47 +01:00
|
|
|
export function compute_narrow_title(filter?: Filter): string {
|
2023-10-05 23:18:00 +02:00
|
|
|
if (filter === undefined) {
|
|
|
|
// "All messages" and "Recent conversations" views have
|
|
|
|
// an `undefined` filter.
|
|
|
|
if (recent_view_util.is_visible()) {
|
|
|
|
return $t({defaultMessage: "Recent conversations"});
|
|
|
|
}
|
|
|
|
|
2023-12-22 06:02:47 +01:00
|
|
|
assert(inbox_util.is_visible());
|
|
|
|
return $t({defaultMessage: "Inbox"});
|
2023-10-05 23:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const filter_title = filter.get_title();
|
|
|
|
|
|
|
|
if (filter_title === undefined) {
|
|
|
|
// Default result for uncommon narrow/search views.
|
|
|
|
return $t({defaultMessage: "Search results"});
|
|
|
|
}
|
|
|
|
|
2024-04-05 19:41:15 +02:00
|
|
|
if (filter.has_operator("channel")) {
|
2023-10-05 23:18:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-22 06:02:47 +01:00
|
|
|
export function redraw_title(): void {
|
2023-10-05 23:18:00 +02:00
|
|
|
// Update window title to reflect unread messages in current view
|
|
|
|
const new_title =
|
|
|
|
(unread_count ? "(" + unread_count + ") " : "") +
|
|
|
|
narrow_title +
|
|
|
|
" - " +
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_name +
|
2023-10-05 23:18:00 +02:00
|
|
|
" - " +
|
|
|
|
"Zulip";
|
|
|
|
|
|
|
|
document.title = new_title;
|
|
|
|
}
|
|
|
|
|
2023-12-22 06:02:47 +01:00
|
|
|
export function update_unread_counts(counts: FullUnreadCountsData): void {
|
2023-10-05 23:18:00 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-04-04 04:53:12 +02:00
|
|
|
export function update_narrow_title(filter?: Filter): void {
|
2023-10-05 23:18:00 +02:00
|
|
|
narrow_title = compute_narrow_title(filter);
|
|
|
|
redraw_title();
|
|
|
|
}
|