messages_overlay_ui: Fix TypeScript noUncheckedIndexedAccess errors.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-05-30 10:07:59 -07:00
parent eddf79fca2
commit b953584d45
1 changed files with 8 additions and 24 deletions

View File

@ -13,8 +13,8 @@ export type Context = {
}; };
export function row_with_focus(context: Context): JQuery { export function row_with_focus(context: Context): JQuery {
const focused_item = $(`.${CSS.escape(context.box_item_selector)}:focus`)[0]; const $focused_item = $(`.${CSS.escape(context.box_item_selector)}:focus`);
return $(focused_item).parent(`.${CSS.escape(context.row_item_selector)}`); return $focused_item.parent(`.${CSS.escape(context.row_item_selector)}`);
} }
export function activate_element(elem: HTMLElement, context: Context): void { export function activate_element(elem: HTMLElement, context: Context): void {
@ -41,7 +41,7 @@ export function focus_on_sibling_element(context: Context): void {
} }
const $new_focus_element = get_element_by_id(elem_to_be_focused_id ?? "", context); const $new_focus_element = get_element_by_id(elem_to_be_focused_id ?? "", context);
if ($new_focus_element.length > 0) { if ($new_focus_element[0] !== undefined) {
assert($new_focus_element[0].children[0] instanceof HTMLElement); assert($new_focus_element[0].children[0] instanceof HTMLElement);
activate_element($new_focus_element[0].children[0], context); activate_element($new_focus_element[0].children[0], context);
} }
@ -125,39 +125,23 @@ function initialize_focus(event_name: string, context: Context): void {
} }
const modal_items_ids = context.get_items_ids(); const modal_items_ids = context.get_items_ids();
if (modal_items_ids.length === 0) { const id = modal_items_ids.at(event_name === "up_arrow" ? -1 : 0);
if (id === undefined) {
// modal is empty // modal is empty
return; return;
} }
let $element: JQuery; const $element = get_element_by_id(id, context);
function get_last_element(): JQuery {
const last_id = modal_items_ids.at(-1) ?? "";
return get_element_by_id(last_id, context);
}
function get_first_element(): JQuery {
const first_id = modal_items_ids[0];
return get_element_by_id(first_id, context);
}
if (event_name === "up_arrow") {
$element = get_last_element();
} else {
$element = get_first_element();
}
const focus_element = $element[0].children[0]; const focus_element = $element[0].children[0];
assert(focus_element instanceof HTMLElement); assert(focus_element instanceof HTMLElement);
activate_element(focus_element, context); activate_element(focus_element, context);
} }
function scroll_to_element($element: JQuery, context: Context): void { function scroll_to_element($element: JQuery, context: Context): void {
if ($element.length === 0) { if ($element[0] === undefined) {
return; return;
} }
if ($element.children.length === 0) { if ($element[0].children[0] === undefined) {
return; return;
} }
assert($element[0].children[0] instanceof HTMLElement); assert($element[0].children[0] instanceof HTMLElement);