feat(catalog): make the live catalog self-correcting and honest about failure
Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 29s
CI / tests-unit (push) Successful in 1m34s
CI / tests-integration (push) Failing after 1m34s
CI / tests-ui (push) Successful in 2m19s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 29s
CI / tests-unit (push) Successful in 1m34s
CI / tests-integration (push) Failing after 1m34s
CI / tests-ui (push) Successful in 2m19s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
The previous commit made imports update the Studio without a reload, but the guarantee only held inside the tab that started the import and only as long as every read succeeded. Four holes were left, and this closes them. A session that mounted the tree before an import kept the pre-import tree for the rest of its life, because ensureCatalogTreeLoaded() was a once-per-session no-op. It now asks the server whether what it holds is still current. The answer is a revision: sendCatalogUpdate() already runs after every catalog write, so it bumps one, and clients read it on mount, on focus, on a 20s poll and from other tabs over a BroadcastChannel. An import that finishes in another tab, another browser or the job worker now lands here too. A failed read used to be swallowed, which is the worst outcome available: the rail kept showing pre-import counts as if they were current and nothing said so. The snapshot now carries the error, the rail shows it with a retry, and the previous tree stays on screen because stale beats empty. Every settled import pulled the entire flat tree, which is the one payload that grows with the size of the catalog. The revision doubles as the ETag on mode=full, so an unchanged catalog answers 304 and the poll costs a file read. An import could also report success for an offer the hotel will never sell: a hidden or disabled page, an item_ids that misses the furni id, a zero amount. importSingleFurni reads its own row back and reports each of those as a warning, where the import report already is, instead of leaving it to surface as "the import did not work" in the client. Finally, the catalog items table no longer falls back to router.refresh() — onRefresh is now required, so every mutation ends in a refresh of the caller's own data instead of a route re-render that threw away editor state and scroll position. useServerAction keeps its default, because 47 callers across the app depend on it. The 750-line CatalogTree in catalog-tree.tsx was dead code that kept its own stale tree and three more router.refresh() calls; only CatalogIcon and LAYOUT_COLORS are still imported, so the rest is gone. Tests: the store now covers revisions, 304s, probe failures and error recovery; a jsdom test mounts a consumer and asserts the tree updates in place with no navigation; the old organize-imports e2e asserted nothing about the endpoints the code actually calls, and is replaced by one that asserts a cross-tab write lands in the mounted categories without a reload. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
1 parent
28ce0f911c
commit
9550b3d66f
18 files changed
+1016
-973
No files matched your search
@@ -1,10 +1,71 @@
|
||||
"use client";
|
||||
|
||||
import { ChevronDown, ChevronRight, FolderTree, Search, X } from "lucide-react";
|
||||
import { memo, useMemo, useState } from "react";
|
||||
import {
|
||||
AlertTriangle,
|
||||
ChevronDown,
|
||||
ChevronRight,
|
||||
FolderTree,
|
||||
Loader2,
|
||||
RefreshCw,
|
||||
Search,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { memo, useMemo, useState, useTransition } from "react";
|
||||
import {
|
||||
refreshCatalogTree,
|
||||
useLiveCatalog,
|
||||
} from "@/features/catalog/client/use-live-catalog";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { TreeNode } from "@/types/catalog";
|
||||
|
||||
/**
|
||||
* A refresh that fails must be visible. Every number and category below is
|
||||
* presented as current, so silently keeping the previous tree after a failed
|
||||
* read would show pre-import data as if nothing had happened — and a staff
|
||||
* member has no other signal that the import they just ran is not on screen.
|
||||
*/
|
||||
function LiveCatalogNotice() {
|
||||
const { refreshError, refreshing } = useLiveCatalog();
|
||||
const [retrying, startRetry] = useTransition();
|
||||
if (!refreshError && !refreshing) return null;
|
||||
return (
|
||||
<p
|
||||
role="status"
|
||||
className={cn(
|
||||
"mx-3 mb-2 flex items-center gap-2 rounded-md border px-2 py-1.5 text-xs",
|
||||
refreshError
|
||||
? "border-destructive/40 bg-destructive/10 text-destructive"
|
||||
: "text-[var(--admin-text-muted)]",
|
||||
)}
|
||||
>
|
||||
{refreshError ? (
|
||||
<>
|
||||
<AlertTriangle size={14} className="shrink-0" />
|
||||
<span className="min-w-0 flex-1">Catalog could not refresh.</span>
|
||||
<button
|
||||
type="button"
|
||||
disabled={retrying}
|
||||
onClick={() =>
|
||||
startRetry(async () => {
|
||||
await refreshCatalogTree();
|
||||
})
|
||||
}
|
||||
className="flex items-center gap-1 rounded px-1.5 py-0.5 font-medium underline disabled:opacity-50"
|
||||
>
|
||||
<RefreshCw size={12} className={cn(retrying && "animate-spin")} />
|
||||
Retry
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Loader2 size={14} className="shrink-0 animate-spin" />
|
||||
<span className="min-w-0 flex-1">Updating catalog…</span>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
export const CatalogRail = memo(function CatalogRail({
|
||||
tree,
|
||||
selectedId,
|
||||
@@ -77,6 +138,7 @@ export const CatalogRail = memo(function CatalogRail({
|
||||
>
|
||||
All categories
|
||||
</button>
|
||||
<LiveCatalogNotice />
|
||||
<div className="min-h-0 flex-1 overflow-y-auto px-2 pb-3">
|
||||
{visible.length === 0 && (
|
||||
<p className="p-3 text-sm text-[var(--admin-text-muted)]">
|
||||
|
||||
Reference in new issue
Block a user