Files
slp-modular-cms/frontend/src/features/hero/services/hooks.ts
T

86 lines
3.6 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { api } from '@/lib/api-client';
import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest, HeroUploadLimits } from './types';
export const HERO_KEY = ['hero'];
/** The hero as the admin edits it (also the image variants of every slide). */
export function useHero() {
return useQuery<HeroAdminDto, Error>({
queryKey: HERO_KEY,
queryFn: () => api.get<HeroAdminDto>('/api/v1/hero/all'),
// Images are cut in the background after an upload or a saved focus change: look again until they are all ready.
refetchInterval: (query) => (query.state.data?.slides.some((s) => s.image.processing) ? 5000 : false),
// Also while the tab is in the background or the connection came back: the state must never stay stale.
refetchIntervalInBackground: true,
});
}
export function useSaveHero() {
const queryClient = useQueryClient();
return useMutation<HeroAdminDto, Error, HeroSaveRequest>({
mutationFn: (body) => api.put<HeroAdminDto>('/api/v1/hero', body),
onSuccess: (hero) => {
queryClient.setQueryData(HERO_KEY, hero);
},
});
}
/** Uploads one image file; the server answers with the image and its variants. */
export function useUploadHeroImage() {
return useMutation<HeroImageDto, Error, File>({
mutationFn: (file) => {
const form = new FormData();
form.append('file', file);
return api.post<HeroImageDto>('/api/v1/hero/images', form);
},
});
}
export function useDeleteHeroImage() {
return useMutation<void, Error, string>({
mutationFn: (imageId) => api.delete<void>(`/api/v1/hero/images/${imageId}`),
});
}
/** Asks the server to try an image again that the background renderer gave up on. */
export function useRetryHeroImage() {
const queryClient = useQueryClient();
return useMutation<void, Error, string>({
mutationFn: (imageId) => api.post<void>(`/api/v1/hero/images/${imageId}/retry`, {}),
onSuccess: () => queryClient.invalidateQueries({ queryKey: HERO_KEY }),
});
}
/** The formats and limits an upload has to meet on this host. */
export function useHeroUploadLimits() {
return useQuery<HeroUploadLimits, Error>({
queryKey: ['hero', 'upload-limits'],
queryFn: () => api.get<HeroUploadLimits>('/api/v1/hero/images/limits'),
staleTime: 10 * 60 * 1000,
});
}
/** The query of one image: the page asks it for photos that were uploaded but not saved yet, until they are ready. */
export const heroImageQuery = (imageId: string) => ({
queryKey: ['hero', 'image', imageId],
queryFn: () => api.get<HeroImageDto>(`/api/v1/hero/images/${imageId}`),
refetchInterval: (query: { state: { data?: HeroImageDto } }) => (query.state.data?.processing ? 5000 : false),
refetchIntervalInBackground: true,
});
/**
* A new crop for an image, applied at once without saving the hero (Bryan, 2026-11-17): the server cuts the new variants
* in the background and the image switches to them when they are complete. The answer is the image, now processing.
*/
export function useSetHeroImageFocus() {
const queryClient = useQueryClient();
return useMutation<HeroImageDto, Error, { imageId: string; request: HeroFocusRequest }>({
mutationFn: ({ imageId, request }) => api.put<HeroImageDto>(`/api/v1/hero/images/${imageId}/focus`, request),
onSuccess: (image) => {
queryClient.setQueryData(['hero', 'image', image.id], image);
return queryClient.invalidateQueries({ queryKey: HERO_KEY });
},
});
}