Files
EpicNext-Cms/src/components/admin/studio/furniture-sync-queue.tsx
T
openhandsandClaude Opus 4.8 28ce0f911c
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
fix(catalog): keep the live catalog truthful after every import path
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]>
2026-09-30 14:45:26 +02:00

77 lines
2.6 KiB
TypeScript

"use client";
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");
// 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);
const requestId = useRef<string | null>(null);
const submitting = useRef(false);
async function start() {
if (submitting.current) return;
submitting.current = true;
setBusy(true);
setError("");
setQueued(false);
// Keep the ID after an uncertain response, including a browser reload.
const key = `furniture-sync-request:${kind}`;
try {
if (!requestId.current) {
try {
requestId.current = sessionStorage.getItem(key);
} catch {}
requestId.current ||= crypto.randomUUID();
}
try {
sessionStorage.setItem(key, requestId.current);
} catch {}
const response = await adminFetch(`/api/admin/import/${kind}/sync-all`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: requestId.current }),
});
const result = await response.json().catch(() => ({}));
if (response.status >= 400 && response.status < 500) {
requestId.current = null;
try {
sessionStorage.removeItem(key);
} catch {}
}
if (!response.ok) throw Error(result.error || t("failed"));
if (result.job?.id !== requestId.current) throw Error(t("failed"));
try {
sessionStorage.removeItem(key);
} catch {}
requestId.current = null;
setQueued(true);
await history.refresh();
} catch (error) {
setError(error instanceof Error ? error.message : t("failed"));
} finally {
submitting.current = false;
setBusy(false);
}
}
return (
<div className="space-y-4">
<p className="text-sm text-muted-foreground">{t("description")}</p>
<Button disabled={busy} onClick={() => void start()}>
{busy ? t("preparing") : t("start")}
</Button>
{error && <p role="alert">{error}</p>}
{queued && <p role="status">{t("queued")}</p>}
<FurnitureJobHistory state={history} expanded />
</div>
);
}