mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-19 18:45:15 +02:00
Closes #1371. Retarget of #1385 onto dev per maintainer request. surfsense_web/lib/query-client/client.ts configures a global MutationCache.onError that shows an error toast for every failed mutation unless meta.suppressGlobalErrorToast is set. The opt-out hook existed in the consumer but had zero producers — every mutation atom that already had its own onError: toast.error(...) was double-toasting on failure. Add meta: { suppressGlobalErrorToast: true } to the 30 mutations across 9 atom files that own their own error toast: - atoms/prompts/prompts-mutation.atoms.ts (4) - atoms/invites/invites-mutation.atoms.ts (4) - atoms/chat-comments/comments-mutation.atoms.ts (4) - atoms/new-llm-config/new-llm-config-mutation.atoms.ts (4) - atoms/members/members-mutation.atoms.ts (3) - atoms/roles/roles-mutation.atoms.ts (3) - atoms/image-gen-config/image-gen-config-mutation.atoms.ts (3) - atoms/vision-llm-config/vision-llm-config-mutation.atoms.ts (3) - atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms.ts (2) Atoms intentionally left alone (no local onError, rely on global): auth, user, search-spaces, logs, documents, connectors. Local validation (against dev): pnpm biome check on the 9 touched files is clean; tsc --noEmit shows no new errors in the touched files (pre-existing errors elsewhere unrelated). Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
import { atomWithMutation } from "jotai-tanstack-query";
|
|
import { toast } from "sonner";
|
|
import type {
|
|
PromptCreateRequest,
|
|
PromptRead,
|
|
PromptUpdateRequest,
|
|
} from "@/contracts/types/prompts.types";
|
|
import { promptsApiService } from "@/lib/apis/prompts-api.service";
|
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
|
import { queryClient } from "@/lib/query-client/client";
|
|
|
|
export const createPromptMutationAtom = atomWithMutation(() => ({
|
|
mutationKey: ["prompts", "create"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
mutationFn: async (request: PromptCreateRequest) => {
|
|
return promptsApiService.create(request);
|
|
},
|
|
onSuccess: () => {
|
|
toast.success("Prompt created");
|
|
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.all() });
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to create prompt");
|
|
},
|
|
}));
|
|
|
|
export const updatePromptMutationAtom = atomWithMutation(() => ({
|
|
mutationKey: ["prompts", "update"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
mutationFn: async ({ id, ...data }: PromptUpdateRequest & { id: number }) => {
|
|
return promptsApiService.update(id, data);
|
|
},
|
|
onSuccess: () => {
|
|
toast.success("Prompt updated");
|
|
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.all() });
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to update prompt");
|
|
},
|
|
}));
|
|
|
|
export const deletePromptMutationAtom = atomWithMutation(() => ({
|
|
mutationKey: ["prompts", "delete"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
mutationFn: async (id: number) => {
|
|
return promptsApiService.delete(id);
|
|
},
|
|
onSuccess: (_: unknown, id: number) => {
|
|
toast.success("Prompt deleted");
|
|
queryClient.setQueryData(cacheKeys.prompts.all(), (old: PromptRead[] | undefined) => {
|
|
if (!old) return old;
|
|
return old.filter((p) => p.id !== id);
|
|
});
|
|
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.public() });
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to delete prompt");
|
|
},
|
|
}));
|
|
|
|
export const copyPromptMutationAtom = atomWithMutation(() => ({
|
|
mutationKey: ["prompts", "copy"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
mutationFn: async (promptId: number) => {
|
|
return promptsApiService.copy(promptId);
|
|
},
|
|
onSuccess: () => {
|
|
toast.success("Prompt added to your collection");
|
|
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.all() });
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to copy prompt");
|
|
},
|
|
}));
|