SurfSense/surfsense_web/atoms/documents/document-mutation.atoms.ts
Anish Sarkar a8c1fb660d feat(rename): complete searchSpace to workspace transition across frontend and backend
- Introduced a comprehensive specification for renaming `searchSpace` to `workspace` in `surfsense_web` and `surfsense_desktop`, ensuring all TypeScript identifiers, React props, and local data structures are updated.
- Implemented migration shims for persisted local state to prevent data loss during the transition.
- Updated observability metrics and IPC channels to reflect the new naming convention.
- Removed legacy `active-search-space` module and replaced it with `active-workspace` to maintain consistency.
- Ensured no behavioral changes or data loss for users during the renaming process.
2026-07-06 15:12:40 +05:30

108 lines
3.5 KiB
TypeScript

import { atomWithMutation } from "jotai-tanstack-query";
import { toast } from "sonner";
import { activeWorkspaceIdAtom } from "@/atoms/workspaces/workspace-query.atoms";
import type {
CreateDocumentRequest,
DeleteDocumentRequest,
GetDocumentsResponse,
UpdateDocumentRequest,
UploadDocumentRequest,
} from "@/contracts/types/document.types";
import { documentsApiService } from "@/lib/apis/documents-api.service";
import { cacheKeys } from "@/lib/query-client/cache-keys";
import { queryClient } from "@/lib/query-client/client";
import { globalDocumentsQueryParamsAtom } from "./ui.atoms";
export const createDocumentMutationAtom = atomWithMutation((get) => {
const workspaceId = get(activeWorkspaceIdAtom);
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
return {
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
enabled: !!workspaceId,
mutationFn: async (request: CreateDocumentRequest) => {
return documentsApiService.createDocument(request);
},
onSuccess: () => {
toast.success("Document created successfully");
queryClient.invalidateQueries({
queryKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
});
},
};
});
export const uploadDocumentMutationAtom = atomWithMutation((get) => {
const workspaceId = get(activeWorkspaceIdAtom);
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
return {
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
enabled: !!workspaceId,
mutationFn: async (request: UploadDocumentRequest) => {
return documentsApiService.uploadDocument(request);
},
onSuccess: () => {
// Note: Toast notification is handled by the caller (DocumentUploadTab) to use i18n
queryClient.invalidateQueries({
queryKey: cacheKeys.logs.summary(workspaceId ?? undefined),
});
},
};
});
export const updateDocumentMutationAtom = atomWithMutation((get) => {
const workspaceId = get(activeWorkspaceIdAtom);
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
return {
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
enabled: !!workspaceId,
mutationFn: async (request: UpdateDocumentRequest) => {
return documentsApiService.updateDocument(request);
},
onSuccess: (_, request: UpdateDocumentRequest) => {
toast.success("Document updated successfully");
queryClient.invalidateQueries({
queryKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
});
queryClient.invalidateQueries({
queryKey: cacheKeys.documents.document(String(request.id)),
});
},
};
});
export const deleteDocumentMutationAtom = atomWithMutation((get) => {
const workspaceId = get(activeWorkspaceIdAtom);
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
return {
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
enabled: !!workspaceId,
mutationFn: async (request: DeleteDocumentRequest) => {
return documentsApiService.deleteDocument(request);
},
onSuccess: (_, request: DeleteDocumentRequest) => {
// Note: Toast is handled by the caller (page.tsx onBulkDelete) to show count info
queryClient.setQueryData(
cacheKeys.documents.globalQueryParams(documentsQueryParams),
(oldData: GetDocumentsResponse | undefined) => {
if (!oldData) return oldData;
return {
...oldData,
items: oldData.items.filter((doc) => doc.id !== request.id),
total: oldData.total - 1,
};
}
);
queryClient.invalidateQueries({
queryKey: cacheKeys.documents.document(String(request.id)),
});
},
};
});