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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 5x 2x 2x 6x 2x 2x 5x 2x 2x 5x 2x 2x 5x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { socialDraftsApi, type CreateDraftInput } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
// ─── List ─────────────────────────────────────────────────────────────────────
export function useSocialDrafts(
proId: string | null,
options?: { refetchInterval?: number | false },
) {
return useQuery({
queryKey: queryKeys.socialDrafts.list(proId ?? ""),
queryFn: async () => {
const response = await socialDraftsApi.list(proId as string);
return response.data ?? { drafts: [], pendingCount: 0 };
},
enabled: !!proId,
staleTime: 10_000,
refetchInterval: options?.refetchInterval,
});
}
// ─── Detail ───────────────────────────────────────────────────────────────────
export function useSocialDraft(
proId: string | null,
draftId: string | null,
options?: { refetchInterval?: number | false },
) {
return useQuery({
queryKey: queryKeys.socialDrafts.detail(proId ?? "", draftId ?? ""),
queryFn: async () => {
const response = await socialDraftsApi.get(
proId as string,
draftId as string,
);
return response.data ?? null;
},
enabled: !!proId && !!draftId,
staleTime: 10_000,
refetchInterval: options?.refetchInterval,
});
}
// ─── Download URL ─────────────────────────────────────────────────────────────
export function useSocialDraftDownloadUrl(
proId: string | null,
draftId: string | null,
) {
return useQuery({
queryKey: queryKeys.socialDrafts.download(proId ?? "", draftId ?? ""),
queryFn: async () => {
const response = await socialDraftsApi.download(
proId as string,
draftId as string,
);
return response.data?.url ?? null;
},
enabled: !!proId && !!draftId,
// Signed URLs expire — don't cache for too long
staleTime: 4 * 60 * 1000, // 4 minutes
gcTime: 5 * 60 * 1000,
});
}
// ─── Music Tracks ─────────────────────────────────────────────────────────────
export function useMusicTracks(proId: string | null) {
return useQuery({
queryKey: queryKeys.socialDrafts.musicTracks(proId ?? ""),
queryFn: async () => {
const response = await socialDraftsApi.listMusicTracks(proId as string);
return response.data?.tracks ?? [];
},
enabled: !!proId,
staleTime: 60 * 60 * 1000, // 1 hour
});
}
// ─── Templates ────────────────────────────────────────────────────────────────
export function useTemplates(proId: string | null) {
return useQuery({
queryKey: queryKeys.socialDrafts.templates(proId ?? ""),
queryFn: async () => {
const response = await socialDraftsApi.listTemplates(proId as string);
return response.data?.templates ?? [];
},
enabled: !!proId,
staleTime: 60 * 60 * 1000, // 1 hour
});
}
// ─── Mutations ────────────────────────────────────────────────────────────────
export function useCreateDraft(proId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (input: CreateDraftInput) =>
socialDraftsApi.create(proId, input),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: queryKeys.socialDrafts.list(proId),
});
},
});
}
export function useRegenerateDraft(proId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (projectId: string) =>
socialDraftsApi.regenerate(proId, projectId),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: queryKeys.socialDrafts.list(proId),
});
},
});
}
export function useUpdateDraftCaption(proId: string, draftId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (captionEn: string) =>
socialDraftsApi.updateCaption(proId, draftId, captionEn),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: queryKeys.socialDrafts.detail(proId, draftId),
});
queryClient.invalidateQueries({
queryKey: queryKeys.socialDrafts.list(proId),
});
},
});
}
// ─── Voiceover Script Preview ────────────────────────────────────────────────
/**
* Fetch the auto-generated voiceover script preview for a project.
*
* Disabled by default — pass `enabled: true` from the caller when the pro
* toggles voiceover ON or clicks [Regenerate]. Script is deterministic per
* (project, pro) so 5-min staleTime is safe. No retries: if the endpoint
* fails, the UI falls back to an empty textarea with a placeholder, and the
* [Regenerate] button is always visible for manual retry.
*/
export function useVoiceoverPreviewScript(
proId: string | null,
projectId: string | null,
options?: { enabled?: boolean },
) {
return useQuery({
queryKey: queryKeys.socialDrafts.voiceoverPreviewScript(
proId ?? "",
projectId ?? "",
),
queryFn: async () => {
const response = await socialDraftsApi.getVoiceoverPreviewScript(
proId as string,
projectId as string,
);
return response.data?.script ?? "";
},
enabled: !!proId && !!projectId && (options?.enabled ?? false),
staleTime: 5 * 60 * 1000,
retry: false,
});
}
|