mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-19 18:45:15 +02:00
Fixes #1369 — log create/update/delete mutations did not invalidate the query keys that useLogs actually subscribes to, causing UI staleness. Replace narrow invalidations (list, summary) with prefix-level invalidation (["logs"]) to cover withQueryParams, list, summary and detail in one shot.
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { atomWithMutation } from "jotai-tanstack-query";
|
|
import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms";
|
|
import type {
|
|
CreateLogRequest,
|
|
DeleteLogRequest,
|
|
UpdateLogRequest,
|
|
} from "@/contracts/types/log.types";
|
|
import { logsApiService } from "@/lib/apis/logs-api.service";
|
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
|
import { queryClient } from "@/lib/query-client/client";
|
|
|
|
/**
|
|
* Create Log Mutation
|
|
*/
|
|
export const createLogMutationAtom = atomWithMutation((get) => {
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
|
return {
|
|
mutationKey: cacheKeys.logs.list(searchSpaceId ?? undefined),
|
|
enabled: !!searchSpaceId,
|
|
mutationFn: async (request: CreateLogRequest) => logsApiService.createLog(request),
|
|
onSuccess: () => {
|
|
// Invalidate all log-related queries (list, summary, detail, withQueryParams)
|
|
queryClient.invalidateQueries({ queryKey: ["logs"] });
|
|
},
|
|
};
|
|
});
|
|
|
|
/**
|
|
* Update Log Mutation
|
|
*/
|
|
export const updateLogMutationAtom = atomWithMutation((get) => {
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
|
return {
|
|
mutationKey: cacheKeys.logs.list(searchSpaceId ?? undefined),
|
|
enabled: !!searchSpaceId,
|
|
mutationFn: async ({ logId, data }: { logId: number; data: UpdateLogRequest }) =>
|
|
logsApiService.updateLog(logId, data),
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ["logs"] });
|
|
},
|
|
};
|
|
});
|
|
|
|
/**
|
|
* Delete Log Mutation
|
|
*/
|
|
export const deleteLogMutationAtom = atomWithMutation((get) => {
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
|
return {
|
|
mutationKey: cacheKeys.logs.list(searchSpaceId ?? undefined),
|
|
enabled: !!searchSpaceId,
|
|
mutationFn: async (request: DeleteLogRequest) => logsApiService.deleteLog(request),
|
|
onSuccess: (_data, request) => {
|
|
queryClient.invalidateQueries({ queryKey: ["logs"] });
|
|
},
|
|
};
|
|
});
|