SurfSense/surfsense_web/atoms/chats/chat-mutation.atoms.ts

31 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-11-12 14:12:07 +02:00
import { atomWithMutation } from "jotai-tanstack-query";
2025-11-15 02:07:20 +02:00
import { toast } from "sonner";
import type { Chat } from "@/app/dashboard/[search_space_id]/chats/chats-client";
2025-11-18 11:35:06 +02:00
import { chatsApiService } from "@/lib/apis/chats-api.service";
2025-11-12 14:12:07 +02:00
import { cacheKeys } from "@/lib/query-client/cache-keys";
2025-11-15 02:07:20 +02:00
import { queryClient } from "@/lib/query-client/client";
import { activeSearchSpaceIdAtom } from "../seach-spaces/seach-space-queries.atom";
2025-11-12 14:12:07 +02:00
export const deleteChatMutationAtom = atomWithMutation((get) => {
2025-11-14 00:42:19 +02:00
const searchSpaceId = get(activeSearchSpaceIdAtom);
const authToken = localStorage.getItem("surfsense_bearer_token");
2025-11-12 14:12:07 +02:00
2025-11-14 00:42:19 +02:00
return {
mutationKey: cacheKeys.activeSearchSpace.chats(searchSpaceId ?? ""),
enabled: !!searchSpaceId && !!authToken,
mutationFn: async (chatId: number) => {
2025-11-18 11:35:06 +02:00
return chatsApiService.deleteChat({ id: chatId });
2025-11-14 00:42:19 +02:00
},
2025-11-12 14:12:07 +02:00
2025-11-14 00:42:19 +02:00
onSuccess: (_, chatId) => {
toast.success("Chat deleted successfully");
queryClient.setQueryData(
cacheKeys.activeSearchSpace.chats(searchSpaceId!),
(oldData: Chat[]) => {
return oldData.filter((chat) => chat.id !== chatId);
}
);
},
};
2025-11-12 14:12:07 +02:00
});