[Glitch] Refactor getFocusedItemIndex to avoid conditionals that closest already handles

Port 238d74fe81 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Brad Dunbar authored and Claire committed 2025-09-27 21:37:00 +02:00
1 parent 364aced99e
commit 381fc173c7
1 file changed
+5 -15
@@ -60,23 +60,13 @@ export function focusColumn({
* Get the index of the currently focused item in one of our item lists
*/
export function getFocusedItemIndex() {
const focusedElement = document.activeElement;
const itemList = focusedElement?.closest('.item-list');
if (!focusedElement || !itemList) {
return -1;
}
let focusedItem: HTMLElement | null = null;
if (focusedElement.parentElement === itemList) {
focusedItem = focusedElement as HTMLElement;
} else {
focusedItem = focusedElement.closest('.item-list > *');
}
const focusedItem = document.activeElement?.closest('.item-list > *');
if (!focusedItem) return -1;
const items = Array.from(itemList.children);
const { parentElement } = focusedItem;
if (!parentElement) return -1;
const items = Array.from(parentElement.children);
return items.indexOf(focusedItem);
}