47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
import { useInfiniteQuery } from "@tanstack/react-query";
|
|
import { useSettingsStore } from "@/store/settings.store";
|
|
import { contentPresentationApi } from "../api/content-presentation.client";
|
|
import { contentPresentationQueryKeys } from "../api/content-presentation.query-keys";
|
|
import type { ContentSearchRequest } from "../types/presentation.types";
|
|
|
|
export type ContentSearchInfiniteRequest = Omit<ContentSearchRequest, "page">;
|
|
|
|
function hasModuleIdentity(request: ContentSearchInfiniteRequest): boolean {
|
|
return Boolean(request.module_id || request.module_code);
|
|
}
|
|
|
|
export function useContentSearchInfiniteQuery(
|
|
request: ContentSearchInfiniteRequest,
|
|
options?: { enabled?: boolean },
|
|
) {
|
|
const preview = useSettingsStore((state) => state.contentPreviewEnabled);
|
|
const enabled =
|
|
(options?.enabled ?? true) &&
|
|
hasModuleIdentity(request) &&
|
|
Boolean(request.template_code.trim());
|
|
|
|
return useInfiniteQuery({
|
|
queryKey: contentPresentationQueryKeys.search(request, preview),
|
|
initialPageParam: 1,
|
|
enabled,
|
|
queryFn: ({ pageParam, signal }) => {
|
|
// Axios 1.x cancellation is already propagated by TanStack Query only if
|
|
// your request wrapper accepts signal. The API call remains valid without it.
|
|
void signal;
|
|
return contentPresentationApi.searchContents(
|
|
{
|
|
...request,
|
|
page: pageParam,
|
|
},
|
|
preview,
|
|
);
|
|
},
|
|
getNextPageParam: (lastPage) => {
|
|
const loadedThrough = lastPage.page * lastPage.page_size;
|
|
return loadedThrough < lastPage.total ? lastPage.page + 1 : undefined;
|
|
},
|
|
staleTime: 2 * 60 * 1000,
|
|
retry: 2,
|
|
});
|
|
}
|