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";
|
2025-11-19 08:59:33 +02:00
|
|
|
import type {
|
2025-12-19 03:53:40 +05:30
|
|
|
ChatSummary,
|
2025-11-19 08:59:33 +02:00
|
|
|
CreateChatRequest,
|
|
|
|
|
DeleteChatRequest,
|
|
|
|
|
UpdateChatRequest,
|
|
|
|
|
} from "@/contracts/types/chat.types";
|
2025-11-18 11:35:06 +02:00
|
|
|
import { chatsApiService } from "@/lib/apis/chats-api.service";
|
2025-12-02 01:24:09 -08:00
|
|
|
import { getBearerToken } from "@/lib/auth-utils";
|
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";
|
2025-12-11 21:19:29 +00:00
|
|
|
import { activeSearchSpaceIdAtom } from "../search-spaces/search-space-query.atoms";
|
2025-11-18 22:12:47 +02:00
|
|
|
import { globalChatsQueryParamsAtom } from "./ui.atoms";
|
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);
|
2025-12-02 01:24:09 -08:00
|
|
|
const authToken = getBearerToken();
|
2025-11-18 22:12:47 +02:00
|
|
|
const chatsQueryParams = get(globalChatsQueryParamsAtom);
|
2025-11-12 14:12:07 +02:00
|
|
|
|
2025-11-14 00:42:19 +02:00
|
|
|
return {
|
2025-11-18 22:12:47 +02:00
|
|
|
mutationKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
2025-11-14 00:42:19 +02:00
|
|
|
enabled: !!searchSpaceId && !!authToken,
|
2025-11-18 19:32:24 +02:00
|
|
|
mutationFn: async (request: DeleteChatRequest) => {
|
|
|
|
|
return chatsApiService.deleteChat(request);
|
2025-11-14 00:42:19 +02:00
|
|
|
},
|
2025-11-12 14:12:07 +02:00
|
|
|
|
2025-11-18 19:32:24 +02:00
|
|
|
onSuccess: (_, request: DeleteChatRequest) => {
|
2025-11-14 00:42:19 +02:00
|
|
|
toast.success("Chat deleted successfully");
|
2025-12-19 22:37:04 +05:30
|
|
|
// Optimistically update the current query
|
2025-11-14 00:42:19 +02:00
|
|
|
queryClient.setQueryData(
|
2025-11-18 22:12:47 +02:00
|
|
|
cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
2025-12-19 03:53:40 +05:30
|
|
|
(oldData: ChatSummary[]) => {
|
2025-12-19 22:37:04 +05:30
|
|
|
return oldData?.filter((chat) => chat.id !== request.id) ?? [];
|
2025-11-14 00:42:19 +02:00
|
|
|
}
|
|
|
|
|
);
|
2025-12-19 22:37:04 +05:30
|
|
|
// Invalidate all chat queries to ensure consistency across components
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: ["chats"],
|
|
|
|
|
});
|
|
|
|
|
// Also invalidate the "all-chats" query used by AllChatsSidebar
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: ["all-chats"],
|
|
|
|
|
});
|
2025-11-14 00:42:19 +02:00
|
|
|
},
|
|
|
|
|
};
|
2025-11-12 14:12:07 +02:00
|
|
|
});
|
2025-11-19 08:59:33 +02:00
|
|
|
|
|
|
|
|
export const createChatMutationAtom = atomWithMutation((get) => {
|
|
|
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
2025-12-02 01:24:09 -08:00
|
|
|
const authToken = getBearerToken();
|
2025-11-19 08:59:33 +02:00
|
|
|
const chatsQueryParams = get(globalChatsQueryParamsAtom);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
mutationKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
|
|
|
|
enabled: !!searchSpaceId && !!authToken,
|
|
|
|
|
mutationFn: async (request: CreateChatRequest) => {
|
|
|
|
|
return chatsApiService.createChat(request);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onSuccess: () => {
|
2025-12-19 22:37:04 +05:30
|
|
|
// Invalidate ALL chat queries to ensure sidebar and other components refresh
|
|
|
|
|
// Using a partial key match to avoid stale closure issues with specific query params
|
2025-11-19 08:59:33 +02:00
|
|
|
queryClient.invalidateQueries({
|
2025-12-19 22:37:04 +05:30
|
|
|
queryKey: ["chats"],
|
|
|
|
|
});
|
|
|
|
|
// Also invalidate the "all-chats" query used by AllChatsSidebar
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: ["all-chats"],
|
2025-11-19 08:59:33 +02:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const updateChatMutationAtom = atomWithMutation((get) => {
|
|
|
|
|
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
2025-12-02 01:24:09 -08:00
|
|
|
const authToken = getBearerToken();
|
2025-11-19 08:59:33 +02:00
|
|
|
const chatsQueryParams = get(globalChatsQueryParamsAtom);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
mutationKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
|
|
|
|
enabled: !!searchSpaceId && !!authToken,
|
|
|
|
|
mutationFn: async (request: UpdateChatRequest) => {
|
|
|
|
|
return chatsApiService.updateChat(request);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: cacheKeys.chats.globalQueryParams(chatsQueryParams),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|