From 7299ca185ae5536cefa203da599a3192d5fbb921 Mon Sep 17 00:00:00 2001 From: bliatun-code <270035675+bliatun-code@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:44:08 +0100 Subject: [PATCH] Open shelf-filtered Stock with one card page --- docs/development/PERFORMANCE_BASELINE.md | 5 ++- scripts/run-loan-dialog-regressions.mjs | 55 +++++++++++++++++++++++- ui/src/lib/use_inventory_page_data.ts | 30 ++++++++++--- ui/src/pages/inventory.tsx | 19 +++++--- 4 files changed, 94 insertions(+), 15 deletions(-) diff --git a/docs/development/PERFORMANCE_BASELINE.md b/docs/development/PERFORMANCE_BASELINE.md index 392f3ab8..c53ea0d7 100644 --- a/docs/development/PERFORMANCE_BASELINE.md +++ b/docs/development/PERFORMANCE_BASELINE.md @@ -32,7 +32,10 @@ Current Hosts also filter individual rolls by status, ownership, material, vendor, location and low-stock policy before grouping. Opening the filter panel requests distinct materials and vendors from the whole stock in the same read transaction. Older Hosts and offline Clients filter the complete live or saved -list. Opening and closing Add spool keeps the loaded cards and reads the +list. Open inventory from a linked shelf passes the location and search text +to the first card-page read. Opening an individual shelf roll still checks its +placement against stock from the reviewed library and generation. +Opening and closing Add spool keeps the loaded cards and reads the registration catalog on demand. A successful registration refreshes stock so the new roll can be opened. List view, labels and selection use the complete stock so they cover every matching roll. diff --git a/scripts/run-loan-dialog-regressions.mjs b/scripts/run-loan-dialog-regressions.mjs index b559f714..15e0e3d7 100644 --- a/scripts/run-loan-dialog-regressions.mjs +++ b/scripts/run-loan-dialog-regressions.mjs @@ -535,6 +535,9 @@ async function checkLocationRollHandoff({ page, state, fixture, calls, fallback, replacement.master.filament_name = "New generation roll"; const started = deferred(), response = deferred(); let delayRead = false; + let cardMode = "live"; + const cardQueries = []; + const cardStarted = deferred(), oldCards = deferred(); const snapshots = []; const detailReads = []; state.invoke = (command, payload) => { @@ -547,9 +550,23 @@ async function checkLocationRollHandoff({ page, state, fixture, calls, fallback, case "fetch_library_sync_printer_overview": return []; case "fetch_library_sync_wishlist_items": return []; case "get_location_spool_page": return { rows: [reviewed], total: 1, matches: 1, revisions: [1, 1] }; + case "get_stock_card_page": + if (!payload.query.filters?.location_id) return null; + cardQueries.push(payload.query); + assert.equal(payload.target.target_generation, current.target_generation); + if (cardMode === "delayed") { + cardStarted.resolve(); + return oldCards.promise; + } + if (cardMode === "legacy") return null; + if (cardMode === "offline") throw Error("Host disconnected"); + return {cards:[{count:1, remaining:500, rows:[reviewed]}], total:2, visible:1, + groups:1, borrowed:0, revisions:[1,1]}; + case "fetch_cached_library_sync_spools": return {rows:[reviewed], captured_at:"2026-01-01"}; case "fetch_library_sync_spool_snapshot": { const generation = payload.input.target_generation; snapshots.push(generation); + if (cardMode === "offline") throw Error("Host disconnected"); if (delayRead && generation === host.target_generation) { started.resolve(); return response.promise; @@ -574,6 +591,23 @@ async function checkLocationRollHandoff({ page, state, fixture, calls, fallback, await roll.waitFor(); }; + for (cardMode of ["live", "legacy", "offline"]) { + await openShelf(); + await panel.getByRole("searchbox").fill("Dialog a"); + await roll.waitFor(); + calls.length = 0; + await panel.getByRole("button", {name: "Open inventory", exact: true}).click(); + await page.getByRole("heading", {name: /Dialog a/}).waitFor(); + await page.waitForFunction(() => document.activeElement?.id === "inventory-location-filter-chip"); + assert.equal(cardQueries.at(-1)?.filters.location_id, location.id); + assert.equal(cardQueries.at(-1)?.search, "Dialog a"); + assert.equal(calls.filter(c => c.command === "get_stock_card_page").length, 1); + assert.equal(calls.some(c => c.command === "fetch_library_sync_spool_snapshot"), cardMode !== "live"); + assert.equal(calls.some(c => c.command === "fetch_cached_library_sync_spools"), cardMode === "offline"); + } + snapshots.length = 0; + calls.length = 0; + cardMode = "live"; await openShelf(); await panel.getByRole("button", { name: "Back", exact: true }).click(); await page.waitForFunction(() => document.activeElement?.getAttribute("aria-label") === "Handoff shelf: 1 connected roll"); @@ -607,6 +641,25 @@ async function checkLocationRollHandoff({ page, state, fixture, calls, fallback, assert.equal(await page.locator("#inventory-location-filter-chip").count(), 0); assert.deepEqual(detailReads, []); assert.deepEqual(snapshots, [21, 21, 23]); + + cardMode = "delayed"; + await openShelf(); + await panel.getByRole("button", {name: "Open inventory", exact: true}).click(); + await waitForStart(cardStarted); + current = { ...host, host_base_url: "http://shelf-b.local:4278", library_id: "B", target_generation: 24 }; + current = { ...host, target_generation: 25 }; + const obsolete = {...reviewed, master: {...reviewed.master, filament_name: "Obsolete shelf card"}}; + await page.evaluate(() => { + window.obsoleteShelfCardSeen = false; + new MutationObserver(() => { + if (document.body.innerText.includes("Obsolete shelf card")) window.obsoleteShelfCardSeen = true; + }).observe(document.body, {childList:true, subtree:true, characterData:true}); + }); + oldCards.resolve({cards:[{count:1, remaining:500, rows:[obsolete]}], total:1, visible:1, + groups:1, borrowed:0, revisions:[1,1]}); + await page.getByRole("heading", {name: /New generation roll/}).waitFor(); + assert.equal(await page.locator("#inventory-location-filter-chip").count(), 0); + assert.equal(await page.evaluate(() => window.obsoleteShelfCardSeen), false); } async function checkWorkspaceWrites({ page, state, fixture, calls, fallback, openWorkspace }) { @@ -1038,7 +1091,7 @@ async function main() { ["loan workspace badges and stock on demand after lending and returning", checkWorkspaceWrites, true], ["Locations rejects an A–B–A read and opens the linked offline rolls on click", checkLinkedLocations, true], ["Location pages and search stay within the shelf and reject an old Host generation", checkLocationPages, true], - ["A shelf roll opens its detail only on the reviewed Host generation", checkLocationRollHandoff, true], + ["Shelf links preserve the filter, keyboard focus and reviewed Host generation", checkLocationRollHandoff, true], ["Stock pages search beyond previews, discard stale results and filter the offline copy", checkStockCards, true], ["An older Host supplies the complete stock for the selected location on demand", checkLegacyLocation, true], ["offline loan rows and badges reject a late A–B–A read", checkWorkspaceOffline, true], diff --git a/ui/src/lib/use_inventory_page_data.ts b/ui/src/lib/use_inventory_page_data.ts index ec3775f7..003852ad 100644 --- a/ui/src/lib/use_inventory_page_data.ts +++ b/ui/src/lib/use_inventory_page_data.ts @@ -72,7 +72,7 @@ export type InventoryReloadReporter = ( type InventoryRefreshInput = { onStockLoaded?: (rows: InventorySpool[]) => void; selectedSpoolId?: string | null; - cardPage?: boolean; + cardPage?: boolean | Pick; purchasesOnly?: boolean; locationsOnly?: boolean; }; @@ -407,7 +407,14 @@ export function useInventoryPageData({ current.clientTargetGeneration !== clientTargetGeneration; }, [clientHostBaseUrl, clientLibraryId, clientTargetGeneration]); - const reloadSpools = useCallback(async (reportResult?: InventoryReloadReporter, onStockLoaded?: InventoryRefreshInput["onStockLoaded"], cards = false, previous?: StockCards, memberOf?: string, searchRequest?: { text: string; filters?: StockCards["filters"]; current: () => boolean }) => { + const reloadSpools = useCallback(async ( + reportResult?: InventoryReloadReporter, + onStockLoaded?: InventoryRefreshInput["onStockLoaded"], + cards: InventoryRefreshInput["cardPage"] = false, + previous?: StockCards, + memberOf?: string, + searchRequest?: { text: string; filters?: StockCards["filters"]; current: () => boolean }, + ) => { if (!tauriAvailable) { reportResult?.("spools", "ERROR"); reportResult?.("locations", "ERROR"); @@ -419,21 +426,32 @@ export function useInventoryPageData({ const target = cards && clientLibraryId && clientTargetGeneration != null && (!clientReadOnly || clientHostBaseUrl) ? {base_url: clientReadOnly ? clientHostBaseUrl : null, expected_library_id: clientLibraryId, target_generation: clientTargetGeneration} : null; + const initialQuery = typeof cards === "object" ? cards : undefined; if (target) { - const page = await loadStockCards(target, previous, memberOf, searchRequest?.text, searchRequest?.filters).catch(() => null); + const page = await loadStockCards( + target, previous, memberOf, + searchRequest?.text ?? initialQuery?.search, + searchRequest?.filters ?? initialQuery?.filters, + ).catch(() => null); if (!isCurrent()) return; - if ((page || searchRequest) && !await isLocationSpoolTargetCurrent(target)) { + if ((page || searchRequest || initialQuery) && !await isLocationSpoolTargetCurrent(target)) { await resolveLibrarySyncRole(); return; } if (!isCurrent()) return; if (page) { setCardPage(page); - setSpools(page.cards.flatMap(card => card.rows)); + const rows = page.cards.flatMap(card => card.rows); + setSpools(rows); clientInventorySourceRef.current = "LIVE"; setClientInventoryUpdatedAt(null); recordClientInventoryDomainSource("spools", "LIVE"); if (!previous && !searchRequest) await reloadLocations(undefined, reportResult, false); + if (!isCurrent()) { + reportResult?.("spools", "SUPERSEDED"); + return; + } + onStockLoaded?.(rows); reportResult?.("spools", "LIVE"); return; } @@ -449,7 +467,7 @@ export function useInventoryPageData({ clientHostBaseUrl && clientLibraryId && await inventoryFallbackPairingRejected(clientHostBaseUrl, clientLibraryId); // Another window can switch the persisted target while a live or fallback read is in flight. - const targetChanged = isCurrent() && (target && searchRequest + const targetChanged = isCurrent() && (target && (searchRequest || initialQuery) ? !await isLocationSpoolTargetCurrent(target) : clientReadOnly && await clientReadTargetChanged()); if (!isCurrent()) { diff --git a/ui/src/pages/inventory.tsx b/ui/src/pages/inventory.tsx index 5e11d82c..98c38c9a 100644 --- a/ui/src/pages/inventory.tsx +++ b/ui/src/pages/inventory.tsx @@ -404,8 +404,6 @@ export default function InventoryPage({ }, [activeWorkspaceView, locationAuthorityKey]); const requestCards = !requiresFullStock && inventoryView === "CARDS" && !navigationIntent && !showRollModal && !recentlyAddedSpoolId && !detailVisualFixture; - const requestCardsRef = useRef(requestCards); - useEffect(() => { requestCardsRef.current = requestCards; }, [requestCards]); useEffect(() => { if (cardPage && !requestCards && activeWorkspaceView === "STOCK" && librarySyncReady) { void refreshInventoryData(); @@ -420,10 +418,15 @@ export default function InventoryPage({ } : undefined, [advancedFiltersOpen, otherFilterCount, statusFilter, ownershipFilter, materialFilter, vendorFilter, locationFilter?.id, lowStockOnly]); const cardSearch = search.trim(); + const stockViewRef = useRef({ requestCards, search: cardSearch, filters: cardFilters }); + useEffect(() => { + stockViewRef.current = { requestCards, search: cardSearch, filters: cardFilters }; + }, [requestCards, cardSearch, cardFilters]); const lastCardSearchAt = useRef(-Infinity); const pendingCardSearch = requestCards && cardPage != null && (cardPage.search !== cardSearch || cardPage.filters !== cardFilters); + const loadingLocationStock = Boolean(locationFilter && (loading || refreshing)); useEffect(() => { - if (!pendingCardSearch || activeWorkspaceView !== "STOCK" || !librarySyncReady) return; + if (loadingLocationStock || !pendingCardSearch || activeWorkspaceView !== "STOCK" || !librarySyncReady) return; let current = true; const delay = cardSearch ? Math.max(0, 100 - (performance.now() - lastCardSearchAt.current)) : 0; const timer = window.setTimeout(() => { @@ -433,19 +436,21 @@ export default function InventoryPage({ }); }, delay); return () => { current = false; window.clearTimeout(timer); }; - }, [activeWorkspaceView, cardFilters, cardSearch, librarySyncReady, locationAuthorityKey, pendingCardSearch, reloadSpools]); + }, [activeWorkspaceView, cardFilters, cardSearch, librarySyncReady, locationAuthorityKey, pendingCardSearch, reloadSpools, loadingLocationStock]); const waitingForAllRows = Boolean(cardPage && (!requestCards || pendingCardSearch)); const cardTotals = useMemo(() => new Map(cardPage?.cards.map(card => [card.rows[0]?.id, {count: card.count, remaining: card.remaining}])), [cardPage]); const pendingPurchaseNavigation = navigationIntent?.kind === "PURCHASES" && !purchasesOnly; useEffect(() => { - const handoff = pendingLocationStock.current; - if (handoff && (activeWorkspaceView !== "STOCK" || handoff.authority !== locationAuthorityKey)) { + if (activeWorkspaceView !== "STOCK" || pendingLocationStock.current?.authority !== locationAuthorityKey) { pendingLocationStock.current = null; } if (!tauri || !librarySyncReady || pendingPurchaseNavigation || loansOnly || pendingLoanNavigation) return; let current = true; - void refreshInventoryData({ purchasesOnly, locationsOnly, cardPage: requestCardsRef.current && !handoff, onStockLoaded: handoff ? async rows => { + const handoff = pendingLocationStock.current; + const { requestCards: useCards, search, filters } = stockViewRef.current; + const cardPage = useCards && !handoff?.spoolId && (handoff ? { search, filters } : true); + void refreshInventoryData({ purchasesOnly, locationsOnly, cardPage, onStockLoaded: handoff ? async rows => { if (!current || pendingLocationStock.current !== handoff) return; try { const sameTarget = await isLocationSpoolTargetCurrent(handoff.target);