diff --git a/surfsense_web/atoms/snapshots/snapshots-query.atoms.ts b/surfsense_web/atoms/snapshots/snapshots-query.atoms.ts new file mode 100644 index 000000000..6c29dccc0 --- /dev/null +++ b/surfsense_web/atoms/snapshots/snapshots-query.atoms.ts @@ -0,0 +1,22 @@ +import { atomWithQuery } from "jotai-tanstack-query"; +import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms"; +import { chatThreadsApiService } from "@/lib/apis/chat-threads-api.service"; +import { cacheKeys } from "@/lib/query-client/cache-keys"; + +export const searchSpaceSnapshotsAtom = atomWithQuery((get) => { + const searchSpaceId = get(activeSearchSpaceIdAtom); + + return { + queryKey: cacheKeys.snapshots.bySearchSpace(Number(searchSpaceId) || 0), + enabled: !!searchSpaceId, + staleTime: 5 * 60 * 1000, + queryFn: async () => { + if (!searchSpaceId) { + return { snapshots: [] }; + } + return chatThreadsApiService.listSearchSpaceSnapshots({ + search_space_id: Number(searchSpaceId), + }); + }, + }; +}); diff --git a/surfsense_web/lib/query-client/cache-keys.ts b/surfsense_web/lib/query-client/cache-keys.ts index e6cf5610b..dcb5288f5 100644 --- a/surfsense_web/lib/query-client/cache-keys.ts +++ b/surfsense_web/lib/query-client/cache-keys.ts @@ -82,4 +82,7 @@ export const cacheKeys = { publicChat: { byToken: (shareToken: string) => ["public-chat", shareToken] as const, }, + snapshots: { + bySearchSpace: (searchSpaceId: number) => ["snapshots", "search-space", searchSpaceId] as const, + }, };