Add right-click menu to issue and patch lists

This commit is contained in:
Brandon Oxendine
2026-10-20 15:44:56 +02:00
committed by Rūdolfs Ošiņš
parent 95405a07cb
commit 41e5b96b09
16 changed files with 942 additions and 23 deletions
+55
View File
@@ -0,0 +1,55 @@
interface Position {
x: number;
y: number;
id: string;
target: HTMLElement;
}
export function createListContextMenu<
T extends { id: string; labels: string[] },
>(items: () => T[]) {
let position: Position | undefined = $state();
const current = $derived.by(() => {
if (!position) return undefined;
const { id } = position;
const item = items().find(item => item.id === id);
return item && { ...position, item };
});
const labelSuggestions = $derived(
items()
.flatMap(item => item.labels)
.filter((label, index, all) => all.indexOf(label) === index),
);
// The item can drop out of the list after a refresh; forget it so the menu
// doesn't pop back up if it ever reappears.
$effect(() => {
if (position && !current) position = undefined;
});
return {
get current() {
return current;
},
get labelSuggestions() {
return labelSuggestions;
},
isActive(id: string): boolean {
return current?.item.id === id;
},
open(event: MouseEvent, id: string) {
event.preventDefault();
position = {
x: event.clientX,
y: event.clientY,
id,
target: event.currentTarget as HTMLElement,
};
},
close() {
position = undefined;
},
};
}
+23
View File
@@ -17,3 +17,26 @@ export function menuFocusIndex(
return undefined;
}
}
export const menuItemSelector = '[role="menuitem"], [role="menuitemcheckbox"]';
// Whether `item` belongs to `menu` itself rather than to a nested submenu.
export function isOwnMenuItem(menu: HTMLElement, item: Element): boolean {
return item.parentElement?.closest('[role="menu"]') === menu;
}
export function ownMenuItems(menu: HTMLElement | undefined): HTMLElement[] {
if (!menu) return [];
return Array.from(
menu.querySelectorAll<HTMLElement>(menuItemSelector),
).filter(item => isOwnMenuItem(menu, item));
}
export function focusMenuItem(ev: KeyboardEvent, items: HTMLElement[]) {
if (items.length === 0) return;
const current = items.findIndex(el => el === document.activeElement);
const next = menuFocusIndex(ev.key, current, items.length);
if (next === undefined) return;
ev.preventDefault();
items[next].focus();
}
+23
View File
@@ -0,0 +1,23 @@
// Runs edits one at a time in submission order and refreshes once the queue
// drains, so quick successive toggles neither race nor refresh per edit.
export function serialEdits(
errorMessage: string,
refresh: () => Promise<void>,
): (run: () => Promise<unknown>) => void {
let queue: Promise<void> = Promise.resolve();
let pending = 0;
return run => {
pending += 1;
queue = queue.then(async () => {
try {
await run();
} catch (error) {
console.error(errorMessage, error);
} finally {
pending -= 1;
if (pending === 0) await refresh();
}
});
};
}