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

297 lines
8.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {CacheNone} from '@shopify/hydrogen';
import type {SanityContext} from 'hydrogen-sanity';
import {unwrapSanityQueryResult} from '~/lib/abiirProductDetail';
/**
* GROQ: `home` singleton → Spring Glow banner module.
* Also re-exported from `~/components/SpringGlowBanner.tsx` for co-location with the UI.
*/
/** Max inner width + horizontal padding (Spring Glow banner, store pages, etc.). */
export const ABIIR_WIDE_CONTENT_SHELL_CLASS =
'mx-auto w-full max-w-[1710px] px-6 sm:px-10 lg:px-16 xl:px-24 2xl:px-[155px]';
/**
* Same max width as the shell, without horizontal padding — matches full-bleed Figma frames
* (e.g. homepage hero `633:2179` at 1710×978).
*/
export const ABIIR_WIDE_INNER_FRAME_CLASS = 'mx-auto w-full max-w-[1710px]';
export const SPRING_GLOW_BANNER_GROQ = `*[_type == "home" && _id == "home"][0].springGlowBanner{
eyebrow,
titleLine1Before,
titleLine1Italic,
titleLine2,
body,
whatsIncludedLabel,
includedItems,
ratingScoreText,
ratingReviewsText,
priceMain,
compareAtLine,
saveLine,
badgeLabel,
badgeAmount,
primaryCtaLabel,
secondaryCtaLabel,
secondaryCtaHref,
bundleProductHandle,
sectionBackground,
gold,
creamText,
saveGreen,
badgeBackground,
image{
alt,
asset->{
_id,
url,
metadata{dimensions{width,height}}
}
}
}` as const;
const BUNDLE_VARIANT_QUERY = `#graphql
query SpringGlowBundleVariant($handle: String!) {
product(handle: $handle) {
selectedOrFirstAvailableVariant {
id
}
}
}
` as const;
export type SpringGlowBannerCms = {
eyebrow: string;
titleLine1Before: string;
titleLine1Italic: string;
titleLine2: string;
body: string;
whatsIncludedLabel: string;
includedItems: string[];
ratingScoreText: string;
ratingReviewsText: string;
priceMain: string;
compareAtLine: string;
saveLine: string;
badgeLabel: string;
badgeAmount: string;
primaryCtaLabel: string;
secondaryCtaLabel: string;
secondaryCtaHref: string;
bundleProductHandle: string;
sectionBackground: string;
gold: string;
creamText: string;
saveGreen: string;
badgeBackground: string;
image: {
id?: string;
url: string;
alt: string;
width?: number;
height?: number;
} | null;
};
type SanitySpringGlowPayload = {
eyebrow?: string | null;
titleLine1Before?: string | null;
titleLine1Italic?: string | null;
titleLine2?: string | null;
body?: string | null;
whatsIncludedLabel?: string | null;
includedItems?: Array<string | null> | null;
ratingScoreText?: string | null;
ratingReviewsText?: string | null;
priceMain?: string | null;
compareAtLine?: string | null;
saveLine?: string | null;
badgeLabel?: string | null;
badgeAmount?: string | null;
primaryCtaLabel?: string | null;
secondaryCtaLabel?: string | null;
secondaryCtaHref?: string | null;
bundleProductHandle?: string | null;
sectionBackground?: string | null;
gold?: string | null;
creamText?: string | null;
saveGreen?: string | null;
badgeBackground?: string | null;
image?: {
alt?: string | null;
asset?: {
_id?: string;
url?: string | null;
metadata?: {dimensions?: {width?: number | null; height?: number | null}} | null;
} | null;
} | null;
} | null;
/** Defaults from `React/src/app/components/SpringGlowBanner.tsx` (pixel-matched). */
export const MOCK_SPRING_GLOW_BANNER_CMS: SpringGlowBannerCms = {
eyebrow: 'Limited Edition · Spring 2026',
titleLine1Before: 'Spring ',
titleLine1Italic: 'Glow',
titleLine2: 'Bundle',
body:
'Celebrate the beauty of self-love and care with our exclusive best-selling 5-star rated products - curated into one radiant ritual.',
whatsIncludedLabel: "What's Included",
includedItems: [
'Hydra Face Serum 30ml',
'Hydrating Moisturizer 50ml',
'Copper Gua Sha',
],
ratingScoreText: '5.0',
ratingReviewsText: '167 reviews',
priceMain: '$99',
compareAtLine: '$135 actual value',
saveLine: 'You save $35',
badgeLabel: 'Save',
badgeAmount: '$35',
primaryCtaLabel: 'Add bundle to Cart',
secondaryCtaLabel: 'View Details',
secondaryCtaHref: '/products/glow-with-love-bundle',
bundleProductHandle: 'glow-with-love-bundle',
sectionBackground: '#5d0b24',
gold: '#b39763',
creamText: '#f8f4ef',
saveGreen: '#6bcf51',
badgeBackground: '#ede8e0',
image: null,
};
function coerceSpringGlowRatingText(value: unknown): string {
if (value == null) return '';
if (typeof value === 'number') {
if (value === 0) return '0';
return String(value);
}
return String(value).trim();
}
/** Treat CMS "0", empty, or zero-prefixed values as "rating disabled". */
function isDisabledSpringGlowRatingText(value: string): boolean {
if (!value) return true;
if (/^0+(\.0+)?$/.test(value)) return true;
if (/^0\s*(reviews?|ratings?)?$/i.test(value)) return true;
return false;
}
/** Hide stars + rating copy unless both score and reviews are meaningful. */
export function hasSpringGlowBannerRating(
cms: Pick<SpringGlowBannerCms, 'ratingScoreText' | 'ratingReviewsText'>,
): boolean {
const score = coerceSpringGlowRatingText(cms.ratingScoreText);
const reviews = coerceSpringGlowRatingText(cms.ratingReviewsText);
if (isDisabledSpringGlowRatingText(score)) return false;
if (reviews && isDisabledSpringGlowRatingText(reviews)) return false;
const n = Number.parseFloat(score);
if (!Number.isNaN(n) && n <= 0) return false;
return true;
}
export function normalizeSpringGlowBannerSanityPayload(
raw: unknown,
): SpringGlowBannerCms | null {
if (raw == null || typeof raw !== 'object') return null;
const b = raw as SanitySpringGlowPayload;
if (
!b?.eyebrow ||
!b?.body ||
!b?.primaryCtaLabel ||
!b?.secondaryCtaHref ||
!b?.includedItems?.length
) {
return null;
}
const items = b.includedItems.filter(
(x): x is string => typeof x === 'string' && x.length > 0,
);
if (!items.length) return null;
const url = b.image?.asset?.url;
const image =
url != null
? {
id: b.image?.asset?._id,
url,
alt: b.image?.alt ?? 'Spring Glow bundle',
width: b.image?.asset?.metadata?.dimensions?.width ?? undefined,
height: b.image?.asset?.metadata?.dimensions?.height ?? undefined,
}
: null;
return {
eyebrow: b.eyebrow,
titleLine1Before: b.titleLine1Before ?? 'Spring ',
titleLine1Italic: b.titleLine1Italic ?? 'Glow',
titleLine2: b.titleLine2 ?? 'Bundle',
body: b.body,
whatsIncludedLabel: b.whatsIncludedLabel ?? "What's Included",
includedItems: items,
ratingScoreText: coerceSpringGlowRatingText(b.ratingScoreText),
ratingReviewsText: coerceSpringGlowRatingText(b.ratingReviewsText),
priceMain: b.priceMain ?? '$99',
compareAtLine: b.compareAtLine ?? '$135 actual value',
saveLine: b.saveLine ?? 'You save $35',
badgeLabel: b.badgeLabel ?? 'Save',
badgeAmount: b.badgeAmount ?? '$35',
primaryCtaLabel: b.primaryCtaLabel,
secondaryCtaLabel: b.secondaryCtaLabel ?? 'View Details',
secondaryCtaHref: b.secondaryCtaHref,
bundleProductHandle: b.bundleProductHandle ?? 'glow-with-love-bundle',
sectionBackground: b.sectionBackground ?? '#5d0b24',
gold: b.gold ?? '#b39763',
creamText: b.creamText ?? '#f8f4ef',
saveGreen: b.saveGreen ?? '#6bcf51',
badgeBackground: b.badgeBackground ?? '#ede8e0',
image,
};
}
type StorefrontLike = {
query: (
query: string,
opts?: {variables?: Record<string, unknown>},
) => Promise<{
product?: {selectedOrFirstAvailableVariant?: {id?: string} | null} | null;
}>;
};
export async function loadSpringGlowBannerFromSanity(args: {
sanity: SanityContext;
storefront: StorefrontLike;
}): Promise<{springGlowCms: SpringGlowBannerCms; bundleVariantId: string | null}> {
// Fresh CMS reads (Sanity CDN + Hydrogen cache were serving stale rating fields).
const raw = await args.sanity
.fetch<SanitySpringGlowPayload | null>(SPRING_GLOW_BANNER_GROQ, {}, {
useCdn: false,
hydrogen: {cache: CacheNone()},
})
.catch((): null => null);
const normalized = normalizeSpringGlowBannerSanityPayload(
unwrapSanityQueryResult(raw) ?? raw,
);
const springGlowCms = normalized ?? MOCK_SPRING_GLOW_BANNER_CMS;
let bundleVariantId: string | null = null;
const handle = springGlowCms.bundleProductHandle?.trim();
if (handle) {
try {
const {product} = await args.storefront.query(BUNDLE_VARIANT_QUERY, {
variables: {handle},
});
bundleVariantId = product?.selectedOrFirstAvailableVariant?.id ?? null;
} catch {
bundleVariantId = null;
}
}
return {springGlowCms, bundleVariantId};
}