recent-topics: Check topic exists when trying to revive focus.

When a message is deleted, if it was the only message in the topic
and it was the previously focused message in recent topics, then
the topic is no longer in the recent topics data.

In this case, we revive the focus to the adjacent message or, if it
was the last message in the view, the focus is reset to the search
bar.
This commit is contained in:
Lauryn Menard
2022-08-19 21:39:42 +02:00
committed by Tim Abbott
parent 91e5ae84ac
commit 3de4fd5fbb

View File

@@ -190,17 +190,20 @@ export function revive_current_focus() {
if (is_table_focused()) {
if (last_visited_topic) {
const topic_last_msg_id = topics.get(last_visited_topic).last_msg_id;
const current_list = topics_widget.get_current_list();
const last_visited_topic_index = current_list.findIndex(
(topic) => topic.last_msg_id === topic_last_msg_id,
);
if (last_visited_topic_index >= 0) {
row_focus = last_visited_topic_index;
// If the only message in the topic was deleted,
// then the topic will not be in recent topics data.
if (topics.get(last_visited_topic) !== undefined) {
const topic_last_msg_id = topics.get(last_visited_topic).last_msg_id;
const current_list = topics_widget.get_current_list();
const last_visited_topic_index = current_list.findIndex(
(topic) => topic.last_msg_id === topic_last_msg_id,
);
if (last_visited_topic_index >= 0) {
row_focus = last_visited_topic_index;
}
}
last_visited_topic = "";
}
set_table_focus(row_focus, col_focus);
return true;
}