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>
96 lines
3.3 KiB
TypeScript
96 lines
3.3 KiB
TypeScript
import { atomWithMutation } from "jotai-tanstack-query";
|
|
import { toast } from "sonner";
|
|
import type {
|
|
CreateImageGenConfigRequest,
|
|
CreateImageGenConfigResponse,
|
|
DeleteImageGenConfigResponse,
|
|
GetImageGenConfigsResponse,
|
|
UpdateImageGenConfigRequest,
|
|
UpdateImageGenConfigResponse,
|
|
} from "@/contracts/types/new-llm-config.types";
|
|
import { imageGenConfigApiService } from "@/lib/apis/image-gen-config-api.service";
|
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
|
import { queryClient } from "@/lib/query-client/client";
|
|
import { activeSearchSpaceIdAtom } from "../search-spaces/search-space-query.atoms";
|
|
|
|
/**
|
|
* Mutation atom for creating a new ImageGenerationConfig
|
|
*/
|
|
export const createImageGenConfigMutationAtom = atomWithMutation((get) => {
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
|
|
|
return {
|
|
mutationKey: ["image-gen-configs", "create"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
enabled: !!searchSpaceId,
|
|
mutationFn: async (request: CreateImageGenConfigRequest) => {
|
|
return imageGenConfigApiService.createConfig(request);
|
|
},
|
|
onSuccess: (_: CreateImageGenConfigResponse, request: CreateImageGenConfigRequest) => {
|
|
toast.success(`${request.name} created`);
|
|
queryClient.invalidateQueries({
|
|
queryKey: cacheKeys.imageGenConfigs.all(Number(searchSpaceId)),
|
|
});
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to create image model");
|
|
},
|
|
};
|
|
});
|
|
|
|
/**
|
|
* Mutation atom for updating an existing ImageGenerationConfig
|
|
*/
|
|
export const updateImageGenConfigMutationAtom = atomWithMutation((get) => {
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
|
|
|
return {
|
|
mutationKey: ["image-gen-configs", "update"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
enabled: !!searchSpaceId,
|
|
mutationFn: async (request: UpdateImageGenConfigRequest) => {
|
|
return imageGenConfigApiService.updateConfig(request);
|
|
},
|
|
onSuccess: (_: UpdateImageGenConfigResponse, request: UpdateImageGenConfigRequest) => {
|
|
toast.success(`${request.data.name ?? "Configuration"} updated`);
|
|
queryClient.invalidateQueries({
|
|
queryKey: cacheKeys.imageGenConfigs.all(Number(searchSpaceId)),
|
|
});
|
|
queryClient.invalidateQueries({
|
|
queryKey: cacheKeys.imageGenConfigs.byId(request.id),
|
|
});
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to update image model");
|
|
},
|
|
};
|
|
});
|
|
|
|
/**
|
|
* Mutation atom for deleting an ImageGenerationConfig
|
|
*/
|
|
export const deleteImageGenConfigMutationAtom = atomWithMutation((get) => {
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
|
|
|
return {
|
|
mutationKey: ["image-gen-configs", "delete"],
|
|
meta: { suppressGlobalErrorToast: true },
|
|
enabled: !!searchSpaceId,
|
|
mutationFn: async (request: { id: number; name: string }) => {
|
|
return imageGenConfigApiService.deleteConfig(request.id);
|
|
},
|
|
onSuccess: (_: DeleteImageGenConfigResponse, request: { id: number; name: string }) => {
|
|
toast.success(`${request.name} deleted`);
|
|
queryClient.setQueryData(
|
|
cacheKeys.imageGenConfigs.all(Number(searchSpaceId)),
|
|
(oldData: GetImageGenConfigsResponse | undefined) => {
|
|
if (!oldData) return oldData;
|
|
return oldData.filter((config) => config.id !== request.id);
|
|
}
|
|
);
|
|
},
|
|
onError: (error: Error) => {
|
|
toast.error(error.message || "Failed to delete image model");
|
|
},
|
|
};
|
|
});
|