All files / src/hooks/queries useProjectQueries.ts

100% Statements 9/9
100% Branches 12/12
100% Functions 6/6
100% Lines 9/9

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 37 38 39 40 41 42 43 44 45 46 47 48            328x     74x 11x                       5x     2x     2x             4x     1x       1x          
import { useQuery } from "@tanstack/react-query";
import { projectsApi } from "../../lib/api/pro/projects";
import { proApi } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
 
export function useProjects(proId: string | null) {
	return useQuery({
		queryKey: queryKeys.projects.list(proId ?? ""),
		queryFn: async () => {
			const response = await proApi.listProjects(proId as string);
			return response.data || [];
		},
		enabled: !!proId,
	});
}
 
/**
 * Like useProjects but fetches with ?includePhotoCount=1.
 * Uses a distinct query key so it doesn't poison the plain list cache.
 * F-008 client.
 */
export function useProjectsWithPhotoCount(proId: string | null) {
	return useQuery({
		queryKey: [...queryKeys.projects.list(proId ?? ""), "withPhotoCount"] as const,
		queryFn: async () => {
			const response = await projectsApi.list(proId as string, undefined, {
				includePhotoCount: true,
			});
			return response.data || [];
		},
		enabled: !!proId,
	});
}
 
export function useProject(proId: string | null, projectId: string) {
	return useQuery({
		queryKey: queryKeys.projects.detail(proId ?? "", projectId),
		queryFn: async () => {
			const response = await proApi.getProject(
				proId as string,
				projectId,
			);
			return response.data;
		},
		enabled: !!proId && !!projectId,
	});
}