All files / src/lib/loaders social-studio-loaders.ts

100% Statements 24/24
75% Branches 12/16
100% Functions 13/13
100% Lines 20/20

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                      4x 4x 3x     2x   2x                   3x 3x 2x     1x                       8x 8x 7x           1x   1x     7x     1x   1x     7x     1x   1x        
import { queryClient } from "../query-client";
import { queryKeys } from "../query-keys";
import { socialDraftsApi } from "../api";
import { projectsApi } from "../api/pro/projects";
import { getCachedProId, STALE_5MIN, STALE_1HR } from "./shared";
 
/**
 * Prefetch the social drafts list for the current pro.
 * Called by the `/social-studio` route loader.
 */
export function prefetchSocialDrafts() {
	const proId = getCachedProId();
	if (!proId) return;
	queryClient.ensureQueryData({
		queryKey: queryKeys.socialDrafts.list(proId),
		queryFn: () =>
			socialDraftsApi
				.list(proId)
				.then((r) => r.data ?? { drafts: [], pendingCount: 0 }),
		staleTime: 10_000,
	});
}
 
/**
 * Prefetch a single social draft by ID.
 * Called by the `/social-studio/$draftId` route loader.
 */
export function prefetchSocialDraft(draftId: string) {
	const proId = getCachedProId();
	if (!proId) return;
	queryClient.ensureQueryData({
		queryKey: queryKeys.socialDrafts.detail(proId, draftId),
		queryFn: () =>
			socialDraftsApi.get(proId, draftId).then((r) => r.data ?? null),
		staleTime: 10_000,
	});
}
 
/**
 * Prefetch data required by the create-reel page:
 * - Projects list with photo counts (for the photo selector)
 * - Music tracks (for MusicPicker)
 * - Templates (for TemplateChooser)
 */
export function prefetchCreateReel() {
	const proId = getCachedProId();
	if (!proId) return;
	queryClient.ensureQueryData({
		queryKey: [
			...queryKeys.projects.list(proId),
			"withPhotoCount",
		] as const,
		queryFn: () =>
			projectsApi
				.list(proId, undefined, { includePhotoCount: true })
				.then((r) => r.data || []),
		staleTime: STALE_5MIN,
	});
	queryClient.ensureQueryData({
		queryKey: queryKeys.socialDrafts.musicTracks(proId),
		queryFn: () =>
			socialDraftsApi
				.listMusicTracks(proId)
				.then((r) => r.data?.tracks ?? []),
		staleTime: STALE_1HR,
	});
	queryClient.ensureQueryData({
		queryKey: queryKeys.socialDrafts.templates(proId),
		queryFn: () =>
			socialDraftsApi
				.listTemplates(proId)
				.then((r) => r.data?.templates ?? []),
		staleTime: STALE_1HR,
	});
}