mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-08 23:32:40 +02:00
organize seach space apis
This commit is contained in:
parent
43362a383e
commit
78ab020d80
15 changed files with 154 additions and 42 deletions
34
surfsense_web/atoms/chats/chat-mutations.atom.ts
Normal file
34
surfsense_web/atoms/chats/chat-mutations.atom.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { atomWithMutation } from "jotai-tanstack-query";
|
||||
import { deleteChat } from "@/lib/apis/chats.api";
|
||||
import { activeSearchSpaceIdAtom } from "../seach-spaces/seach-space-queries.atom";
|
||||
import { queryClient } from "@/lib/query-client/client";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
import { toast } from "sonner";
|
||||
import { Chat } from "@/app/dashboard/[search_space_id]/chats/chats-client";
|
||||
|
||||
export const deleteChatMutationAtom = atomWithMutation((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
const authToken = localStorage.getItem("surfsense_bearer_token");
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.activeSearchSpace.chats(searchSpaceId ?? ""),
|
||||
enabled: !!searchSpaceId && !!authToken,
|
||||
mutationFn: async (chatId: number) => {
|
||||
if (!authToken) {
|
||||
throw new Error("No authentication token found");
|
||||
}
|
||||
if (!searchSpaceId) {
|
||||
throw new Error("No search space id found");
|
||||
}
|
||||
|
||||
return deleteChat(chatId, authToken);
|
||||
},
|
||||
|
||||
onSuccess: (_, chatId) => {
|
||||
toast.success("Chat deleted successfully");
|
||||
queryClient.setQueryData(cacheKeys.activeSearchSpace.chats(searchSpaceId!), (oldData: Chat[]) => {
|
||||
return oldData.filter((chat) => chat.id !== chatId);
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue