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({ queryKey: HERO_KEY, queryFn: () => api.get('/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({ mutationFn: (body) => api.put('/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({ mutationFn: (file) => { const form = new FormData(); form.append('file', file); return api.post('/api/v1/hero/images', form); }, }); } export function useDeleteHeroImage() { return useMutation({ mutationFn: (imageId) => api.delete(`/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({ mutationFn: (imageId) => api.post(`/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({ queryKey: ['hero', 'upload-limits'], queryFn: () => api.get('/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(`/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({ mutationFn: ({ imageId, request }) => api.put(`/api/v1/hero/images/${imageId}/focus`, request), onSuccess: (image) => { queryClient.setQueryData(['hero', 'image', image.id], image); return queryClient.invalidateQueries({ queryKey: HERO_KEY }); }, }); }