diff --git a/surfsense_web/atoms/logs/log-mutation.atoms.ts b/surfsense_web/atoms/logs/log-mutation.atoms.ts index 8ea27f818..e17b42fb6 100644 --- a/surfsense_web/atoms/logs/log-mutation.atoms.ts +++ b/surfsense_web/atoms/logs/log-mutation.atoms.ts @@ -1,10 +1,9 @@ import { atomWithMutation } from "jotai-tanstack-query"; -import { toast } from "sonner"; +import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms"; import type { - CreateLogRequest, - DeleteLogRequest, - UpdateLogRequest, - GetLogSummaryRequest, + CreateLogRequest, + DeleteLogRequest, + UpdateLogRequest, } from "@/contracts/types/log.types"; import { logsApiService } from "@/lib/apis/logs-api.service"; import { cacheKeys } from "@/lib/query-client/cache-keys"; @@ -13,36 +12,57 @@ import { queryClient } from "@/lib/query-client/client"; /** * Create Log Mutation */ -export const createLogMutationAtom = atomWithMutation(() => ({ - mutationKey: cacheKeys.logs.create(), - mutationFn: async (request: CreateLogRequest) => logsApiService.createLog(request), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: cacheKeys.logs.list() }); - }, -})); +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: () => { + queryClient.invalidateQueries({ queryKey: cacheKeys.logs.list(searchSpaceId ?? undefined) }); + queryClient.invalidateQueries({ + queryKey: cacheKeys.logs.summary(searchSpaceId ?? undefined), + }); + }, + }; +}); /** * Update Log Mutation */ -export const updateLogMutationAtom = atomWithMutation(() => ({ - mutationKey: cacheKeys.logs.update(), - mutationFn: async ({ logId, data }: { logId: number; data: UpdateLogRequest }) => - logsApiService.updateLog(logId, data), - onSuccess: (_data, variables) => { - queryClient.invalidateQueries({ queryKey: cacheKeys.logs.detail(variables.logId) }); - queryClient.invalidateQueries({ queryKey: cacheKeys.logs.list() }); - }, -})); +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: cacheKeys.logs.detail(variables.logId) }); + queryClient.invalidateQueries({ queryKey: cacheKeys.logs.list(searchSpaceId ?? undefined) }); + queryClient.invalidateQueries({ + queryKey: cacheKeys.logs.summary(searchSpaceId ?? undefined), + }); + }, + }; +}); /** * Delete Log Mutation */ -export const deleteLogMutationAtom = atomWithMutation(() => ({ - mutationKey: cacheKeys.logs.delete(), - mutationFn: async (request: DeleteLogRequest) => logsApiService.deleteLog(request), - onSuccess: (_data, request) => { - queryClient.invalidateQueries({ queryKey: cacheKeys.logs.list() }); - if (request?.id) queryClient.invalidateQueries({ queryKey: cacheKeys.logs.detail(request.id) }); - }, -})); - +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: cacheKeys.logs.list(searchSpaceId ?? undefined) }); + queryClient.invalidateQueries({ + queryKey: cacheKeys.logs.summary(searchSpaceId ?? undefined), + }); + if (request?.id) + queryClient.invalidateQueries({ queryKey: cacheKeys.logs.detail(request.id) }); + }, + }; +}); diff --git a/surfsense_web/lib/query-client/cache-keys.ts b/surfsense_web/lib/query-client/cache-keys.ts index 8e0f1431e..74eee19c5 100644 --- a/surfsense_web/lib/query-client/cache-keys.ts +++ b/surfsense_web/lib/query-client/cache-keys.ts @@ -18,6 +18,11 @@ export const cacheKeys = { typeCounts: (searchSpaceId?: string) => ["documents", "type-counts", searchSpaceId] as const, byChunk: (chunkId: string) => ["documents", "by-chunk", chunkId] as const, }, + logs: { + list: (searchSpaceId?: number | string) => ["logs", "list", searchSpaceId] as const, + detail: (logId: number | string) => ["logs", "detail", logId] as const, + summary: (searchSpaceId?: number | string) => ["logs", "summary", searchSpaceId] as const, + }, newLLMConfigs: { all: (searchSpaceId: number) => ["new-llm-configs", searchSpaceId] as const, byId: (configId: number) => ["new-llm-configs", "detail", configId] as const,