Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Discard skill import results after switching bots

vincentce651c0c457c
diff --git a/src/components/bot-settings/SkillsSection.tsx b/src/components/bot-settings/SkillsSection.tsx
index feeb03f798..7c53f893e9 100644
--- a/src/components/bot-settings/SkillsSection.tsx
+++ b/src/components/bot-settings/SkillsSection.tsx
@@ -325,13 +325,21 @@ export function useSkillImport(botId: string, { refresh, setError }: Pick<Return
const [importing, setImporting] = useState<"url" | "folder" | null>(null);
const [imported, setImported] = useState<string[] | null>(null);
+ const selection = useRef(botId);
+ useEffect(() => {
+ selection.current = botId;
+ setImporting(null);
+ setImported(null);
+ setSource("");
+ return () => { selection.current = ""; };
+ }, [botId]);
const submit = async (from: "url" | "folder") => {
const trimmed = source.trim();
if (importing || (from === "folder" ? !pick : !trimmed)) return;
setImporting(from);
try {
const picked = from === "folder" ? await pick?.() : null;
- if (from === "folder" && !picked) return;
+ if (selection.current !== botId || (from === "folder" && !picked)) return;
setError("");
setImported(null);
if (picked && "error" in picked) throw new Error(picked.error);
@@ -341,6 +349,7 @@ export function useSkillImport(botId: string, { refresh, setError }: Pick<Return
...(picked ? { redirect: "error" } : {}),
body: JSON.stringify(picked ? { files: picked.files } : { source: trimmed }),
});
+ if (selection.current !== botId) return;
const summaries = result.imported ?? [];
setImported(skillImportLines(picked ? summaries.map((summary) => ({
...summary,
@@ -348,11 +357,11 @@ export function useSkillImport(botId: string, { refresh, setError }: Pick<Return
skippedCount: picked.skippedCount + summary.skippedCount,
})) : summaries));
if (!picked) setSource("");
- await refresh();
+ await refresh(() => selection.current !== botId);
} catch (cause) {
- setError(cause instanceof Error && cause.message ? cause.message : failure);
+ if (selection.current === botId) setError(cause instanceof Error && cause.message ? cause.message : failure);
} finally {
- setImporting(null);
+ if (selection.current === botId) setImporting(null);
}
};
diff --git a/src/components/bot-settings/skill-import.dom.test.ts b/src/components/bot-settings/skill-import.dom.test.ts
new file mode 100644
index 0000000000..97c37b09e1
--- /dev/null
+++ b/src/components/bot-settings/skill-import.dom.test.ts
@@ -0,0 +1,34 @@
+// @vitest-environment happy-dom
+import { act, createElement } from "react";
+import { createRoot } from "react-dom/client";
+import { expect, it, vi } from "vitest";
+import { useSkillImport } from "./SkillsSection";
+
+const request = vi.hoisted(() => vi.fn());
+vi.mock("./BotEditorContext", () => ({ useBotEditor: () => ({ request }) }));
+
+it("discards a pending import when the selected bot changes", async () => {
+ const refresh = vi.fn(), setError = vi.fn();
+ const root = createRoot(document.createElement("div"));
+ let hook!: ReturnType<typeof useSkillImport>;
+ function Probe({ id }: { id: string }) {
+ hook = useSkillImport(id, { refresh, setError }, "Import failed");
+ return null;
+ }
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ try {
+ await act(async () => root.render(createElement(Probe, { id: "first" })));
+ await act(async () => hook.setSource("owner/repo"));
+ let finish!: () => void;
+ request.mockImplementation(() => new Promise(resolve => { finish = () => resolve({ imported: [] }); }));
+ let pending!: Promise<void>;
+ await act(async () => { pending = hook.submit("url"); });
+ await act(async () => root.render(createElement(Probe, { id: "second" })));
+ await act(async () => { finish(); await pending; });
+ expect(refresh).not.toHaveBeenCalled();
+ expect(hook.imported).toBeNull();
+ } finally {
+ await act(async () => root.unmount());
+ vi.unstubAllGlobals();
+ }
+});