AbiirSkincare/Hydrogen/web/app/lib/homeHero.ts

171 lines
4.8 KiB
TypeScript

import type {QueryResponseInitial} from '@sanity/react-loader';
import type {SanityContext} from 'hydrogen-sanity';
/** GROQ: ABiiR homepage hero on the `home` singleton (`_id == "home"`). */
export const HOME_HERO_GROQ = `*[_type == "home" && _id == "home"][0].hero{
eyebrow,
headlineLine1Prefix,
headlineLine1Italic,
headlineLine2Prefix,
headlineLine2Accent,
body,
primaryCtaLabel,
primaryCtaHref,
secondaryCtaLabel,
secondaryCtaHref,
gradientStart,
gradientEnd,
accentBurgundy,
marqueeText,
image{
alt,
asset->{
_id,
url,
metadata{dimensions{width,height}}
}
}
}` as const;
export type AbiirHomeHeroCms = {
eyebrow: string;
headlineLine1Prefix: string;
headlineLine1Italic: string;
headlineLine2Prefix: string;
headlineLine2Accent: string;
body: string;
primaryCtaLabel: string;
primaryCtaHref: string;
secondaryCtaLabel: string;
secondaryCtaHref: string;
gradientStart: string;
gradientEnd: string;
accentBurgundy: string;
marqueeText: string;
image: {
id?: string;
url: string;
alt: string;
width?: number;
height?: number;
} | null;
};
type SanityHeroPayload = {
eyebrow?: string | null;
headlineLine1Prefix?: string | null;
headlineLine1Italic?: string | null;
headlineLine2Prefix?: string | null;
headlineLine2Accent?: string | null;
body?: string | null;
primaryCtaLabel?: string | null;
primaryCtaHref?: string | null;
secondaryCtaLabel?: string | null;
secondaryCtaHref?: string | null;
gradientStart?: string | null;
gradientEnd?: string | null;
accentBurgundy?: string | null;
marqueeText?: string | null;
image?: {
alt?: string | null;
asset?: {
_id?: string;
url?: string | null;
metadata?: {dimensions?: {width?: number | null; height?: number | null}} | null;
} | null;
} | null;
} | null;
/** Map legacy catalog URLs to the unified all-products collection route. */
function normalizePrimaryCtaHref(href: string): string {
if (href === '/products' || href === '/products/') {
return '/collections/all';
}
return href;
}
/** Pixel-matched defaults from `React/src/app/components/Hero.tsx`. */
export const MOCK_HOME_HERO_CMS: AbiirHomeHeroCms = {
eyebrow: 'Ayurvedic Luxury · Spring 2026',
headlineLine1Prefix: 'Skincare ',
headlineLine1Italic: 'Rooted',
headlineLine2Prefix: 'in ',
headlineLine2Accent: 'Ayurveda',
body: 'Formulated with rare Ayurvedic botanicals and cutting-edge actives, ABiiR transforms your skincare ritual into a moment of pure elegance.',
primaryCtaLabel: 'Shop Collection',
primaryCtaHref: '/collections/all',
secondaryCtaLabel: 'Our Story',
secondaryCtaHref: '/about',
gradientStart: '#815049',
gradientEnd: '#CD977B',
accentBurgundy: '#3c0617',
marqueeText:
'Ayurvedic Skincare · Ancient Wisdom · Modern Science · Cruelty Free · Rare Botanicals · Dermatologist Tested ·',
image: null,
};
export function normalizeHomeHeroSanityPayload(
raw: unknown,
): AbiirHomeHeroCms | null {
if (raw == null || typeof raw !== 'object') return null;
const h = raw as SanityHeroPayload;
if (
!h?.eyebrow ||
!h?.body ||
!h?.primaryCtaLabel ||
!h?.primaryCtaHref ||
!h?.secondaryCtaLabel ||
!h?.secondaryCtaHref ||
!h?.marqueeText
) {
return null;
}
const url = h.image?.asset?.url;
const image =
url != null
? {
id: h.image?.asset?._id,
url,
alt: h.image?.alt ?? 'Hero image',
width: h.image?.asset?.metadata?.dimensions?.width ?? undefined,
height: h.image?.asset?.metadata?.dimensions?.height ?? undefined,
}
: null;
return {
eyebrow: h.eyebrow,
headlineLine1Prefix: h.headlineLine1Prefix ?? 'Skincare ',
headlineLine1Italic: h.headlineLine1Italic ?? 'Rooted',
headlineLine2Prefix: h.headlineLine2Prefix ?? 'in ',
headlineLine2Accent: h.headlineLine2Accent ?? 'Ayurveda',
body: h.body,
primaryCtaLabel: h.primaryCtaLabel,
primaryCtaHref: normalizePrimaryCtaHref(h.primaryCtaHref),
secondaryCtaLabel: h.secondaryCtaLabel,
secondaryCtaHref: h.secondaryCtaHref,
gradientStart: h.gradientStart ?? '#815049',
gradientEnd: h.gradientEnd ?? '#CD977B',
accentBurgundy: h.accentBurgundy ?? '#3c0617',
marqueeText: h.marqueeText,
image,
};
}
export async function loadHomeHeroFromSanity(args: {
sanity: SanityContext;
}): Promise<{homeHeroCms: AbiirHomeHeroCms}> {
const initial: QueryResponseInitial<SanityHeroPayload> = await args.sanity
.loadQuery<SanityHeroPayload>(HOME_HERO_GROQ)
.catch(
(): QueryResponseInitial<SanityHeroPayload> => ({
data: null,
sourceMap: undefined,
}),
);
const normalized = normalizeHomeHeroSanityPayload(initial.data);
return {
homeHeroCms: normalized ?? MOCK_HOME_HERO_CMS,
};
}