A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io
Rust ai-toolsclaude-codecodexdeepseek-harnessdesktop-appgrokgrokbuildhermeskimi-codemcpopen-sourceopencodepiprovider-managementrustskillsskills-managementtauriwsl-support
fix(skills): roll back only the failed cell when a Skill toggle fails
Jason52ef598b572e
diff --git a/src/hooks/useSkills.ts b/src/hooks/useSkills.tsindex 06b71913d..3b62fe853 100644--- a/src/hooks/useSkills.ts+++ b/src/hooks/useSkills.ts@@ -356,35 +356,46 @@ export function useToggleSkillApp() {app: AppId;enabled: boolean;}) => skillsApi.toggleApp(id, app, enabled),- // 乐观更新:点了格子立刻翻过来,不等写完再刷新;失败时回滚+ // 乐观更新:点了格子立刻翻过来,不等写完再刷新;失败时只回滚这一格。+ // 不能整份还原发起前的列表:几次开关可能同时在途(如发现页并发安装到多个应用),+ // 整份还原会把别人已成功的格子也翻回去。onMutate: async ({ id, app, enabled }) => {await queryClient.cancelQueries({ queryKey: ["skills", "installed"] });- const previous = queryClient.getQueryData<InstalledSkill[]>([- "skills",- "installed",- ]);- if (previous) {- queryClient.setQueryData<InstalledSkill[]>(- ["skills", "installed"],- previous.map((skill) =>- skill.id === id- ? { ...skill, apps: { ...skill.apps, [app]: enabled } }- : skill,- ),- );- }+ const skill = queryClient+ .getQueryData<InstalledSkill[]>(["skills", "installed"])+ ?.find((item) => item.id === id);+ const previous = skill+ ? Boolean((skill.apps as Partial<Record<AppId, boolean>>)[app])+ : undefined;+ setInstalledSkillApp(queryClient, id, app, enabled);return { previous };},- onError: (_error, _vars, context) => {- if (context?.previous) {- queryClient.setQueryData(["skills", "installed"], context.previous);+ onError: (_error, { id, app }, context) => {+ if (context?.previous !== undefined) {+ setInstalledSkillApp(queryClient, id, app, context.previous);}},- onSuccess: () =>+ // 失败也重新拉一次:后端已按自己的状态回滚,以它为准+ onSettled: () =>queryClient.invalidateQueries({ queryKey: ["skills", "installed"] }),});}+function setInstalledSkillApp(+ queryClient: QueryClient,+ id: string,+ app: AppId,+ enabled: boolean,+) {+ queryClient.setQueryData<InstalledSkill[]>(["skills", "installed"], (list) =>+ list?.map((skill) =>+ skill.id === id+ ? { ...skill, apps: { ...skill.apps, [app]: enabled } }+ : skill,+ ),+ );+}+/** Toggle multiple Skills serially because each operation writes app files. */export function useBulkToggleSkillApp() {const queryClient = useQueryClient();diff --git a/tests/hooks/useSkillsBulkToggle.test.tsx b/tests/hooks/useSkillsBulkToggle.test.tsxindex 1a2631247..23a8e3339 100644--- a/tests/hooks/useSkillsBulkToggle.test.tsx+++ b/tests/hooks/useSkillsBulkToggle.test.tsx@@ -11,6 +11,7 @@ import {useUpdateSkill,} from "@/hooks/useSkills";import type {+ InstalledSkill,SkillBackupEntry,SkillUpdateCheckResult,} from "@/lib/api/skills";@@ -123,6 +124,75 @@ describe("Skills management mutation hooks", () => {await waitFor(() => expect(result.current.isPending).toBe(false));});+ it("rolls back only the failed cell when two toggles overlap", async () => {+ const skill = (id: string): InstalledSkill => ({+ id,+ name: id,+ directory: id,+ apps: {+ claude: false,+ codex: false,+ gemini: false,+ opencode: false,+ openclaw: false,+ hermes: false,+ pi: false,+ },+ installedAt: 0,+ updatedAt: 0,+ });+ let rejectAlpha: ((error: Error) => void) | undefined;+ toggleAppMock+ .mockImplementationOnce(+ () =>+ new Promise((_resolve, reject) => {+ rejectAlpha = reject;+ }),+ )+ .mockResolvedValueOnce(undefined);+ const queryClient = new QueryClient({+ defaultOptions: { mutations: { retry: false } },+ });+ queryClient.setQueryData<InstalledSkill[]>(+ ["skills", "installed"],+ [skill("alpha"), skill("beta")],+ );+ const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");+ const { result } = renderHook(+ () => ({ first: useToggleSkillApp(), second: useToggleSkillApp() }),+ { wrapper: createWrapper(queryClient) },+ );++ let failing!: Promise<unknown>;+ act(() => {+ failing = result.current.first+ .mutateAsync({ id: "alpha", app: "claude", enabled: true })+ .catch((error: unknown) => error);+ });+ await waitFor(() => expect(toggleAppMock).toHaveBeenCalledTimes(1));+ await act(async () => {+ await result.current.second.mutateAsync({+ id: "beta",+ app: "claude",+ enabled: true,+ });+ });+ await act(async () => {+ rejectAlpha?.(new Error("conflict"));+ await failing;+ });++ const apps = queryClient+ .getQueryData<InstalledSkill[]>(["skills", "installed"])+ ?.map((item) => [item.id, item.apps.claude]);+ expect(apps).toEqual([+ ["alpha", false],+ ["beta", true],+ ]);+ // 失败的那次也要重新拉列表,以后端为准+ expect(invalidateSpy).toHaveBeenCalledTimes(2);+ });+it("keeps backup restore pending until installed skills and backups refresh", async () => {let releaseInvalidation: (() => void) | undefined;const invalidationPending = new Promise<void>((resolve) => {