From 986e5ad05d9b858901dfdb0a010a89ed3fc42657 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 03:01:44 +0800 Subject: [PATCH] refactor(inventory): reuse ordered row pagination (#16441) --- .../analysisView/artifactInventoryView.ts | 54 +++++++++---------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/domain/analysisView/artifactInventoryView.ts b/src/domain/analysisView/artifactInventoryView.ts index 8a176f8c5db..8046f5dadb8 100644 --- a/src/domain/analysisView/artifactInventoryView.ts +++ b/src/domain/analysisView/artifactInventoryView.ts @@ -32,7 +32,7 @@ import { type InventorySet, } from "../artifactInventoryEvidence.js"; import type { ArtifactInspectionResult } from "../artifactInspection.js"; -import { filterSet } from "./selectViewRows.js"; +import { filterSet, selectViewRows } from "./selectViewRows.js"; import type { AnalysisViewParent } from "./analysisViewParent.js"; import { assertOwnedView, @@ -510,10 +510,10 @@ export const projectArtifactInventoryView = ( }); } const inventoryPage = ({ - items, + rows: items, total, }: { - items: Items; + rows: Items; total: number; }) => ok({ @@ -538,7 +538,7 @@ export const projectArtifactInventoryView = ( "format_record_headers pages are read from the inventoried root file; this source cannot reopen it.", ), ); - return inventoryPage({ items: [...page.items], total: page.total }); + return inventoryPage({ rows: [...page.items], total: page.total }); } if (view.collection === "occurrences") return inventoryPage(occurrencePage(source, view)); @@ -548,11 +548,23 @@ export const projectArtifactInventoryView = ( ? inventory.edges : inventory.integrityContradictions; return inventoryPage({ - items: rows.slice(view.offset, view.offset + view.limit), + rows: rows.slice(view.offset, view.offset + view.limit), total: rows.length, }); }; +const selectInventoryRows = ( + rows: readonly Source[], + indices: readonly number[] | undefined, + view: { readonly offset: number; readonly limit: number }, + project: (row: Source) => Row, +) => + indices === undefined + ? selectViewRows(rows, view, undefined, project) + : selectViewRows(indices, view, undefined, (index) => + project(rows[index]!), + ); + const occurrencePage = (source: InventorySource, view: OccurrenceView) => { const rows = source.inventory.occurrences; const selected = occurrenceIndices(source, view); @@ -560,15 +572,9 @@ const occurrencePage = (source: InventorySource, view: OccurrenceView) => { view.order_by === undefined ? selected : sizeOrder(rows, selected, view.order_by); - const total = indices?.length ?? rows.length; - const items = []; - for ( - let at = view.offset; - at < Math.min(total, view.offset + view.limit); - at++ - ) - items.push(selectOccurrenceFields(rows[indices?.[at] ?? at]!, view.fields)); - return { items, total }; + return selectInventoryRows(rows, indices, view, (row) => + selectOccurrenceFields(row, view.fields), + ); }; /** Stable size order; unknown sizes follow known ones in either direction. */ @@ -597,21 +603,11 @@ const nodePage = ( ) => { const indices = nodeIndices(source, view.artifact_ids); const rows = source.inventory.nodes; - const total = indices?.length ?? rows.length; - const items = []; - for ( - let at = view.offset; - at < Math.min(total, view.offset + view.limit); - at++ - ) { - const row = rows[indices?.[at] ?? at]!; - items.push( - view.fields === undefined - ? row - : { ...selectFields(row, view.fields), artifact_id: row.artifact_id }, - ); - } - return { items, total }; + return selectInventoryRows(rows, indices, view, (row) => + view.fields === undefined + ? row + : { ...selectFields(row, view.fields), artifact_id: row.artifact_id }, + ); }; const inventoryCounts = (source: InventorySource): InventoryCounts => {