fix(catalog): keep the live catalog truthful after every import path
Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 29s
CI / tests-integration (push) Failing after 1m43s
CI / tests-unit (push) Successful in 1m47s
CI / tests-ui (push) Successful in 2m33s
CI / preflight (push) Skipped
CI / deploy (push) Skipped

The live catalog store only covered part of the import surface. A durable
job settled, a sync queue drained, a .nitro upload or a clone run left the
Studio rail and the stats bar showing pre-import numbers until the page was
reloaded, and the Catalog Manager kept a second tree that never saw writes
made elsewhere in the session.

Every one of those paths now pulls the tree again, and the refresh carries
the totals with it: importing writes catalog rows server-side, so the counts
the store holds were stale for the rest of the session.

- refreshCatalogTree shares one request between concurrent callers and queues
  a single follow-up read when a write lands mid-flight, so a burst of edits
  costs at most one extra read.
- useFurnitureJobs treats its first payload as a baseline, so a page load no
  longer replays every past import as "just settled", and hands the settled
  jobs to the callback.
- The Catalog Manager pushes its own mutations into the store and re-reads its
  active tab when the store changes.
- The 30s unstable_cache on the admin totals is now tagged and invalidated from
  every catalog write, including the import worker, so it no longer survives an
  import even across a hard reload.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
openhandsandClaude Opus 4.8 committed 2026-09-30 14:45:26 +02:00
1 parent d73baf1458
commit 28ce0f911c
22 files changed
+445 -70

No files matched your search

@@ -13,6 +13,7 @@ import type { CatalogSelection } from "@/features/catalog/client/catalog-selecti
import { useCatalogNavigation } from "@/features/catalog/client/use-catalog-navigation";
import { useLatestRequest } from "@/features/catalog/client/use-latest-request";
import {
refreshCatalogTree,
seedCatalogTotals,
useLiveCatalog,
} from "@/features/catalog/client/use-live-catalog";
@@ -271,6 +272,16 @@ function CatalogManagerInner({
}
}, [dispatch, catQs, rootRequests]);
/**
* A page created, moved or deleted here changes the tree the Studio rail and
* the stats bar read from the live store. Refreshing it here keeps both views
* truthful without a route re-render; the store coalesces bursts of edits.
*/
const onTreeMutation = useCallback(() => {
void loadRootPages();
void refreshCatalogTree();
}, [loadRootPages]);
// Seed once on opening, then refresh independently of category selection.
useEffect(() => {
if (!open) return;
@@ -504,7 +515,7 @@ function CatalogManagerInner({
activeTabId={state.activeTabId}
selectedPageId={state.selectedPageId}
onSelectPage={handleSelectPage}
onTreeMutation={loadRootPages}
onTreeMutation={onTreeMutation}
nodeUpdate={null}
/>
</div>
@@ -59,6 +59,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import { requestCatalogNavigation } from "@/features/catalog/client/use-catalog-navigation";
import { useLiveCatalog } from "@/features/catalog/client/use-live-catalog";
import { DuplicateCategoryDialog } from "@/features/catalog/components/duplicate-category-dialog";
import type { DuplicateInput } from "@/features/catalog/domain/duplicate";
import { cn } from "@/lib/utils";
@@ -213,6 +214,21 @@ export function SortableTree({
void loadTabTree();
}, [activeTabId, loadTabTree, dispatch]);
/**
* This tree is loaded per root tab, so it cannot see writes that happen
* elsewhere in the session — an import settling, or a page edited through the
* Studio. The live store is where those land; when it changes, re-read this
* tab so the dialog never shows pre-import nodes.
*/
const liveTree = useLiveCatalog().tree;
const seenLiveTree = useRef(liveTree);
useEffect(() => {
if (liveTree === seenLiveTree.current) return;
seenLiveTree.current = liveTree;
if (activeTabId === null) return;
void loadTabTree();
}, [liveTree, activeTabId, loadTabTree]);
// Ignore stale search completions when the query or catalog changes.
useEffect(() => {
const controller = new AbortController();
+3 -1
View File
@@ -2,11 +2,13 @@
import { useTranslations } from "next-intl";
import { CatalogExportClient } from "@/app/admin/studio/sync/catalog-export-client";
import Link from "@/components/link";
import { refreshCatalogTree } from "@/features/catalog/client/use-live-catalog";
import { FurnitureJobHistory } from "./studio/furniture-jobs";
import { useFurnitureJobs } from "./studio/use-furniture-jobs";
export function OperationsCenter() {
const t = useTranslations("pages.admin.operationsCenter");
const jobs = useFurnitureJobs(() => {}, true);
// Keep the live catalog in step with imports that finish while this tab is open.
const jobs = useFurnitureJobs(() => void refreshCatalogTree(), true);
return (
<div className="space-y-6">
<h1 className="text-2xl font-bold">{t("title")}</h1>
@@ -2,13 +2,16 @@
import { useTranslations } from "next-intl";
import { useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { refreshCatalogTree } from "@/features/catalog/client/use-live-catalog";
import { adminFetch } from "@/lib/admin-fetch";
import { FurnitureJobHistory } from "./furniture-jobs";
import { useFurnitureJobs } from "./use-furniture-jobs";
export function FurnitureSyncQueue({ kind }: { kind: "official" | "clone" }) {
const t = useTranslations("pages.admin.syncQueue");
const history = useFurnitureJobs(() => {}, true);
// A sync job writes offers and pages server-side; the history list alone
// would leave the catalog rail and stats bar showing pre-sync numbers.
const history = useFurnitureJobs(() => void refreshCatalogTree(), true);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const [queued, setQueued] = useState(false);
@@ -189,6 +189,9 @@ export function StudioClient({
() => {
void fetchItems(activeSearch, 1, activeSource);
void fetchStats();
// A durable job derives its "Imported Furniture" pages server-side, so
// the rail cannot predict them either — pull the tree and totals.
void refreshCatalogTree();
},
false,
actorId,
@@ -5,7 +5,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { toast } from "sonner";
import { useVisiblePolling } from "@/hooks/use-visible-polling";
import { adminFetch } from "@/lib/admin-fetch";
import type { ImportJobItem } from "@/lib/furni/import-job";
import type { ImportJob, ImportJobItem } from "@/lib/furni/import-job";
import { createFurnitureImportRequests } from "./furniture-import-requests";
import {
createFurnitureJobsClient,
@@ -15,8 +15,10 @@ import {
readFurnitureJobResponse,
} from "./furniture-jobs-query";
const SETTLED_STATES = ["completed", "interrupted", "cancelled"];
export function useFurnitureJobs(
onComplete: () => void,
onComplete: (settled: ImportJob[]) => void,
paginated = false,
actorId?: number,
) {
@@ -54,19 +56,24 @@ export function useFurnitureJobs(
},
[client],
);
const baselined = useRef(false);
useEffect(() => {
if (!query.data || unauthorized) return;
let changed = false;
const settled: ImportJob[] = [];
for (const job of query.data.jobs) {
if (
["completed", "interrupted", "cancelled"].includes(job.state) &&
!completed.current.has(job.id)
) {
completed.current.add(job.id);
changed = true;
}
if (!SETTLED_STATES.includes(job.state)) continue;
if (completed.current.has(job.id)) continue;
completed.current.add(job.id);
settled.push(job);
}
if (changed) callback.current();
// The first payload is history this component did not witness finishing.
// Recording it as a baseline keeps a page load from replaying every past
// import as "just settled", which would refetch the catalog on every visit.
if (!baselined.current) {
baselined.current = true;
return;
}
if (settled.length > 0) callback.current(settled);
}, [query.data, unauthorized]);
const refresh = useCallback(async () => {
// fetchQuery shares an in-flight GET. Query owns data, errors and loading.