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

97 lines
3.4 KiB
TypeScript
Raw 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 type {QueryResponseInitial} from '@sanity/react-loader';
import type {SanityContext} from 'hydrogen-sanity';
export function unwrapSanityQueryResult<T>(
result: QueryResponseInitial<T> | T | null | undefined,
): T | null {
if (result == null) return null;
if (typeof result === 'object' && result !== null && 'data' in result) {
return (result as QueryResponseInitial<T>).data ?? null;
}
return result as T;
}
/**
* Normalized editorial slice for the PDP, projected from Sanity `product.abiirPdp`
* and merged with Storefront data in the route.
*/
export type AbiirPdpCms = {
subtitle: string;
shortDescription: string;
benefitChips: string[];
keyIngredients: Array<{name: string; benefit: string}>;
howToUse: string;
ingredients: string[];
shippingReturns: string;
rating: number;
reviewCount: number;
relatedProductHandles: string[];
};
export type SanityProductDetailPayload = {
abiirPdp: Partial<{
subtitle: string | null;
shortDescription: string | null;
benefitChips: (string | null)[] | null;
keyIngredients: Array<{name?: string | null; benefit?: string | null} | null> | null;
howToUse: string | null;
ingredients: (string | null)[] | null;
shippingReturns: string | null;
rating: number | null;
reviewCount: number | null;
relatedProductHandles: (string | null)[] | null;
}> | null;
} | null;
const DEFAULT_SHIPPING =
'Complimentary shipping on orders over $75. Standard delivery 3–5 business days. Express available at checkout. Free returns within 30 days of purchase — no questions asked.';
export const PDP_DEFAULT_SHIPPING_RETURNS = DEFAULT_SHIPPING;
function compactStrings(arr: (string | null | undefined)[] | null | undefined): string[] {
if (!arr?.length) return [];
return arr.filter((s): s is string => typeof s === 'string' && s.length > 0);
}
function normalizePdp(
raw: NonNullable<SanityProductDetailPayload>['abiirPdp'] | null | undefined,
): Partial<AbiirPdpCms> {
if (!raw) return {};
return {
subtitle: raw.subtitle?.trim() || undefined,
shortDescription: raw.shortDescription?.trim() || undefined,
benefitChips: compactStrings(raw.benefitChips ?? []),
keyIngredients: (raw.keyIngredients ?? [])
.map((k) => {
if (!k?.name) return null;
return {
name: k.name,
benefit: (k.benefit ?? '').trim(),
};
})
.filter((k): k is {name: string; benefit: string} => k !== null),
howToUse: raw.howToUse?.trim() || undefined,
ingredients: compactStrings(raw.ingredients ?? []),
shippingReturns: raw.shippingReturns?.trim() || undefined,
rating: typeof raw.rating === 'number' ? raw.rating : undefined,
reviewCount: typeof raw.reviewCount === 'number' ? raw.reviewCount : undefined,
relatedProductHandles: compactStrings(raw.relatedProductHandles ?? []),
};
}
/**
* Fetches the Sanity `product` document (Shopify sync) by handle and returns normalized PDP fields.
*/
export async function loadProductDetailFromSanity(args: {
sanity: SanityContext;
groq: string;
}): Promise<Partial<AbiirPdpCms> | null> {
const raw = await args.sanity
.query<SanityProductDetailPayload | null>(args.groq)
.catch((): null => null);
const initial = unwrapSanityQueryResult<SanityProductDetailPayload | null>(raw);
if (initial == null) return null;
const partial = normalizePdp(initial.abiirPdp);
return Object.keys(partial).length > 0 ? partial : null;
}