204 lines
6.8 KiB
TypeScript
204 lines
6.8 KiB
TypeScript
import type {QueryResponseInitial} from '@sanity/react-loader';
|
|
import type {SanityContext} from 'hydrogen-sanity';
|
|
|
|
export const INGREDIENTS_ICON_KEYS = [
|
|
'leaf',
|
|
'flame',
|
|
'pillBottle',
|
|
'droplets',
|
|
'beaker',
|
|
] as const;
|
|
|
|
export type IngredientsIconKey = (typeof INGREDIENTS_ICON_KEYS)[number];
|
|
|
|
export type IngredientsRowCms = {
|
|
iconKey: IngredientsIconKey;
|
|
name: string;
|
|
description: string;
|
|
};
|
|
|
|
export type IngredientsSectionCms = {
|
|
eyebrow: string;
|
|
headlineLine1Before: string;
|
|
headlineLine1Accent: string;
|
|
headlineLine2: string;
|
|
body: string;
|
|
badgePercent: string;
|
|
badgeCaption: string;
|
|
primaryCtaLabel: string;
|
|
primaryCtaHref: string;
|
|
image: {
|
|
id?: string;
|
|
url: string;
|
|
alt: string;
|
|
width?: number;
|
|
height?: number;
|
|
} | null;
|
|
ingredients: IngredientsRowCms[];
|
|
sectionBackground: string;
|
|
gold: string;
|
|
cream: string;
|
|
creamMuted: string;
|
|
badgeBackground: string;
|
|
charcoal: string;
|
|
};
|
|
|
|
type SanityIngredientsPayload = {
|
|
eyebrow?: string | null;
|
|
headlineLine1Before?: string | null;
|
|
headlineLine1Accent?: string | null;
|
|
headlineLine2?: string | null;
|
|
body?: string | null;
|
|
badgePercent?: string | null;
|
|
badgeCaption?: string | null;
|
|
primaryCtaLabel?: string | null;
|
|
primaryCtaHref?: string | null;
|
|
image?: {
|
|
alt?: string | null;
|
|
asset?: {
|
|
_id?: string;
|
|
url?: string | null;
|
|
metadata?: {dimensions?: {width?: number | null; height?: number | null}} | null;
|
|
} | null;
|
|
} | null;
|
|
ingredients?: Array<{
|
|
iconKey?: string | null;
|
|
name?: string | null;
|
|
description?: string | null;
|
|
} | null> | null;
|
|
sectionBackground?: string | null;
|
|
gold?: string | null;
|
|
cream?: string | null;
|
|
creamMuted?: string | null;
|
|
badgeBackground?: string | null;
|
|
charcoal?: string | null;
|
|
} | null;
|
|
|
|
function isIngredientsIconKey(x: string): x is IngredientsIconKey {
|
|
return (INGREDIENTS_ICON_KEYS as readonly string[]).includes(x);
|
|
}
|
|
|
|
/** Pixel-matched defaults from `React/src/app/components/Ingredients.tsx`. */
|
|
export const MOCK_INGREDIENTS_SECTION_CMS: IngredientsSectionCms = {
|
|
eyebrow: 'Ingredient Philosophy',
|
|
headlineLine1Before: '',
|
|
headlineLine1Accent: 'Potent',
|
|
headlineLine2: 'Ingredients',
|
|
body:
|
|
'We source rare botanicals from sustainable farms around the world, combining them with clinically proven actives to create formulas that truly deliver results.',
|
|
badgePercent: '98%',
|
|
badgeCaption: 'Naturally Derived Ingredients',
|
|
primaryCtaLabel: 'More Details',
|
|
primaryCtaHref: '/about',
|
|
image: null,
|
|
sectionBackground: '#1a1a18',
|
|
gold: '#b39763',
|
|
cream: '#f8f4ef',
|
|
creamMuted: 'rgba(248, 244, 239, 0.5)',
|
|
badgeBackground: '#faf8f3',
|
|
charcoal: '#1a1a1a',
|
|
ingredients: [
|
|
{
|
|
iconKey: 'leaf',
|
|
name: 'Centella Asiatica',
|
|
description:
|
|
'Known for its soothing properties, Centella Asiatica helps in calming irritated skin and reducing redness and inflammation. It promotes collagen production, aiding in skin healing and improving elasticity, making it ideal for sensitive or damaged skin',
|
|
},
|
|
{
|
|
iconKey: 'flame',
|
|
name: 'Rose Oil',
|
|
description:
|
|
"Rich in antioxidants, rose oil can help nourish and rejuvenate the skin. It offers anti-inflammatory benefits, calming redness and irritation. Additionally, it can help balance the skin's oil production, hydration levels, and provides a pleasant aroma that enhances the overall skincare experience.",
|
|
},
|
|
{
|
|
iconKey: 'pillBottle',
|
|
name: 'Turmeric',
|
|
description:
|
|
'Turmeric is renowned for its anti-inflammatory and antibacterial properties. It can help brighten the skin, reduce acne scars, and even out skin tone. Its antioxidant content also protects the skin from free radical damage, promoting a healthy and radiant complexion.',
|
|
},
|
|
{
|
|
iconKey: 'droplets',
|
|
name: 'Saffron Oil',
|
|
description:
|
|
'Saffron oil is known for its ability to hydrate and effectively brighten the skin. Rich in antioxidants, it helps reduce the appearance of dark spots and pigmentation. Additionally, it has anti-inflammatory benefits that can soothe skin irritations and improve overall skin texture.',
|
|
},
|
|
{
|
|
iconKey: 'beaker',
|
|
name: 'Hyaluronic Acid',
|
|
description:
|
|
'Hyaluronic acid is a powerful humectant, meaning it attracts moisture to the skin. It provides deep hydration, plumping up the skin to minimize fine lines and wrinkles. Its ability to retain water makes it essential for maintaining skin elasticity and overall hydration levels.',
|
|
},
|
|
],
|
|
};
|
|
|
|
export function normalizeIngredientsSanityPayload(
|
|
raw: unknown,
|
|
): IngredientsSectionCms | null {
|
|
if (raw == null || typeof raw !== 'object') return null;
|
|
const b = raw as SanityIngredientsPayload;
|
|
if (!b?.eyebrow || !b?.body || !b?.ingredients?.length) return null;
|
|
|
|
const rows: IngredientsRowCms[] = [];
|
|
for (const row of b.ingredients) {
|
|
if (!row?.name || !row?.description || !row.iconKey) continue;
|
|
const key = String(row.iconKey);
|
|
if (!isIngredientsIconKey(key)) continue;
|
|
rows.push({
|
|
iconKey: key,
|
|
name: row.name,
|
|
description: row.description,
|
|
});
|
|
}
|
|
if (!rows.length) return null;
|
|
|
|
const url = b.image?.asset?.url;
|
|
const image =
|
|
url != null
|
|
? {
|
|
id: b.image?.asset?._id,
|
|
url,
|
|
alt: b.image?.alt ?? 'Ingredients',
|
|
width: b.image?.asset?.metadata?.dimensions?.width ?? undefined,
|
|
height: b.image?.asset?.metadata?.dimensions?.height ?? undefined,
|
|
}
|
|
: null;
|
|
|
|
return {
|
|
eyebrow: b.eyebrow,
|
|
headlineLine1Before: b.headlineLine1Before ?? '',
|
|
headlineLine1Accent: b.headlineLine1Accent ?? 'Potent',
|
|
headlineLine2: b.headlineLine2 ?? 'Ingredients',
|
|
body: b.body,
|
|
badgePercent: b.badgePercent ?? '98%',
|
|
badgeCaption: b.badgeCaption ?? 'Naturally Derived Ingredients',
|
|
primaryCtaLabel: b.primaryCtaLabel ?? 'More Details',
|
|
primaryCtaHref: b.primaryCtaHref ?? '/about',
|
|
image,
|
|
ingredients: rows,
|
|
sectionBackground: b.sectionBackground ?? '#1a1a18',
|
|
gold: b.gold ?? '#b39763',
|
|
cream: b.cream ?? '#f8f4ef',
|
|
creamMuted: b.creamMuted ?? 'rgba(248, 244, 239, 0.5)',
|
|
badgeBackground: b.badgeBackground ?? '#faf8f3',
|
|
charcoal: b.charcoal ?? '#1a1a1a',
|
|
};
|
|
}
|
|
|
|
export async function loadIngredientsFromSanity(args: {
|
|
sanity: SanityContext;
|
|
groq: string;
|
|
}): Promise<{ingredientsCms: IngredientsSectionCms}> {
|
|
const initial: QueryResponseInitial<SanityIngredientsPayload> = await args.sanity
|
|
.loadQuery<SanityIngredientsPayload>(args.groq)
|
|
.catch(
|
|
(): QueryResponseInitial<SanityIngredientsPayload> => ({
|
|
data: null,
|
|
sourceMap: undefined,
|
|
}),
|
|
);
|
|
const normalized = normalizeIngredientsSanityPayload(initial.data);
|
|
return {
|
|
ingredientsCms: normalized ?? MOCK_INGREDIENTS_SECTION_CMS,
|
|
};
|
|
}
|