import { useQuery } from '@tanstack/react-query'; /** Active offering banner as served by `GET /api/v1/offerings/banner` (no planning fields). */ export interface OfferingBannerData { title: string; text: string; linkLabel: string | null; linkUrl: string | null; } /** Two places: `bottom` (default, big banner under the cards) and `top` (slim one-line bar above them). */ export type OfferingBannerPosition = 'top' | 'bottom'; function bannerEndpoint(position: OfferingBannerPosition) { const base = `${import.meta.env.VITE_API_BASE_URL ?? ''}/api/v1/offerings/banner`; // No parameter means `bottom` (backwards compatible with the first API version). return position === 'top' ? `${base}?position=top` : base; } /** 204 (no active banner) and any failure resolve to `null`: the banner is optional, never an error. */ async function fetchBanner(position: OfferingBannerPosition): Promise { try { const response = await fetch(bannerEndpoint(position)); if (response.status === 204 || !response.ok) { return null; } const data = (await response.json()) as Partial | null; // A top banner may have an empty title; text is always required. if (!data || typeof data.text !== 'string' || data.text === '') { return null; } return { title: typeof data.title === 'string' ? data.title : '', text: data.text, linkLabel: data.linkLabel ?? null, linkUrl: data.linkUrl ?? null, }; } catch { return null; } } export function useOfferingBannerQuery(position: OfferingBannerPosition = 'bottom') { return useQuery({ queryKey: ['offering-banner', position], queryFn: () => fetchBanner(position), // Short staleness so a change in the CMS shows up quickly (the API itself caches for 60 s). staleTime: 60_000, }); }