All files / src/hooks/queries useBlogImageQueries.ts

100% Statements 8/8
90.9% Branches 10/11
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          4x       1x 1x                     8x     3x 1x     2x 2x            
import { useQuery } from "@tanstack/react-query";
import { proApi } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
 
export function useBlogImages(proId: string | null, blogId: string | null) {
	return useQuery({
		queryKey: queryKeys.blogImages.list(proId as string, blogId as string),
		queryFn: async () => {
			// Safe to assert as string here because enabled check ensures non-null
			const response = await proApi.getBlogImages(proId as string, blogId as string);
			return response.data || [];
		},
		enabled: !!proId && !!blogId,
	});
}
 
export function usePexelsSearch(
	proId: string | null,
	query: string,
	page = 1,
) {
	return useQuery({
		queryKey: queryKeys.blogImages.pexelsSearch(query, page),
		queryFn: async () => {
			if (!query || query.trim().length === 0) {
				return { photos: [], page: 1, per_page: 20, total_results: 0 };
			}
			// Safe to assert as string here because enabled check ensures non-null
			const response = await proApi.searchPexels(proId as string, query, page);
			return response.data;
		},
		enabled: !!proId && query.trim().length > 0,
		staleTime: 5 * 60 * 1000, // Cache for 5 minutes (Pexels rate limit)
	});
}