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

163 lines
4.5 KiB
TypeScript

import type {QueryResponseInitial} from '@sanity/react-loader';
import type {SanityContext} from 'hydrogen-sanity';
export const BRAND_VALUE_ICON_KEYS = [
'leaf',
'flaskConical',
'shield',
'sparkles',
] as const;
export type BrandValueIconKey = (typeof BRAND_VALUE_ICON_KEYS)[number];
export type BrandValueItemCms = {
iconKey: BrandValueIconKey;
title: string;
description: string;
};
export type BrandValuesCms = {
eyebrow: string;
headlinePrefix: string;
headlineItalic: string;
sectionBackground: string;
iconTileBackground: string;
accentBurgundy: string;
titleColor: string;
bodyColor: string;
gridGutterColor: string;
values: BrandValueItemCms[];
};
/**
* GROQ: `home` singleton → brand values module.
* Re-exported from `~/components/BrandValues.tsx` for co-location with the UI.
*/
export const BRAND_VALUES_GROQ = `*[_type == "home" && _id == "home"][0].brandValues{
eyebrow,
headlinePrefix,
headlineItalic,
sectionBackground,
iconTileBackground,
accentBurgundy,
titleColor,
bodyColor,
gridGutterColor,
values[]{
iconKey,
title,
description
}
}` as const;
type SanityBrandValuesPayload = {
eyebrow?: string | null;
headlinePrefix?: string | null;
headlineItalic?: string | null;
sectionBackground?: string | null;
iconTileBackground?: string | null;
accentBurgundy?: string | null;
titleColor?: string | null;
bodyColor?: string | null;
gridGutterColor?: string | null;
values?: Array<{
iconKey?: string | null;
title?: string | null;
description?: string | null;
} | null> | null;
} | null;
function isBrandValueIconKey(x: string): x is BrandValueIconKey {
return (BRAND_VALUE_ICON_KEYS as readonly string[]).includes(x);
}
/** Defaults from `React/src/app/components/BrandValues.tsx`. */
export const MOCK_BRAND_VALUES_CMS: BrandValuesCms = {
eyebrow: 'THE ABiiR DIFFERENCE',
headlinePrefix: 'Beauty Rooted in ',
headlineItalic: 'Truth',
sectionBackground: '#ede8e0',
iconTileBackground: '#f8f4ef',
accentBurgundy: '#5d0b24',
titleColor: '#1a1a18',
bodyColor: '#7a7068',
gridGutterColor: 'rgba(26, 26, 24, 0.08)',
values: [
{
iconKey: 'leaf',
title: 'Clean Formulas',
description:
'Free from parabens, sulfates, and synthetic fragrances. Every ingredient is chosen with intention.',
},
{
iconKey: 'flaskConical',
title: 'Science-Backed',
description:
'Clinically tested actives combined with time-honored botanical wisdom for visible results.',
},
{
iconKey: 'shield',
title: 'Skin-Safe Promise',
description:
'Dermatologist approved, cruelty-free, and suitable for all skin types including sensitive.',
},
{
iconKey: 'sparkles',
title: 'Luxury Ritual',
description:
'Every ABiiR product transforms your routine into a sensorial, meditative experience.',
},
],
};
export function normalizeBrandValuesSanityPayload(
raw: unknown,
): BrandValuesCms | null {
if (raw == null || typeof raw !== 'object') return null;
const b = raw as SanityBrandValuesPayload;
if (!b?.eyebrow || !b?.values?.length) return null;
const values: BrandValueItemCms[] = [];
for (const row of b.values) {
if (!row?.title || !row?.description || !row.iconKey) continue;
const key = String(row.iconKey);
if (!isBrandValueIconKey(key)) continue;
values.push({
iconKey: key,
title: row.title,
description: row.description,
});
}
if (!values.length) return null;
return {
eyebrow: b.eyebrow,
headlinePrefix: b.headlinePrefix ?? 'Beauty Rooted in ',
headlineItalic: b.headlineItalic ?? 'Truth',
sectionBackground: b.sectionBackground ?? '#ede8e0',
iconTileBackground: b.iconTileBackground ?? '#f8f4ef',
accentBurgundy: b.accentBurgundy ?? '#5d0b24',
titleColor: b.titleColor ?? '#1a1a18',
bodyColor: b.bodyColor ?? '#7a7068',
gridGutterColor: b.gridGutterColor ?? 'rgba(26, 26, 24, 0.08)',
values,
};
}
export async function loadBrandValuesFromSanity(args: {
sanity: SanityContext;
}): Promise<{brandValuesCms: BrandValuesCms}> {
const initial: QueryResponseInitial<SanityBrandValuesPayload> = await args.sanity
.loadQuery<SanityBrandValuesPayload>(BRAND_VALUES_GROQ)
.catch(
(): QueryResponseInitial<SanityBrandValuesPayload> => ({
data: null,
sourceMap: undefined,
}),
);
const normalized = normalizeBrandValuesSanityPayload(initial.data);
return {
brandValuesCms: normalized ?? MOCK_BRAND_VALUES_CMS,
};
}