Spaces:
Running
Running
File size: 1,265 Bytes
a32aee9 f1fa34c a32aee9 f1fa34c a32aee9 f1fa34c a32aee9 f1fa34c a32aee9 f1fa34c a32aee9 f1fa34c a32aee9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import { useQuery } from "@tanstack/react-query";
import { useAuth } from "@/context/AuthContext";
import { getConfig, getKbStats, listDocuments } from "@/lib/api";
/**
* The user id is still part of every query KEY so switching accounts cannot
* surface the previous account's cached rows — but it is no longer passed to
* the API, which derives identity from the bearer token instead.
*/
function useScopeKey(): string {
const { session } = useAuth();
return session?.userId ?? "anonymous";
}
/** App config + model catalog (public, fetched once). */
export function useConfig() {
return useQuery({
queryKey: ["config"],
queryFn: getConfig,
staleTime: Infinity,
});
}
/** Documents in the authenticated user's knowledge base. */
export function useDocuments() {
const scope = useScopeKey();
const { isAuthenticated } = useAuth();
return useQuery({
queryKey: ["documents", scope],
queryFn: listDocuments,
enabled: isAuthenticated,
});
}
/** Knowledge-base stats (chunk + document counts). */
export function useKbStats() {
const scope = useScopeKey();
const { isAuthenticated } = useAuth();
return useQuery({
queryKey: ["kb-stats", scope],
queryFn: getKbStats,
enabled: isAuthenticated,
});
}
|