feat(web): useSearch infinite query + useDebouncedValue + MSW search handler
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+34
-1
@@ -1,4 +1,4 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { api } from "./client";
|
||||
import type { components } from "./schema";
|
||||
@@ -281,6 +281,39 @@ export function useCreateAuthority() {
|
||||
});
|
||||
}
|
||||
|
||||
const SEARCH_PAGE = 20;
|
||||
|
||||
export function useSearch(q: string, visibility: string | null) {
|
||||
const term = q.trim();
|
||||
|
||||
return useInfiniteQuery({
|
||||
queryKey: ["search", term, visibility],
|
||||
enabled: term.length > 0,
|
||||
initialPageParam: 0,
|
||||
queryFn: async ({ pageParam }) => {
|
||||
const { data, error } = await api.GET("/api/admin/search", {
|
||||
params: {
|
||||
query: {
|
||||
q: term,
|
||||
...(visibility ? { visibility } : {}),
|
||||
offset: pageParam,
|
||||
limit: SEARCH_PAGE,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (error || !data) throw new Error("search failed");
|
||||
|
||||
return data;
|
||||
},
|
||||
getNextPageParam: (lastPage, allPages) => {
|
||||
const loaded = allPages.reduce((n, page) => n + page.hits.length, 0);
|
||||
|
||||
return loaded < lastPage.estimated_total ? loaded : undefined;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
type Visibility = "draft" | "internal" | "public";
|
||||
|
||||
/** Error carrying the HTTP status so callers can branch 422-gate vs 409-illegal. */
|
||||
|
||||
Reference in New Issue
Block a user