created useGetAllFiltersQuery
This commit is contained in:
parent
425d5a61d0
commit
8ca0f84cd8
1 changed files with 36 additions and 0 deletions
36
frontend/app/api/queries/useGetAllFiltersQuery.ts
Normal file
36
frontend/app/api/queries/useGetAllFiltersQuery.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import {
|
||||
type UseQueryOptions,
|
||||
useQuery,
|
||||
useQueryClient,
|
||||
} from "@tanstack/react-query";
|
||||
import type { KnowledgeFilter } from "./useGetFiltersSearchQuery";
|
||||
|
||||
export const useGetAllFiltersQuery = (
|
||||
options?: Omit<UseQueryOptions<KnowledgeFilter[]>, "queryKey" | "queryFn">,
|
||||
) => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
async function getAllFilters(): Promise<KnowledgeFilter[]> {
|
||||
const response = await fetch("/api/knowledge-filter/search", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ query: "", limit: 1000 }), // Fetch all filters
|
||||
});
|
||||
|
||||
const json = await response.json();
|
||||
if (!response.ok || !json.success) {
|
||||
// ensure we always return a KnowledgeFilter[] to satisfy the return type
|
||||
return [];
|
||||
}
|
||||
return (json.filters || []) as KnowledgeFilter[];
|
||||
}
|
||||
|
||||
return useQuery<KnowledgeFilter[]>(
|
||||
{
|
||||
queryKey: ["knowledge-filters", "all"],
|
||||
queryFn: getAllFilters,
|
||||
...options,
|
||||
},
|
||||
queryClient,
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue