fix(admin): harden clothing/pets/effects/clone imports
Align grids on data.items, only treat SSE done as success, hard-fail clothing sets when libs fail, and add Cancel via AbortController. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
9c4949186c
commit
16191cef14
8 files changed
+344
-173
No files matched your search
@@ -13,7 +13,7 @@ import {
|
||||
Trash2,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@@ -355,6 +355,7 @@ function FurniGrid({ source }: FurniGridProps) {
|
||||
} | null>(null);
|
||||
const [busyClassname, setBusyClassname] = useState<string | null>(null);
|
||||
const [filter, setFilter] = useState<"all" | "missing" | "present">("all");
|
||||
const batchAbortRef = useRef<AbortController | null>(null);
|
||||
|
||||
const fetchItems = useCallback(
|
||||
async (
|
||||
@@ -378,7 +379,7 @@ function FurniGrid({ source }: FurniGridProps) {
|
||||
setError(data.error || "Failed to load");
|
||||
return;
|
||||
}
|
||||
setItems(data.item || []);
|
||||
setItems(data.items || []);
|
||||
setMeta(data.meta || null);
|
||||
setStats(data.stats || null);
|
||||
setSelected(new Set());
|
||||
@@ -442,30 +443,46 @@ function FurniGrid({ source }: FurniGridProps) {
|
||||
}
|
||||
|
||||
async function cloneViaSse(toClone: FurniItem[]) {
|
||||
batchAbortRef.current?.abort();
|
||||
const abort = new AbortController();
|
||||
batchAbortRef.current = abort;
|
||||
setBatchProgress({ done: 0, total: toClone.length });
|
||||
let done = 0;
|
||||
await runSseImport(
|
||||
"/api/admin/import/clone/batch",
|
||||
{
|
||||
sourceId: source.id,
|
||||
classnames: toClone.map((it) => it.classname),
|
||||
concurrency: 2,
|
||||
},
|
||||
(classname) => {
|
||||
done++;
|
||||
markDone(classname);
|
||||
setBatchProgress({ done, total: toClone.length });
|
||||
},
|
||||
(succeeded, failed) => {
|
||||
setBatchProgress(null);
|
||||
setSelected(new Set());
|
||||
if (succeeded > 0) toast.success(`${succeeded} furni cloned`);
|
||||
if (failed > 0) toast.error(`${failed} failed`);
|
||||
// Refresh stats after batch
|
||||
fetchItems(activeSearch, page, filter);
|
||||
},
|
||||
);
|
||||
setBatchProgress(null);
|
||||
try {
|
||||
await runSseImport(
|
||||
"/api/admin/import/clone/batch",
|
||||
{
|
||||
sourceId: source.id,
|
||||
classnames: toClone.map((it) => it.classname),
|
||||
concurrency: 2,
|
||||
},
|
||||
(classname) => {
|
||||
done++;
|
||||
markDone(classname);
|
||||
setBatchProgress({ done, total: toClone.length });
|
||||
},
|
||||
(succeeded, failed) => {
|
||||
setSelected(new Set());
|
||||
if (succeeded > 0) toast.success(`${succeeded} furni cloned`);
|
||||
if (failed > 0) toast.error(`${failed} failed`);
|
||||
fetchItems(activeSearch, page, filter);
|
||||
},
|
||||
abort.signal,
|
||||
() => {
|
||||
done++;
|
||||
setBatchProgress({ done, total: toClone.length });
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
if ((err as Error)?.name === "AbortError") {
|
||||
toast.info("Clone cancelled");
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
} finally {
|
||||
if (batchAbortRef.current === abort) batchAbortRef.current = null;
|
||||
setBatchProgress(null);
|
||||
}
|
||||
}
|
||||
|
||||
async function cloneOne(it: FurniItem) {
|
||||
@@ -514,32 +531,55 @@ function FurniGrid({ source }: FurniGridProps) {
|
||||
) {
|
||||
return;
|
||||
}
|
||||
batchAbortRef.current?.abort();
|
||||
const abort = new AbortController();
|
||||
batchAbortRef.current = abort;
|
||||
const CHUNK = 400;
|
||||
setBatchProgress({ done: 0, total: names.length });
|
||||
let done = 0;
|
||||
let okCount = 0;
|
||||
let failCount = 0;
|
||||
for (let i = 0; i < names.length; i += CHUNK) {
|
||||
const chunk = names.slice(i, i + CHUNK);
|
||||
await runSseImport(
|
||||
"/api/admin/import/clone/batch",
|
||||
{ sourceId: source.id, classnames: chunk, concurrency: 2 },
|
||||
(classname) => {
|
||||
done++;
|
||||
markDone(classname);
|
||||
setBatchProgress({ done, total: names.length });
|
||||
},
|
||||
(succeeded, failed) => {
|
||||
okCount += succeeded;
|
||||
failCount += failed;
|
||||
},
|
||||
);
|
||||
try {
|
||||
for (let i = 0; i < names.length; i += CHUNK) {
|
||||
if (abort.signal.aborted) break;
|
||||
const chunk = names.slice(i, i + CHUNK);
|
||||
await runSseImport(
|
||||
"/api/admin/import/clone/batch",
|
||||
{ sourceId: source.id, classnames: chunk, concurrency: 2 },
|
||||
(classname) => {
|
||||
done++;
|
||||
markDone(classname);
|
||||
setBatchProgress({ done, total: names.length });
|
||||
},
|
||||
(succeeded, failed) => {
|
||||
okCount += succeeded;
|
||||
failCount += failed;
|
||||
},
|
||||
abort.signal,
|
||||
() => {
|
||||
done++;
|
||||
setBatchProgress({ done, total: names.length });
|
||||
},
|
||||
);
|
||||
}
|
||||
if (abort.signal.aborted) {
|
||||
toast.info("Clone cancelled");
|
||||
} else {
|
||||
if (okCount > 0) toast.success(`${okCount} furni cloned`);
|
||||
if (failCount > 0) toast.error(`${failCount} failed`);
|
||||
}
|
||||
setSelected(new Set());
|
||||
fetchItems(activeSearch, page, filter);
|
||||
} catch (err) {
|
||||
if ((err as Error)?.name === "AbortError") {
|
||||
toast.info("Clone cancelled");
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
} finally {
|
||||
if (batchAbortRef.current === abort) batchAbortRef.current = null;
|
||||
setBatchProgress(null);
|
||||
}
|
||||
setBatchProgress(null);
|
||||
setSelected(new Set());
|
||||
if (okCount > 0) toast.success(`${okCount} furni cloned`);
|
||||
if (failCount > 0) toast.error(`${failCount} failed`);
|
||||
fetchItems(activeSearch, page, filter);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -692,9 +732,19 @@ function FurniGrid({ source }: FurniGridProps) {
|
||||
</Button>
|
||||
)}
|
||||
{batchProgress && (
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{batchProgress.done}/{batchProgress.total}
|
||||
</span>
|
||||
<>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{batchProgress.done}/{batchProgress.total}
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-8"
|
||||
onClick={() => batchAbortRef.current?.abort()}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user