mirror of
https://github.com/nimbold/Firelink.git
synced 2026-08-27 19:17:13 +00:00
fix(downloads): avoid quadratic queue drag lookups
This commit is contained in:
@@ -666,12 +666,20 @@ export const DownloadTable: React.FC<DownloadTableProps> = ({ filter }) => {
|
|||||||
addToast({ message: `${message}: ${detail}`, variant: 'error', isActionable: true });
|
addToast({ message: `${message}: ${detail}`, variant: 'error', isActionable: true });
|
||||||
}, [addToast]);
|
}, [addToast]);
|
||||||
|
|
||||||
|
const queueRowsById = (list: HTMLDivElement): Map<string, HTMLElement> => {
|
||||||
|
const rows = new Map<string, HTMLElement>();
|
||||||
|
for (const element of Array.from(list.children)) {
|
||||||
|
if (!(element instanceof HTMLElement)) continue;
|
||||||
|
const id = element.dataset.downloadId;
|
||||||
|
if (id) rows.set(id, element);
|
||||||
|
}
|
||||||
|
return rows;
|
||||||
|
};
|
||||||
|
|
||||||
const queueRowForId = (id: string): HTMLElement | null => {
|
const queueRowForId = (id: string): HTMLElement | null => {
|
||||||
const list = queueListElementRef.current;
|
const list = queueListElementRef.current;
|
||||||
if (!list) return null;
|
if (!list) return null;
|
||||||
return Array.from(list.children).find(element =>
|
return queueRowsById(list).get(id) || null;
|
||||||
element instanceof HTMLElement && element.dataset.downloadId === id
|
|
||||||
) as HTMLElement | undefined || null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const queueDropPosition = (
|
const queueDropPosition = (
|
||||||
@@ -682,10 +690,13 @@ export const DownloadTable: React.FC<DownloadTableProps> = ({ filter }) => {
|
|||||||
const list = queueListElementRef.current;
|
const list = queueListElementRef.current;
|
||||||
if (!list || items.length === 0) return { targetIndex: 0, markerTop: 0 };
|
if (!list || items.length === 0) return { targetIndex: 0, markerTop: 0 };
|
||||||
const listRect = list.getBoundingClientRect();
|
const listRect = list.getBoundingClientRect();
|
||||||
|
// Build the DOM index once per pointer update. Looking up each row by
|
||||||
|
// scanning list.children inside the loop made large queue drags O(n^2).
|
||||||
|
const rowsById = queueRowsById(list);
|
||||||
let boundaryIndex = items.length;
|
let boundaryIndex = items.length;
|
||||||
let markerTop = 0;
|
let markerTop = 0;
|
||||||
for (let index = 0; index < items.length; index += 1) {
|
for (let index = 0; index < items.length; index += 1) {
|
||||||
const row = queueRowForId(items[index].id);
|
const row = rowsById.get(items[index].id);
|
||||||
if (!row) continue;
|
if (!row) continue;
|
||||||
const rect = row.getBoundingClientRect();
|
const rect = row.getBoundingClientRect();
|
||||||
if (clientY < rect.top + rect.height / 2) {
|
if (clientY < rect.top + rect.height / 2) {
|
||||||
|
|||||||
Reference in New Issue
Block a user