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

+4 -40
View File
@@ -1,6 +1,5 @@
import { asc, count, eq, or } from "drizzle-orm";
import { asc, eq, or } from "drizzle-orm";
import { Crown, Store, Wrench } from "lucide-react";
import { unstable_cache } from "next/cache";
import Link from "next/link";
import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
@@ -8,47 +7,12 @@ import { AdminPageShell } from "@/components/admin/admin-page-shell";
import { OrganizeImportsLauncher } from "@/components/admin/studio/organize-imports-launcher";
import { CatalogSyncStatus } from "@/features/catalog/components/catalog-sync-status";
import { CatalogPackagesDialog } from "@/features/catalog/components/packages/catalog-packages-dialog";
import {
CatalogItems,
CatalogItemsBc,
CatalogPages,
CatalogPagesBc,
db,
} from "@/lib/db";
import { getCachedCatalogTotals } from "@/features/catalog/server/catalog-totals";
import { CatalogPages, CatalogPagesBc, db } from "@/lib/db";
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
import type { RootTab } from "@/types/catalog";
import { CatalogManagerWorkspace } from "./catalog-manager-dialog-wrapper";
interface Totals {
totalPages: number;
enabledPages: number;
totalItems: number;
}
async function loadTotals(catalogType: "normal" | "bc"): Promise<Totals> {
const pagesTable = catalogType === "bc" ? CatalogPagesBc : CatalogPages;
const itemsTable = catalogType === "bc" ? CatalogItemsBc : CatalogItems;
const [pages, items, enabled] = await Promise.all([
db.select({ total: count() }).from(pagesTable),
db.select({ total: count() }).from(itemsTable),
db
.select({ total: count() })
.from(pagesTable)
.where(eq(pagesTable.enabled, "1")),
]);
return {
totalPages: Number(pages[0]?.total ?? 0),
totalItems: Number(items[0]?.total ?? 0),
enabledPages: Number(enabled[0]?.total ?? 0),
};
}
const getCachedTotals = unstable_cache(
async (catalogType: "normal" | "bc") => loadTotals(catalogType),
["admin-catalog-totals"],
{ revalidate: 30 },
);
async function loadRootPages(catalogType: "normal" | "bc"): Promise<RootTab[]> {
const pagesTable = catalogType === "bc" ? CatalogPagesBc : CatalogPages;
const rootRaw = await db
@@ -85,7 +49,7 @@ export default async function CatalogPagesPage({
const catalogType = sp.catalog === "bc" ? "bc" : "normal";
const [totals, rootPages] = await Promise.all([
getCachedTotals(catalogType),
getCachedCatalogTotals(catalogType),
loadRootPages(catalogType),
]);
@@ -26,6 +26,7 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { refreshCatalogTree } from "@/features/catalog/client/use-live-catalog";
import { adminFetch } from "@/lib/admin-fetch";
import { HABBO_GAMEDATA_HOTELS } from "@/lib/habbo-gamedata-hotel";
import { runSseImport } from "@/lib/sse-client";
@@ -532,6 +533,8 @@ function FurniGrid({ source }: FurniGridProps) {
if (succeeded > 0) toast.success(`${succeeded} furni cloned`);
if (failed > 0) toast.error(`${failed} failed`);
fetchItems(activeSearch, page, filter);
// Cloning writes catalog rows and category pages server-side.
if (succeeded > 0) void refreshCatalogTree();
},
abort.signal,
() => {
@@ -635,6 +638,7 @@ function FurniGrid({ source }: FurniGridProps) {
}
setSelected(new Set());
fetchItems(activeSearch, page, filter);
if (okCount > 0) void refreshCatalogTree();
} catch (err) {
if ((err as Error)?.name === "AbortError") {
toast.info("Clone cancelled");
@@ -9,6 +9,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { refreshCatalogTree } from "@/features/catalog/client/use-live-catalog";
import { adminFetch } from "@/lib/admin-fetch";
const CLASSNAME_RE = /^[a-zA-Z0-9_]+$/;
@@ -127,6 +128,9 @@ export function UploadFurniClient() {
}
setResult(data);
// The upload creates a category page server-side, so the live tree and
// totals would stay pre-import without pulling them again.
void refreshCatalogTree();
toast.success(`${data.classname} imported successfully`);
} catch {
const msg = "Network error";
+8 -2
View File
@@ -1,4 +1,5 @@
import { asc, count, eq, inArray, like, or } from "drizzle-orm";
import { loadCatalogTotals } from "@/features/catalog/server/catalog-totals";
import { catalogFailure } from "@/features/catalog/server/errors";
import {
deletePageCommand,
@@ -348,8 +349,13 @@ export const GET = withAdmin(
const isBc = sp.get("catalog") === "bc";
if (sp.get("mode") === "full") {
const pages = isBc ? await getBcTreeFlat() : await getTreeFlat();
return apiOk({ pages });
// Totals travel with the tree so one refresh updates both the rail and
// the stats bar. Read uncached — this response is already the fresh read.
const [pages, totals] = await Promise.all([
isBc ? getBcTreeFlat() : getTreeFlat(),
loadCatalogTotals(isBc ? "bc" : "normal"),
]);
return apiOk({ pages, totals });
}
const pageIdRaw = sp.get("pageId");
@@ -1,3 +1,4 @@
import { invalidateCatalogTotals } from "@/features/catalog/server/catalog-totals";
import { apiError } from "@/lib/api";
import { withAdmin } from "@/lib/api-handler";
import { PERMS } from "@/lib/permissions";
@@ -183,6 +184,7 @@ export const POST = withAdmin(
// New items + files landed — drop caches so the Studio refresh is fresh.
clearFurniImportCache();
invalidateCatalogTotals();
},
});
},
@@ -1,4 +1,5 @@
import { randomUUID } from "node:crypto";
import { invalidateCatalogTotals } from "@/features/catalog/server/catalog-totals";
import { apiError } from "@/lib/api";
import { withAdmin } from "@/lib/api-handler";
import { getRequestId } from "@/lib/foundation/request-context";
@@ -417,6 +418,9 @@ export const POST = withAdmin(
);
}
}
// The batch wrote offers and category pages whatever the emulator
// refresh did; the admin totals must not serve pre-batch numbers.
invalidateCatalogTotals();
if (!aborted) {
send({
+4
View File
@@ -1,6 +1,7 @@
import { existsSync, promises as fs } from "node:fs";
import path from "node:path";
import { eq, sql } from "drizzle-orm";
import { invalidateCatalogTotals } from "@/features/catalog/server/catalog-totals";
import { apiError, apiOk } from "@/lib/api";
import { withAdmin } from "@/lib/api-handler";
import { db, ItemsBase, queryRows } from "@/lib/db";
@@ -101,6 +102,7 @@ export const DELETE = withAdmin(
} catch (err) {
errors.push(`RCON: ${(err as Error).message}`);
}
invalidateCatalogTotals();
logAudit({
userId: ctx.session.user.id,
@@ -457,6 +459,7 @@ export const POST = withAdmin(
(err as Error).message,
);
}
invalidateCatalogTotals();
// Force offer_id to mirror its catalog row id across the tree
const reconciled = await reconcileImportedOfferIds();
@@ -587,6 +590,7 @@ export const POST = withAdmin(
);
result.warnings.push("RCON cache refresh failed");
}
invalidateCatalogTotals();
// Force offer_id to mirror its catalog row id across the tree
const reconciled = await reconcileImportedOfferIds();
@@ -1,3 +1,4 @@
import { invalidateCatalogTotals } from "@/features/catalog/server/catalog-totals";
import { apiError, apiOk } from "@/lib/api";
import { withAdmin } from "@/lib/api-handler";
import { PERMS } from "@/lib/permissions";
@@ -69,6 +70,9 @@ export const POST = withAdmin(
return apiError(result.error || "Upload failed", 400);
}
// The upload can add an offer and its category page.
invalidateCatalogTotals();
logAudit({
userId: ctx.session.user.id,
action: "furni_upload",