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,
|
Trash2,
|
||||||
X,
|
X,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -355,6 +355,7 @@ function FurniGrid({ source }: FurniGridProps) {
|
|||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [busyClassname, setBusyClassname] = useState<string | null>(null);
|
const [busyClassname, setBusyClassname] = useState<string | null>(null);
|
||||||
const [filter, setFilter] = useState<"all" | "missing" | "present">("all");
|
const [filter, setFilter] = useState<"all" | "missing" | "present">("all");
|
||||||
|
const batchAbortRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
const fetchItems = useCallback(
|
const fetchItems = useCallback(
|
||||||
async (
|
async (
|
||||||
@@ -378,7 +379,7 @@ function FurniGrid({ source }: FurniGridProps) {
|
|||||||
setError(data.error || "Failed to load");
|
setError(data.error || "Failed to load");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setItems(data.item || []);
|
setItems(data.items || []);
|
||||||
setMeta(data.meta || null);
|
setMeta(data.meta || null);
|
||||||
setStats(data.stats || null);
|
setStats(data.stats || null);
|
||||||
setSelected(new Set());
|
setSelected(new Set());
|
||||||
@@ -442,30 +443,46 @@ function FurniGrid({ source }: FurniGridProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function cloneViaSse(toClone: FurniItem[]) {
|
async function cloneViaSse(toClone: FurniItem[]) {
|
||||||
|
batchAbortRef.current?.abort();
|
||||||
|
const abort = new AbortController();
|
||||||
|
batchAbortRef.current = abort;
|
||||||
setBatchProgress({ done: 0, total: toClone.length });
|
setBatchProgress({ done: 0, total: toClone.length });
|
||||||
let done = 0;
|
let done = 0;
|
||||||
await runSseImport(
|
try {
|
||||||
"/api/admin/import/clone/batch",
|
await runSseImport(
|
||||||
{
|
"/api/admin/import/clone/batch",
|
||||||
sourceId: source.id,
|
{
|
||||||
classnames: toClone.map((it) => it.classname),
|
sourceId: source.id,
|
||||||
concurrency: 2,
|
classnames: toClone.map((it) => it.classname),
|
||||||
},
|
concurrency: 2,
|
||||||
(classname) => {
|
},
|
||||||
done++;
|
(classname) => {
|
||||||
markDone(classname);
|
done++;
|
||||||
setBatchProgress({ done, total: toClone.length });
|
markDone(classname);
|
||||||
},
|
setBatchProgress({ done, total: toClone.length });
|
||||||
(succeeded, failed) => {
|
},
|
||||||
setBatchProgress(null);
|
(succeeded, failed) => {
|
||||||
setSelected(new Set());
|
setSelected(new Set());
|
||||||
if (succeeded > 0) toast.success(`${succeeded} furni cloned`);
|
if (succeeded > 0) toast.success(`${succeeded} furni cloned`);
|
||||||
if (failed > 0) toast.error(`${failed} failed`);
|
if (failed > 0) toast.error(`${failed} failed`);
|
||||||
// Refresh stats after batch
|
fetchItems(activeSearch, page, filter);
|
||||||
fetchItems(activeSearch, page, filter);
|
},
|
||||||
},
|
abort.signal,
|
||||||
);
|
() => {
|
||||||
setBatchProgress(null);
|
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) {
|
async function cloneOne(it: FurniItem) {
|
||||||
@@ -514,32 +531,55 @@ function FurniGrid({ source }: FurniGridProps) {
|
|||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
batchAbortRef.current?.abort();
|
||||||
|
const abort = new AbortController();
|
||||||
|
batchAbortRef.current = abort;
|
||||||
const CHUNK = 400;
|
const CHUNK = 400;
|
||||||
setBatchProgress({ done: 0, total: names.length });
|
setBatchProgress({ done: 0, total: names.length });
|
||||||
let done = 0;
|
let done = 0;
|
||||||
let okCount = 0;
|
let okCount = 0;
|
||||||
let failCount = 0;
|
let failCount = 0;
|
||||||
for (let i = 0; i < names.length; i += CHUNK) {
|
try {
|
||||||
const chunk = names.slice(i, i + CHUNK);
|
for (let i = 0; i < names.length; i += CHUNK) {
|
||||||
await runSseImport(
|
if (abort.signal.aborted) break;
|
||||||
"/api/admin/import/clone/batch",
|
const chunk = names.slice(i, i + CHUNK);
|
||||||
{ sourceId: source.id, classnames: chunk, concurrency: 2 },
|
await runSseImport(
|
||||||
(classname) => {
|
"/api/admin/import/clone/batch",
|
||||||
done++;
|
{ sourceId: source.id, classnames: chunk, concurrency: 2 },
|
||||||
markDone(classname);
|
(classname) => {
|
||||||
setBatchProgress({ done, total: names.length });
|
done++;
|
||||||
},
|
markDone(classname);
|
||||||
(succeeded, failed) => {
|
setBatchProgress({ done, total: names.length });
|
||||||
okCount += succeeded;
|
},
|
||||||
failCount += failed;
|
(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 (
|
return (
|
||||||
@@ -692,9 +732,19 @@ function FurniGrid({ source }: FurniGridProps) {
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{batchProgress && (
|
{batchProgress && (
|
||||||
<span className="text-xs text-muted-foreground">
|
<>
|
||||||
{batchProgress.done}/{batchProgress.total}
|
<span className="text-xs text-muted-foreground">
|
||||||
</span>
|
{batchProgress.done}/{batchProgress.total}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="h-8"
|
||||||
|
onClick={() => batchAbortRef.current?.abort()}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
Trash2,
|
Trash2,
|
||||||
X,
|
X,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -103,6 +103,7 @@ function LibsView() {
|
|||||||
total: number;
|
total: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [busyLib, setBusyLib] = useState<string | null>(null);
|
const [busyLib, setBusyLib] = useState<string | null>(null);
|
||||||
|
const batchAbortRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
const fetchItems = useCallback(async (search: string, pageNum: number) => {
|
const fetchItems = useCallback(async (search: string, pageNum: number) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -116,7 +117,7 @@ function LibsView() {
|
|||||||
setError(data.error || "Failed to load");
|
setError(data.error || "Failed to load");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setItems(data.item || []);
|
setItems(data.items || []);
|
||||||
setTotal(data.meta?.total || 0);
|
setTotal(data.meta?.total || 0);
|
||||||
setSelected(new Set());
|
setSelected(new Set());
|
||||||
} catch {
|
} catch {
|
||||||
@@ -168,24 +169,41 @@ function LibsView() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function importViaSse(toImport: FigureItem[]) {
|
async function importViaSse(toImport: FigureItem[]) {
|
||||||
|
batchAbortRef.current?.abort();
|
||||||
|
const abort = new AbortController();
|
||||||
|
batchAbortRef.current = abort;
|
||||||
setBatchProgress({ done: 0, total: toImport.length });
|
setBatchProgress({ done: 0, total: toImport.length });
|
||||||
let done = 0;
|
let done = 0;
|
||||||
await runSseImport(
|
try {
|
||||||
"/api/admin/import/clothing/batch",
|
await runSseImport(
|
||||||
{ items: toImport, concurrency: 3 },
|
"/api/admin/import/clothing/batch",
|
||||||
(label) => {
|
{ items: toImport, concurrency: 3 },
|
||||||
done++;
|
(label) => {
|
||||||
markDone(label);
|
done++;
|
||||||
setBatchProgress({ done, total: toImport.length });
|
markDone(label);
|
||||||
},
|
setBatchProgress({ done, total: toImport.length });
|
||||||
(succeeded, failed) => {
|
},
|
||||||
setBatchProgress(null);
|
(succeeded, failed) => {
|
||||||
setSelected(new Set());
|
setSelected(new Set());
|
||||||
if (succeeded > 0) toast.success(`${succeeded} libraries imported`);
|
if (succeeded > 0) toast.success(`${succeeded} libraries imported`);
|
||||||
if (failed > 0) toast.error(`${failed} failed`);
|
if (failed > 0) toast.error(`${failed} failed`);
|
||||||
},
|
},
|
||||||
);
|
abort.signal,
|
||||||
setBatchProgress(null);
|
() => {
|
||||||
|
done++;
|
||||||
|
setBatchProgress({ done, total: toImport.length });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
if ((err as Error)?.name === "AbortError") {
|
||||||
|
toast.info("Import cancelled");
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (batchAbortRef.current === abort) batchAbortRef.current = null;
|
||||||
|
setBatchProgress(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importOne(p: FigureItem) {
|
async function importOne(p: FigureItem) {
|
||||||
@@ -327,9 +345,19 @@ function LibsView() {
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{batchProgress && (
|
{batchProgress && (
|
||||||
<span className="text-xs text-muted-foreground">
|
<>
|
||||||
{batchProgress.done}/{batchProgress.total}
|
<span className="text-xs text-muted-foreground">
|
||||||
</span>
|
{batchProgress.done}/{batchProgress.total}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="h-8"
|
||||||
|
onClick={() => batchAbortRef.current?.abort()}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -476,6 +504,7 @@ function SetsView() {
|
|||||||
total: number;
|
total: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [busyKey, setBusyKey] = useState<string | null>(null);
|
const [busyKey, setBusyKey] = useState<string | null>(null);
|
||||||
|
const batchAbortRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
function itemKey(it: ClothingSetItem): string {
|
function itemKey(it: ClothingSetItem): string {
|
||||||
return `${it.setType}-${it.setId}`;
|
return `${it.setType}-${it.setId}`;
|
||||||
@@ -493,7 +522,7 @@ function SetsView() {
|
|||||||
setError(data.error || "Failed to load");
|
setError(data.error || "Failed to load");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setItems(data.item || []);
|
setItems(data.items || []);
|
||||||
setTotal(data.meta?.total || 0);
|
setTotal(data.meta?.total || 0);
|
||||||
setSelected(new Set());
|
setSelected(new Set());
|
||||||
} catch {
|
} catch {
|
||||||
@@ -542,27 +571,47 @@ function SetsView() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function importViaSse(toImport: ClothingSetItem[]) {
|
async function importViaSse(toImport: ClothingSetItem[]) {
|
||||||
|
batchAbortRef.current?.abort();
|
||||||
|
const abort = new AbortController();
|
||||||
|
batchAbortRef.current = abort;
|
||||||
setBatchProgress({ done: 0, total: toImport.length });
|
setBatchProgress({ done: 0, total: toImport.length });
|
||||||
let done = 0;
|
let done = 0;
|
||||||
await runSseImport(
|
try {
|
||||||
"/api/admin/import/clothing/sets/batch",
|
await runSseImport(
|
||||||
{
|
"/api/admin/import/clothing/sets/batch",
|
||||||
items: toImport.map((it) => ({ setType: it.setType, setId: it.setId })),
|
{
|
||||||
concurrency: 3,
|
items: toImport.map((it) => ({
|
||||||
},
|
setType: it.setType,
|
||||||
(label) => {
|
setId: it.setId,
|
||||||
done++;
|
})),
|
||||||
markDone(label);
|
concurrency: 3,
|
||||||
setBatchProgress({ done, total: toImport.length });
|
},
|
||||||
},
|
(label) => {
|
||||||
(succeeded, failed) => {
|
done++;
|
||||||
setBatchProgress(null);
|
markDone(label);
|
||||||
setSelected(new Set());
|
setBatchProgress({ done, total: toImport.length });
|
||||||
if (succeeded > 0) toast.success(`${succeeded} sets imported`);
|
},
|
||||||
if (failed > 0) toast.error(`${failed} failed`);
|
(succeeded, failed) => {
|
||||||
},
|
setSelected(new Set());
|
||||||
);
|
if (succeeded > 0) toast.success(`${succeeded} sets imported`);
|
||||||
setBatchProgress(null);
|
if (failed > 0) toast.error(`${failed} failed`);
|
||||||
|
},
|
||||||
|
abort.signal,
|
||||||
|
() => {
|
||||||
|
done++;
|
||||||
|
setBatchProgress({ done, total: toImport.length });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
if ((err as Error)?.name === "AbortError") {
|
||||||
|
toast.info("Import cancelled");
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (batchAbortRef.current === abort) batchAbortRef.current = null;
|
||||||
|
setBatchProgress(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importOne(it: ClothingSetItem) {
|
async function importOne(it: ClothingSetItem) {
|
||||||
@@ -684,9 +733,19 @@ function SetsView() {
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{batchProgress && (
|
{batchProgress && (
|
||||||
<span className="text-xs text-muted-foreground">
|
<>
|
||||||
{batchProgress.done}/{batchProgress.total}
|
<span className="text-xs text-muted-foreground">
|
||||||
</span>
|
{batchProgress.done}/{batchProgress.total}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="h-8"
|
||||||
|
onClick={() => batchAbortRef.current?.abort()}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -838,7 +897,7 @@ export function ImportClothingClient() {
|
|||||||
className="h-7 text-xs"
|
className="h-7 text-xs"
|
||||||
onClick={() => setMode("libs")}
|
onClick={() => setMode("libs")}
|
||||||
>
|
>
|
||||||
Librerie
|
Libraries
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
@@ -846,7 +905,7 @@ export function ImportClothingClient() {
|
|||||||
className="h-7 text-xs"
|
className="h-7 text-xs"
|
||||||
onClick={() => setMode("sets")}
|
onClick={() => setMode("sets")}
|
||||||
>
|
>
|
||||||
Vestiti (set)
|
Sets
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,13 @@ import {
|
|||||||
Trash2,
|
Trash2,
|
||||||
X,
|
X,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { adminFetch } from "@/lib/admin-fetch";
|
import { adminFetch } from "@/lib/admin-fetch";
|
||||||
|
import { runSseImport } from "@/lib/sse-client";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
interface EffectItem {
|
interface EffectItem {
|
||||||
@@ -44,6 +45,7 @@ export function ImportEffectsClient() {
|
|||||||
total: number;
|
total: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [busyLib, setBusyLib] = useState<string | null>(null);
|
const [busyLib, setBusyLib] = useState<string | null>(null);
|
||||||
|
const batchAbortRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
const fetchEffects = useCallback(async (search: string) => {
|
const fetchEffects = useCallback(async (search: string) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -101,51 +103,41 @@ export function ImportEffectsClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function importViaSse(items: EffectItem[]) {
|
async function importViaSse(items: EffectItem[]) {
|
||||||
|
batchAbortRef.current?.abort();
|
||||||
|
const abort = new AbortController();
|
||||||
|
batchAbortRef.current = abort;
|
||||||
setBatchProgress({ done: 0, total: items.length });
|
setBatchProgress({ done: 0, total: items.length });
|
||||||
const res = await adminFetch("/api/admin/import/effects/batch", {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ items, concurrency: 3 }),
|
|
||||||
});
|
|
||||||
if (!res.body) {
|
|
||||||
toast.error("No response stream");
|
|
||||||
setBatchProgress(null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const reader = res.body.getReader();
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
let buf = "";
|
|
||||||
let done = 0;
|
let done = 0;
|
||||||
let succeeded = 0;
|
try {
|
||||||
let failed = 0;
|
await runSseImport(
|
||||||
while (true) {
|
"/api/admin/import/effects/batch",
|
||||||
const { value, done: streamDone } = await reader.read();
|
{ items, concurrency: 3 },
|
||||||
if (streamDone) break;
|
(lib) => {
|
||||||
buf += decoder.decode(value, { stream: true });
|
|
||||||
const parts = buf.split("\n\n");
|
|
||||||
buf = parts.pop() ?? "";
|
|
||||||
for (const part of parts) {
|
|
||||||
if (!part.startsWith("data: ")) continue;
|
|
||||||
const evt = JSON.parse(part.slice(6));
|
|
||||||
if (evt.type === "item_progress" && evt.status === "done") {
|
|
||||||
done++;
|
done++;
|
||||||
succeeded++;
|
markDone(lib);
|
||||||
markDone(evt.classname);
|
|
||||||
setBatchProgress({ done, total: items.length });
|
setBatchProgress({ done, total: items.length });
|
||||||
} else if (evt.type === "item_progress" && evt.status === "failed") {
|
},
|
||||||
|
(succeeded, failed) => {
|
||||||
|
setSelected(new Set());
|
||||||
|
if (succeeded > 0) toast.success(`${succeeded} effect(s) imported`);
|
||||||
|
if (failed > 0) toast.error(`${failed} failed`);
|
||||||
|
},
|
||||||
|
abort.signal,
|
||||||
|
() => {
|
||||||
done++;
|
done++;
|
||||||
failed++;
|
|
||||||
setBatchProgress({ done, total: items.length });
|
setBatchProgress({ done, total: items.length });
|
||||||
} else if (evt.type === "batch_complete") {
|
},
|
||||||
succeeded = evt.succeeded;
|
);
|
||||||
failed = evt.failed;
|
} catch (err) {
|
||||||
}
|
if ((err as Error)?.name === "AbortError") {
|
||||||
|
toast.info("Import cancelled");
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
if (batchAbortRef.current === abort) batchAbortRef.current = null;
|
||||||
|
setBatchProgress(null);
|
||||||
}
|
}
|
||||||
setBatchProgress(null);
|
|
||||||
setSelected(new Set());
|
|
||||||
if (succeeded > 0) toast.success(`${succeeded} effect(s) imported`);
|
|
||||||
if (failed > 0) toast.error(`${failed} failed`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importOne(e: EffectItem) {
|
async function importOne(e: EffectItem) {
|
||||||
@@ -304,9 +296,19 @@ export function ImportEffectsClient() {
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{batchProgress && (
|
{batchProgress && (
|
||||||
<span className="text-xs text-muted-foreground">
|
<>
|
||||||
{batchProgress.done}/{batchProgress.total}
|
<span className="text-xs text-muted-foreground">
|
||||||
</span>
|
{batchProgress.done}/{batchProgress.total}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="h-8"
|
||||||
|
onClick={() => batchAbortRef.current?.abort()}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -336,7 +336,7 @@ export function ImportFurniClient() {
|
|||||||
setError(data.error || "Failed to load");
|
setError(data.error || "Failed to load");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const loadedItems = data.item || [];
|
const loadedItems = data.items || [];
|
||||||
setItems(loadedItems);
|
setItems(loadedItems);
|
||||||
setMeta(data.meta || null);
|
setMeta(data.meta || null);
|
||||||
setSelected(new Set());
|
setSelected(new Set());
|
||||||
|
|||||||
@@ -11,13 +11,13 @@ import {
|
|||||||
Trash2,
|
Trash2,
|
||||||
X,
|
X,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { adminFetch } from "@/lib/admin-fetch";
|
import { adminFetch } from "@/lib/admin-fetch";
|
||||||
import { readSseStream } from "@/lib/sse-client";
|
import { runSseImport } from "@/lib/sse-client";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
interface PetItem {
|
interface PetItem {
|
||||||
@@ -51,6 +51,7 @@ export function ImportPetsClient() {
|
|||||||
total: number;
|
total: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [busyLib, setBusyLib] = useState<string | null>(null);
|
const [busyLib, setBusyLib] = useState<string | null>(null);
|
||||||
|
const batchAbortRef = useRef<AbortController | null>(null);
|
||||||
|
|
||||||
const fetchPets = useCallback(async (search: string) => {
|
const fetchPets = useCallback(async (search: string) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -99,37 +100,41 @@ export function ImportPetsClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function importViaSse(items: PetItem[]) {
|
async function importViaSse(items: PetItem[]) {
|
||||||
|
batchAbortRef.current?.abort();
|
||||||
|
const abort = new AbortController();
|
||||||
|
batchAbortRef.current = abort;
|
||||||
setBatchProgress({ done: 0, total: items.length });
|
setBatchProgress({ done: 0, total: items.length });
|
||||||
const res = await adminFetch("/api/admin/import/pets/batch", {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ items, concurrency: 3 }),
|
|
||||||
});
|
|
||||||
if (!res.body) {
|
|
||||||
toast.error("No response stream");
|
|
||||||
setBatchProgress(null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let done = 0;
|
let done = 0;
|
||||||
let succeeded = 0;
|
try {
|
||||||
let failed = 0;
|
await runSseImport(
|
||||||
await readSseStream(res.body, (evt) => {
|
"/api/admin/import/pets/batch",
|
||||||
if (evt.type === "item_progress" && evt.status === "done") {
|
{ items, concurrency: 3 },
|
||||||
done++;
|
(lib) => {
|
||||||
markDone(String(evt.classname ?? ""));
|
done++;
|
||||||
setBatchProgress({ done, total: items.length });
|
markDone(lib);
|
||||||
} else if (evt.type === "item_progress" && evt.status === "failed") {
|
setBatchProgress({ done, total: items.length });
|
||||||
done++;
|
},
|
||||||
setBatchProgress({ done, total: items.length });
|
(succeeded, failed) => {
|
||||||
} else if (evt.type === "batch_complete") {
|
setSelected(new Set());
|
||||||
succeeded = Number(evt.succeeded ?? 0);
|
if (succeeded > 0) toast.success(`${succeeded} pet(s) imported`);
|
||||||
failed = Number(evt.failed ?? 0);
|
if (failed > 0) toast.error(`${failed} failed`);
|
||||||
|
},
|
||||||
|
abort.signal,
|
||||||
|
() => {
|
||||||
|
done++;
|
||||||
|
setBatchProgress({ done, total: items.length });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
if ((err as Error)?.name === "AbortError") {
|
||||||
|
toast.info("Import cancelled");
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
});
|
} finally {
|
||||||
setBatchProgress(null);
|
if (batchAbortRef.current === abort) batchAbortRef.current = null;
|
||||||
setSelected(new Set());
|
setBatchProgress(null);
|
||||||
if (succeeded > 0) toast.success(`${succeeded} pet(s) imported`);
|
}
|
||||||
if (failed > 0) toast.error(`${failed} failed`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importOne(p: PetItem) {
|
async function importOne(p: PetItem) {
|
||||||
@@ -271,9 +276,19 @@ export function ImportPetsClient() {
|
|||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{batchProgress && (
|
{batchProgress && (
|
||||||
<span className="text-xs text-muted-foreground">
|
<>
|
||||||
{batchProgress.done}/{batchProgress.total}
|
<span className="text-xs text-muted-foreground">
|
||||||
</span>
|
{batchProgress.done}/{batchProgress.total}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="h-8"
|
||||||
|
onClick={() => batchAbortRef.current?.abort()}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -87,4 +87,20 @@ describe("importClothingSet", () => {
|
|||||||
const r = await importClothingSet({ setType: "ch", setId: 999999 });
|
const r = await importClothingSet({ setType: "ch", setId: 999999 });
|
||||||
expect(r.ok).toBe(false);
|
expect(r.ok).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not write set data when a required library import fails", async () => {
|
||||||
|
mergeSet.mockClear();
|
||||||
|
mergePalette.mockClear();
|
||||||
|
importSingleFigure.mockResolvedValueOnce({
|
||||||
|
ok: false,
|
||||||
|
lib: "shirt_M_x",
|
||||||
|
warnings: [],
|
||||||
|
error: "nitro missing",
|
||||||
|
});
|
||||||
|
const r = await importClothingSet({ setType: "ch", setId: 5000 });
|
||||||
|
expect(r.ok).toBe(false);
|
||||||
|
expect(r.error).toMatch(/required figure libraries failed/i);
|
||||||
|
expect(mergeSet).not.toHaveBeenCalled();
|
||||||
|
expect(mergePalette).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -133,6 +133,8 @@ export async function importClothingSet(params: {
|
|||||||
|
|
||||||
await ensureFigureDirs();
|
await ensureFigureDirs();
|
||||||
|
|
||||||
|
let hardFail = false;
|
||||||
|
|
||||||
for (const libId of requiredLibIds) {
|
for (const libId of requiredLibIds) {
|
||||||
const officialLib = officialLibs.find((l) => l.id === libId);
|
const officialLib = officialLibs.find((l) => l.id === libId);
|
||||||
if (!officialLib) continue;
|
if (!officialLib) continue;
|
||||||
@@ -141,18 +143,31 @@ export async function importClothingSet(params: {
|
|||||||
onProgress?.(`importing library ${libId}`);
|
onProgress?.(`importing library ${libId}`);
|
||||||
const result = await importSingleFigure({ lib: libId, onProgress });
|
const result = await importSingleFigure({ lib: libId, onProgress });
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
|
hardFail = true;
|
||||||
warnings.push(
|
warnings.push(
|
||||||
`Library import failed for ${libId}: ${result.error ?? "unknown error"}`,
|
`Library import failed for ${libId}: ${result.error ?? "unknown error"}`,
|
||||||
);
|
);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (result.warnings.length) {
|
if (result.warnings.length) {
|
||||||
warnings.push(...result.warnings);
|
warnings.push(...result.warnings);
|
||||||
}
|
}
|
||||||
|
localLibIds.add(libId);
|
||||||
}
|
}
|
||||||
|
|
||||||
await mergeLibrary(officialLib);
|
await mergeLibrary(officialLib);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hardFail) {
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
setId,
|
||||||
|
warnings,
|
||||||
|
error:
|
||||||
|
"One or more required figure libraries failed to import — set was not written",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const mappedSet = mapOfficialSet(set);
|
const mappedSet = mapOfficialSet(set);
|
||||||
await mergeSet(setType, st.paletteId, mappedSet as never);
|
await mergeSet(setType, st.paletteId, mappedSet as never);
|
||||||
|
|
||||||
|
|||||||
+19
-5
@@ -50,6 +50,7 @@ export async function runSseImport(
|
|||||||
onDone: (classname: string) => void,
|
onDone: (classname: string) => void,
|
||||||
onComplete: (succeeded: number, failed: number) => void,
|
onComplete: (succeeded: number, failed: number) => void,
|
||||||
signal?: AbortSignal,
|
signal?: AbortSignal,
|
||||||
|
onFailed?: (classname: string, error?: string) => void,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const res = await adminFetch(url, {
|
const res = await adminFetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -57,8 +58,19 @@ export async function runSseImport(
|
|||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
signal,
|
signal,
|
||||||
});
|
});
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => ({}));
|
||||||
|
toast.error(
|
||||||
|
typeof data.error === "string"
|
||||||
|
? data.error
|
||||||
|
: `Import failed (${res.status})`,
|
||||||
|
);
|
||||||
|
onComplete(0, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!res.body) {
|
if (!res.body) {
|
||||||
toast.error("No response stream");
|
toast.error("No response stream");
|
||||||
|
onComplete(0, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,11 +80,13 @@ export async function runSseImport(
|
|||||||
await readSseStream(
|
await readSseStream(
|
||||||
res.body,
|
res.body,
|
||||||
(evt) => {
|
(evt) => {
|
||||||
if (
|
if (evt.type === "item_progress") {
|
||||||
evt.type === "item_progress" &&
|
const classname = String(evt.classname ?? "");
|
||||||
(evt.status === "done" || evt.status === "failed")
|
if (evt.status === "done") {
|
||||||
) {
|
onDone(classname);
|
||||||
onDone(String(evt.classname ?? ""));
|
} else if (evt.status === "failed") {
|
||||||
|
onFailed?.(classname, String(evt.error ?? ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (evt.type === "batch_complete") {
|
if (evt.type === "batch_complete") {
|
||||||
succeeded = Number(evt.succeeded ?? 0);
|
succeeded = Number(evt.succeeded ?? 0);
|
||||||
|
|||||||
Reference in new issue
Block a user