perf(studio): cache furni imports and virtualize the furniture grid
CI / check (push) Failing after 22s
CI / deploy (push) Skipped
CI / publish-container (push) Skipped

- Add TTL-based caching for local index lookup, furnidata classnames,
  catalog id set, nitro file presence and import stats
- Invalidate caches after furnace single/batch/clone imports
- Rewrite batch progress with elapsed time, rate and verification chips
- Virtualize the grid with @tanstack/react-virtual and replace the
  Load more button with infinite scroll via an IntersectionObserver
This commit is contained in:
openhands committed 2026-09-13 11:40:55 +02:00
1 parent 2920669362
commit d02aaa0d87
6 files changed
+672 -345

No files matched your search

+166 -48
View File
@@ -1,8 +1,17 @@
"use client";
import { Ban, Check, Circle, Loader2, RefreshCw, X } from "lucide-react";
import { memo } from "react";
import {
Ban,
Check,
Circle,
CircleAlert,
Loader2,
RefreshCw,
X,
} from "lucide-react";
import { memo, useEffect, useMemo, useState } from "react";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import type { BatchItemStatus, VerificationSummary } from "./studio-types";
const statusLabels: Record<BatchItemStatus["status"], string> = {
@@ -15,6 +24,22 @@ const statusLabels: Record<BatchItemStatus["status"], string> = {
failed: "Failed",
};
function formatDuration(ms: number): string {
if (!Number.isFinite(ms) || ms < 0) return "0s";
const s = Math.floor(ms / 1000);
if (s < 60) return `${s}s`;
const m = Math.floor(s / 60);
const rest = s % 60;
return rest > 0 ? `${m}m ${rest}s` : `${m}m`;
}
function formatRate(completed: number, ms: number): string {
if (ms < 1500) return "";
const perMinute = completed / (ms / 60000);
if (perMinute < 1) return `${(completed / (ms / 1000)).toFixed(1)}/s`;
return `${Math.round(perMinute)}/min`;
}
export const BatchProgress = memo(function BatchProgress({
progress,
done,
@@ -23,6 +48,7 @@ export const BatchProgress = memo(function BatchProgress({
cancelling,
verification,
failedClassnames,
startTime,
onCancel,
onDismiss,
onRetry,
@@ -39,10 +65,21 @@ export const BatchProgress = memo(function BatchProgress({
onDismiss?: () => void;
onRetry?: (classnames: string[]) => void;
}) {
const entries = [...progress.values()];
const completed = entries.filter(
(item) => item.status === "done" || item.status === "failed",
).length;
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
if (done) return;
const id = setInterval(() => setNow(Date.now()), 500);
return () => clearInterval(id);
}, [done]);
const entries = useMemo(() => [...progress.values()], [progress]);
const completed = useMemo(
() =>
entries.filter((i) => i.status === "done" || i.status === "failed")
.length,
[entries],
);
const total = entries.length;
const percent = total ? Math.round((completed / total) * 100) : 0;
const imported = done
@@ -52,6 +89,8 @@ export const BatchProgress = memo(function BatchProgress({
? failed
: entries.filter((item) => item.status === "failed").length;
const unfinished = Math.max(0, total - completed);
const elapsed = startTime ? now - startTime : 0;
const rate = formatRate(completed, elapsed);
const heading = !done
? cancelling
? "Stopping import…"
@@ -64,6 +103,7 @@ export const BatchProgress = memo(function BatchProgress({
const issues = verification
? verification.furniDataMissing + verification.furniDataConflicts
: 0;
return (
<section
aria-label="Import progress"
@@ -77,10 +117,44 @@ export const BatchProgress = memo(function BatchProgress({
>
{heading}
</h2>
<p className="mt-1 text-xs text-[var(--admin-text-muted)]">
{imported} imported · {errors} failed
{unfinished > 0 &&
` · ${unfinished} ${done ? "not completed" : "remaining"}`}
<p className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-xs text-[var(--admin-text-muted)]">
<span>
<span className="font-semibold text-[var(--admin-success)]">
{imported}
</span>{" "}
imported
</span>
<span className="text-[var(--admin-border)]">·</span>
<span>
<span
className={cn(
"font-semibold",
errors > 0 && "text-[var(--admin-error)]",
)}
>
{errors}
</span>{" "}
failed
</span>
{unfinished > 0 && (
<>
<span className="text-[var(--admin-border)]">·</span>
<span>
{unfinished} {done ? "not completed" : "remaining"}
</span>
</>
)}
{!done && startTime ? (
<>
<span className="text-[var(--admin-border)]">·</span>
<span className="tabular-nums">{formatDuration(elapsed)}</span>
{rate && (
<span className="tabular-nums text-[var(--admin-accent)]">
{rate}
</span>
)}
</>
) : null}
</p>
</div>
<div className="flex flex-wrap gap-2">
@@ -127,16 +201,38 @@ export const BatchProgress = memo(function BatchProgress({
className="my-3 h-2 overflow-hidden rounded-full bg-[var(--admin-accent)]/10"
>
<div
className="h-full rounded-full bg-[var(--admin-accent)] transition-all"
className="h-full rounded-full bg-gradient-to-r from-[var(--admin-accent)] to-[var(--admin-accent)]/70 transition-all"
style={{ width: `${percent}%` }}
/>
</div>
{done && verification && (
<p className="mb-2 text-sm text-[var(--admin-text-muted)]">
{issues
? `${issues} catalog data issue(s) still need attention. Open Catalog audit to review them.`
: "Catalog data verification completed."}
</p>
<div className="mb-3 flex flex-wrap items-center gap-1.5">
<p className="mr-1 text-sm text-[var(--admin-text-muted)]">
{issues
? `${issues} catalog data issue(s) still need attention. Open Catalog audit to review them.`
: "Catalog data verification completed."}
</p>
<NodeChip
count={verification.furniDataFixedIds}
label="furnidata ids fixed"
/>
<NodeChip
count={verification.offerIdsFixed}
label="offer ids reconciled"
/>
<NodeChip
count={verification.ownershipFixed.length}
label="ownerships fixed"
/>
<NodeChip
count={verification.nitrosSynced.length}
label="nitros synced"
/>
<NodeChip
count={verification.iconsSynced.length}
label="icons synced"
/>
</div>
)}
<details>
<summary className="cursor-pointer text-xs font-medium">
@@ -146,40 +242,52 @@ export const BatchProgress = memo(function BatchProgress({
{entries.map((item) => (
<div
key={item.classname}
className="rounded-md bg-[var(--admin-canvas)] p-2 text-xs"
className="flex items-start gap-2 rounded-md bg-[var(--admin-canvas)] p-2 text-xs"
>
<div className="flex items-center gap-2">
{item.status === "done" ? (
<Check size={14} className="text-[var(--admin-success)]" />
) : item.status === "failed" ? (
<X size={14} className="text-[var(--admin-warning)]" />
) : done || item.status === "pending" ? (
<Circle size={12} />
) : (
<Loader2 size={14} className="animate-spin" />
)}
<span className="min-w-0 flex-1 break-all font-medium">
{item.classname}
</span>
<span className="shrink-0 text-[var(--admin-text-muted)]">
{done && item.status !== "done" && item.status !== "failed"
? "Not completed"
: statusLabels[item.status]}
</span>
</div>
{item.message && (
<p className="mt-1 break-words text-[var(--admin-text-muted)]">
{item.message}
</p>
{item.status === "done" ? (
<Check
size={14}
className="mt-0.5 shrink-0 text-[var(--admin-success)]"
/>
) : item.status === "failed" ? (
<X
size={14}
className="mt-0.5 shrink-0 text-[var(--admin-error)]"
/>
) : done || item.status === "pending" ? (
<Circle
size={12}
className="mt-0.5 shrink-0 text-[var(--admin-text-muted)]"
/>
) : (
<Loader2 size={14} className="mt-0.5 shrink-0 animate-spin" />
)}
{[...new Set(item.warnings ?? [])].map((warning) => (
<p
key={warning}
className="mt-1 break-words text-[var(--admin-warning)]"
>
{warning}
</p>
))}
<span className="min-w-0 flex-1">
<span className="flex flex-wrap items-baseline justify-between gap-1">
<span className="break-all font-medium">
{item.classname}
</span>
<span className="shrink-0 text-[var(--admin-text-muted)]">
{done && item.status !== "done" && item.status !== "failed"
? "Not completed"
: statusLabels[item.status]}
</span>
</span>
{item.message && (
<span className="mt-0.5 block break-words text-[var(--admin-text-muted)]">
{item.message}
</span>
)}
{[...new Set(item.warnings ?? [])].map((warning) => (
<span
key={warning}
className="mt-0.5 flex items-center gap-1 break-words text-[var(--admin-warning)]"
>
<CircleAlert size={11} className="shrink-0" />
{warning}
</span>
))}
</span>
</div>
))}
</div>
@@ -187,3 +295,13 @@ export const BatchProgress = memo(function BatchProgress({
</section>
);
});
function NodeChip({ count, label }: { count: number; label: string }) {
if (!count) return null;
return (
<span className="inline-flex items-center gap-1 rounded-full border border-[var(--admin-success)]/30 bg-[var(--admin-success)]/10 px-2 py-0.5 text-[0.65rem] text-[var(--admin-success)]">
<Check size={10} />
{count} {label}
</span>
);
}
+244 -133
View File
@@ -1,11 +1,12 @@
"use client";
import { useVirtualizer } from "@tanstack/react-virtual";
import {
ArrowDown,
ArrowUpDown,
Check,
CircleAlert,
CloudDownload,
Download,
Globe,
Languages,
Layers,
@@ -101,6 +102,11 @@ const ImportReview = lazy(() =>
})),
);
// Grid virtualization geometry (px) — matches the grid-card markup below.
const GRID_CARD_W = 150;
const GRID_GAP = 12;
const GRID_ROW_H = 176;
export function StudioClient({
source,
initialTree,
@@ -269,6 +275,39 @@ export function StudioClient({
[filteredItems, selected],
);
const hasMorePages = meta ? meta.currentPage < meta.lastPage : false;
// ── Grid virtualization ─────────────────────────────────────────
const [gridCols, setGridCols] = useState(1);
useEffect(() => {
const el = listRef.current;
if (viewMode !== "grid" || !el) return;
const update = () => {
const width = Math.max(el.clientWidth - 24, 100);
const cols = Math.max(
1,
Math.floor((width + GRID_GAP) / (GRID_CARD_W + GRID_GAP)),
);
setGridCols((previous) => (previous === cols ? previous : cols));
};
update();
const observer = new ResizeObserver(update);
observer.observe(el);
return () => observer.disconnect();
}, [viewMode]);
const gridRowCount =
viewMode === "grid" ? Math.ceil(filteredItems.length / gridCols) : 0;
const gridVirtualizer = useVirtualizer({
count: gridRowCount,
getScrollElement: () => listRef.current,
estimateSize: () => GRID_ROW_H,
overscan: 5,
});
// Infinite scroll: auto-load the next page when the sentinel enters view.
const sentinelRef = useRef<HTMLDivElement | null>(null);
const fetchStats = useCallback(async () => {
try {
const res = await adminFetch("/api/admin/import/furni?action=stats");
@@ -335,6 +374,39 @@ export function StudioClient({
[],
);
// Infinite scroll: auto-load the next page when the sentinel enters view.
useEffect(() => {
const el = sentinelRef.current;
if (!el || !hasMorePages) return;
let cancelled = false;
const observer = new IntersectionObserver(
(entries) => {
if (cancelled) return;
if (
entries.some((e) => e.isIntersecting) &&
!loading &&
!batchProgress
) {
fetchItems(activeSearch, (meta?.currentPage ?? 1) + 1, activeSource);
}
},
{ root: listRef.current, rootMargin: "700px 0px" },
);
observer.observe(el);
return () => {
cancelled = true;
observer.disconnect();
};
}, [
hasMorePages,
meta?.currentPage,
loading,
batchProgress,
activeSearch,
activeSource,
fetchItems,
]);
const changeSource = (sourceId: string) => {
sourceGeneration.current += 1;
setConfirmDelete(false);
@@ -1631,118 +1703,153 @@ export function StudioClient({
) : (
<>
{viewMode === "grid" ? (
<div className="grid grid-cols-[repeat(auto-fill,minmax(150px,1fr))] gap-3">
{filteredItems.map((item) => {
const isSel = selected.has(item.classname);
<div
className="relative w-full"
style={{ height: `${gridVirtualizer.getTotalSize()}px` }}
>
{gridVirtualizer.getVirtualItems().map((vi) => {
const start = vi.index * gridCols;
const rowItems = filteredItems.slice(
start,
start + gridCols,
);
return (
<div key={item.classname} className="group relative">
<button
type="button"
onClick={() => openDetail(item)}
className={cn(
"flex w-full flex-col overflow-hidden rounded-xl border text-left transition-all outline-none focus-visible:ring-2 focus-visible:ring-[var(--admin-accent)]",
isSel
? "border-[var(--admin-accent)] bg-[var(--admin-accent)]/10"
: "border-[var(--admin-border)] bg-[var(--admin-surface)] hover:-translate-y-0.5 hover:border-[var(--admin-accent)]/60 hover:shadow-lg hover:shadow-[var(--admin-accent)]/10",
)}
>
<div
className="relative flex h-24 items-center justify-center overflow-hidden"
style={{
backgroundImage:
"radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--admin-text-muted) 14%, transparent) 1px, transparent 0)",
backgroundSize: "14px 14px",
}}
>
<img
src={getFurniImageUrl(item)}
alt={item.name}
className="h-14 w-14 object-contain transition-transform group-hover:scale-110"
style={{ imageRendering: "pixelated" }}
loading="lazy"
onError={(e) => handleImgError(e, item)}
/>
</div>
<div className="flex items-start justify-between gap-1 border-t border-[var(--admin-border)] px-2 py-1.5">
<div className="min-w-0">
<p className="truncate text-xs font-semibold text-[var(--admin-text)]">
{item.name}
</p>
<p className="truncate font-mono text-[0.6rem] text-[var(--admin-text-muted)]">
{item.classname}
</p>
</div>
</div>
<div className="flex items-center gap-1 px-2 pb-1.5">
<Badge className="h-4 bg-[var(--admin-accent)] text-[var(--admin-accent-foreground)]">
{TYPE_LABELS[item.type] ?? item.type}
</Badge>
{item.alreadyImported ? (
<>
{!item.nitroExists && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-warning)] border-[var(--admin-warning)]/40"
>
No nitro
</Badge>
)}
{item.hasFurnidata === false && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-warning)] border-[var(--admin-warning)]/40"
>
No furnidata
</Badge>
)}
{item.hasCatalogEntry === false && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-warning)] border-[var(--admin-warning)]/40"
>
No catalog
</Badge>
)}
{item.nitroExists &&
item.hasFurnidata !== false &&
item.hasCatalogEntry !== false && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-success)] border-[var(--admin-success)]/40"
>
Imported
</Badge>
)}
</>
) : (
<Badge className="h-4 bg-[var(--admin-accent)] text-[var(--admin-accent-foreground)]">
New
</Badge>
)}
</div>
</button>
<button
type="button"
onClick={(e) => {
e.stopPropagation();
toggleSelect(item.classname);
<div
key={vi.key}
data-index={vi.index}
ref={gridVirtualizer.measureElement}
className="absolute left-0 top-0 w-full will-change-transform"
style={{ transform: `translateY(${vi.start}px)` }}
>
<div
className="grid gap-3"
style={{
gridTemplateColumns: `repeat(${gridCols}, minmax(0, 1fr))`,
}}
className={cn(
"absolute right-2 top-2 grid h-8 w-8 place-items-center rounded-md border transition-colors",
isSel
? "border-[var(--admin-accent)] bg-[var(--admin-accent)]"
: "border-[var(--admin-border)] bg-[var(--admin-surface)] hover:border-[var(--admin-accent)]/60",
)}
aria-label={`${isSel ? "Deselect" : "Select"} ${item.name}`}
aria-pressed={isSel}
>
{isSel ? (
<Check
size={12}
className="text-[var(--admin-accent-foreground)]"
/>
) : null}
</button>
{rowItems.map((item) => {
const isSel = selected.has(item.classname);
return (
<div
key={item.classname}
className="group relative"
>
<button
type="button"
onClick={() => openDetail(item)}
className={cn(
"flex w-full flex-col overflow-hidden rounded-xl border text-left transition-all outline-none focus-visible:ring-2 focus-visible:ring-[var(--admin-accent)]",
isSel
? "border-[var(--admin-accent)] bg-[var(--admin-accent)]/10 ring-2 ring-[var(--admin-accent)]/20"
: "border-[var(--admin-border)] bg-[var(--admin-surface)] hover:-translate-y-0.5 hover:border-[var(--admin-accent)]/60 hover:shadow-lg hover:shadow-[var(--admin-accent)]/10",
)}
>
<div
className="relative flex h-24 items-center justify-center overflow-hidden"
style={{
backgroundImage:
"radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--admin-text-muted) 14%, transparent) 1px, transparent 0)",
backgroundSize: "14px 14px",
}}
>
<div className="pointer-events-none absolute inset-x-0 top-0 h-10 bg-gradient-to-b from-[var(--admin-text)]/5 to-transparent" />
<img
src={getFurniImageUrl(item)}
alt={item.name}
className="h-14 w-14 object-contain drop-shadow-[0_2px_4px_rgba(0,0,0,0.25)] transition-transform duration-200 group-hover:scale-110"
style={{ imageRendering: "pixelated" }}
loading="lazy"
decoding="async"
onError={(e) => handleImgError(e, item)}
/>
</div>
<div className="flex items-start justify-between gap-1 border-t border-[var(--admin-border)] px-2 py-1.5">
<div className="min-w-0">
<p className="truncate text-xs font-semibold text-[var(--admin-text)]">
{item.name}
</p>
<p className="truncate font-mono text-[0.6rem] text-[var(--admin-text-muted)]">
{item.classname}
</p>
<p className="truncate text-[0.6rem] text-[var(--admin-text-muted)]/80">
{item.category || "—"}
</p>
</div>
</div>
<div className="flex items-center gap-1 px-2 pb-1.5">
<Badge className="h-4 bg-[var(--admin-accent)] text-[var(--admin-accent-foreground)]">
{TYPE_LABELS[item.type] ?? item.type}
</Badge>
{item.alreadyImported ? (
<>
{!item.nitroExists && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-warning)] border-[var(--admin-warning)]/40"
>
No nitro
</Badge>
)}
{item.hasFurnidata === false && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-warning)] border-[var(--admin-warning)]/40"
>
No furnidata
</Badge>
)}
{item.hasCatalogEntry === false && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-warning)] border-[var(--admin-warning)]/40"
>
No catalog
</Badge>
)}
{item.nitroExists &&
item.hasFurnidata !== false &&
item.hasCatalogEntry !== false && (
<Badge
variant="secondary"
className="h-4 text-[var(--admin-success)] border-[var(--admin-success)]/40"
>
Imported
</Badge>
)}
</>
) : (
<Badge className="h-4 bg-[var(--admin-accent)] text-[var(--admin-accent-foreground)]">
New
</Badge>
)}
</div>
</button>
<button
type="button"
onClick={(e) => {
e.stopPropagation();
toggleSelect(item.classname);
}}
className={cn(
"absolute right-2 top-2 grid h-8 w-8 place-items-center rounded-md border transition-all",
isSel
? "scale-105 border-[var(--admin-accent)] bg-[var(--admin-accent)] shadow-md shadow-[var(--admin-accent)]/30"
: "border-[var(--admin-border)] bg-[var(--admin-surface)] opacity-70 hover:border-[var(--admin-accent)]/60 hover:opacity-100",
)}
aria-label={`${isSel ? "Deselect" : "Select"} ${item.name}`}
aria-pressed={isSel}
>
{isSel ? (
<Check
size={12}
className="text-[var(--admin-accent-foreground)]"
/>
) : null}
</button>
</div>
);
})}
</div>
</div>
);
})}
@@ -1956,29 +2063,33 @@ export function StudioClient({
</div>
)}
{meta && meta.currentPage < meta.lastPage && (
<div className="mt-4 flex justify-center">
<Button
variant="secondary"
onClick={() =>
fetchItems(
activeSearch,
meta.currentPage + 1,
activeSource,
)
}
disabled={loading}
>
{loading ? (
<Loader2 size={14} className="animate-spin" />
) : (
<Download size={14} />
)}
Load more ({meta.total - meta.currentPage * meta.perPage}{" "}
left)
</Button>
</div>
)}
<div
ref={sentinelRef}
className="mt-4 flex flex-col items-center justify-center gap-2 py-2 text-xs text-[var(--admin-text-muted)]"
>
{hasMorePages ? (
loading ? (
<span className="flex items-center gap-2">
<Loader2
size={14}
className="animate-spin text-[var(--admin-accent)]"
/>
Loading more…
</span>
) : (
<span className="flex items-center gap-2">
<ArrowDown size={12} />
Scroll for more
</span>
)
) : (
<span className="flex items-center gap-2">
<span className="h-px w-8 bg-[var(--admin-border)]" />
End of results
<span className="h-px w-8 bg-[var(--admin-border)]" />
</span>
)}
</div>
</>
)}
</div>