SurfSense/surfsense_web/atoms/chats/queries/active-search-space-chats.query.atom.ts

25 lines
832 B
TypeScript
Raw Normal View History

import { atomWithQuery } from "jotai-tanstack-query";
import { fetchChatsBySearchSpace } from "@/lib/apis/chat-apis";
2025-11-12 13:27:15 +02:00
import { activeSearchSpaceIdAtom } from "../../seach-spaces/active-seach-space.atom";
2025-11-12 14:12:07 +02:00
import { cacheKeys } from "@/lib/query-client/cache-keys";
export const activeSearchSpaceChatsAtom = atomWithQuery((get) => {
const searchSpaceId = get(activeSearchSpaceIdAtom);
const authToken = localStorage.getItem("surfsense_bearer_token");
return {
2025-11-12 14:12:07 +02:00
queryKey: cacheKeys.activeSearchSpace.chats(searchSpaceId ?? ""),
enabled: !!searchSpaceId && !!authToken,
queryFn: async () => {
if (!authToken) {
throw new Error("No authentication token found");
}
if (!searchSpaceId) {
throw new Error("No search space id found");
}
return fetchChatsBySearchSpace(searchSpaceId, authToken);
},
};
});