All files / src/hooks/queries useProjectQueries.ts

100% Statements 13/13
100% Branches 21/21
100% Functions 8/8
100% Lines 13/13

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                  312x     64x 2x                       5x     2x     2x             4x     1x       1x                                                                                       189x 189x         72x 68x                                    
import { useQuery } from "@tanstack/react-query";
import { projectsApi } from "../../lib/api/pro/projects";
import { proApi } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
import { STALE_5MIN } from "../../lib/loaders/shared";
import type { Project } from "../../lib/api";
import type { ApiResponse } from "../../lib/api/base";
 
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,
	});
}
 
/** A project as the detail page needs it: the record plus its legacy photos. */
export type ProjectWithPhotos = Project & { photos: unknown[] };
 
/**
 * The project detail page's data, through the query cache.
 *
 * WHY THIS EXISTS
 * `ProjectDetailContainer` used raw `useState`/`useEffect` to load the
 * project — a direct violation of the React Query rule in
 * `apps/portal/CLAUDE.md`, and why every navigation into a project showed a
 * spinner even when the row had just been read on the list. The route already
 * has a loader (`prefetchProjectDetail`); it was warming a cache nothing read.
 *
 * WHY IT TAKES A FETCHER
 * The same container serves `/projects/$projectId` (pro) and
 * `/admin/projects/$projectId` (admin), and the two speak different
 * endpoints behind `ProjectApiAdapter`. Passing the adapter's `getProject` in
 * keeps the key, the staleTime and the unwrapping decided once, here, instead
 * of once per context.
 *
 * The pro-context `queryFn` returns exactly what `prefetchProjectDetail`
 * stores — `proApi.getProject(proId, projectId).then((r) => r.data)` — which
 * is the condition `apps/portal/CLAUDE.md` puts on a loader being any use at
 * all.
 */
export function useProjectDetail({
	proId,
	projectId,
	context,
	getProject,
}: {
	proId: string | null;
	projectId: string;
	context: "pro" | "admin";
	getProject: (
		projectId: string,
	) => Promise<ApiResponse<ProjectWithPhotos>>;
}) {
	const isAdmin = context === "admin";
	return useQuery({
		queryKey: isAdmin
			? queryKeys.projects.adminDetail(projectId)
			: queryKeys.projects.detail(proId ?? "", projectId),
		queryFn: async () => {
			const response = await getProject(projectId);
			return response.data ?? null;
		},
		// Admin has no proId to scope by; a pro does, and must not fetch before
		// `usePro()` has resolved one.
		enabled: Boolean(projectId) && (isAdmin || Boolean(proId)),
		staleTime: STALE_5MIN,
	});
}
 
/**
 * The publish bar moved out of this module.
 *
 * It lives in `hooks/queries/usePublishReadiness.ts` — one hook, one query
 * key, one copy of the types — because it is studio-scoped rather than
 * project-scoped and three separate hooks meant a publish invalidated one
 * reader's cache out of three. Import `usePublishReadiness`,
 * `PublishReadiness` and `PublishRequirement` from there.
 */