organize seach space apis

This commit is contained in:
thierryverse 2025-11-12 16:22:30 +02:00
parent 43362a383e
commit 78ab020d80
15 changed files with 154 additions and 42 deletions

View file

@ -1,6 +1,6 @@
import { atomWithMutation } from "jotai-tanstack-query";
import { deleteChat } from "@/lib/apis/chat-apis";
import { activeSearchSpaceIdAtom } from "../../seach-spaces/active-seach-space.atom";
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";

View file

@ -2,9 +2,13 @@ import { atom } from "jotai";
import { atomWithQuery } from "jotai-tanstack-query";
import type { ChatDetails } from "@/app/dashboard/[search_space_id]/chats/chats-client";
import type { PodcastItem } from "@/app/dashboard/[search_space_id]/podcasts/podcasts-client";
import { fetchChatDetails } from "@/lib/apis/chat-apis";
import { getPodcastByChatId } from "@/lib/apis/podcast-apis";
import {
fetchChatDetails,
fetchChatsBySearchSpace,
} from "@/lib/apis/chats.api";
import { getPodcastByChatId } from "@/lib/apis/podcasts.api";
import { cacheKeys } from "@/lib/query-client/cache-keys";
import { activeSearchSpaceIdAtom } from "@/atoms/seach-spaces/seach-space-queries.atom";
type ActiveChatState = {
chatId: string | null;
@ -38,3 +42,23 @@ export const activeChatAtom = atomWithQuery<ActiveChatState>((get) => {
},
};
});
export const activeSearchSpaceChatsAtom = atomWithQuery((get) => {
const searchSpaceId = get(activeSearchSpaceIdAtom);
const authToken = localStorage.getItem("surfsense_bearer_token");
return {
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);
},
};
});

View file

@ -1,24 +0,0 @@
import { atomWithQuery } from "jotai-tanstack-query";
import { fetchChatsBySearchSpace } from "@/lib/apis/chat-apis";
import { activeSearchSpaceIdAtom } from "../../seach-spaces/active-seach-space.atom";
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 {
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);
},
};
});

View file

@ -1,3 +1,3 @@
import { atom } from "jotai";
export const activeSearchSpaceIdAtom = atom<string | null>(null);
export const activeSearchSpaceIdAtom = atom<string | null>(null);