Skip to content

Commit

Permalink
recent_view: Fix no topic found at center of recent view.
Browse files Browse the repository at this point in the history
The bug is hard to reproduce, but I was able to reproduce with #30134
and used the same strategy to fix it here.
  • Loading branch information
amanagr committed May 22, 2024
1 parent 39c6a01 commit a4c1a9f
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions web/src/recent_view_ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ let dropdown_filters = new Set<string>();
const recent_conversation_key_prefix = "recent_conversation:";

let is_initial_message_fetch_pending = true;
// We wait for rows to render and restore focus before processing
// any new events.
let is_waiting_for_revive_current_focus = true;

export function set_initial_message_fetch_status(value: boolean): void {
is_initial_message_fetch_pending = value;
Expand Down Expand Up @@ -1111,6 +1114,10 @@ function topic_offset_to_visible_area($topic_row: JQuery): string | undefined {
}

function recenter_focus_if_off_screen(): void {
if (is_waiting_for_revive_current_focus) {
return;
}

const table_wrapper_element = $("#recent_view_table .table_fix_head")[0];
const $topic_rows = $("#recent_view_table table tbody tr");

Expand All @@ -1133,8 +1140,13 @@ function recenter_focus_if_off_screen(): void {
const topic_center_y = (position.top + position.bottom) / 2;

const topic_element = document.elementFromPoint(topic_center_x, topic_center_y);
assert(topic_element !== null);
row_focus = $topic_rows.index($(topic_element).closest("tr")[0]);
if (topic_element === null) {
// Last visible element is above the vertical center of the table.
row_focus = $topic_row.length - 1;
} else {
row_focus = $topic_rows.index($(topic_element).closest("tr")[0]);
}

set_table_focus(row_focus, col_focus);
}
}
Expand All @@ -1153,7 +1165,10 @@ function is_scroll_position_for_render(scroll_container: HTMLElement): boolean {

function callback_after_render(): void {
update_load_more_banner();
setTimeout(revive_current_focus, 0);
setTimeout(() => {
revive_current_focus();
is_waiting_for_revive_current_focus = false;
}, 0);
}

function filter_click_handler(
Expand Down Expand Up @@ -1281,6 +1296,7 @@ function filter_buttons(): JQuery {
}

export function hide(): void {
is_waiting_for_revive_current_focus = true;
views_util.hide({
$view: $("#recent_view"),
set_visible: recent_view_util.set_visible,
Expand Down

0 comments on commit a4c1a9f

Please sign in to comment.