160 lines
4.7 KiB
TypeScript
160 lines
4.7 KiB
TypeScript
import type {QueryResponseInitial} from '@sanity/react-loader';
|
|
import type {SanityContext} from 'hydrogen-sanity';
|
|
import communityVertical2Src from '~/assets/instagram/vertical2.png';
|
|
import communityVertical3Src from '~/assets/instagram/vertical3.png';
|
|
import communityVerticalSrc from '~/assets/instagram/vertical.png';
|
|
|
|
export type LifestyleGridTileCms = {
|
|
caption: string;
|
|
tall: boolean;
|
|
/** Resolved URL for Hydrogen `Image` (`https://…` or Vite asset URL). */
|
|
imageUrl: string;
|
|
alt: string;
|
|
/** Hint for `width` / `height` on remote images */
|
|
width?: number;
|
|
height?: number;
|
|
/** Instagram post URL — tile becomes a link when set */
|
|
postUrl?: string;
|
|
};
|
|
|
|
export type LifestyleGridSectionCms = {
|
|
eyebrow: string;
|
|
headline: string;
|
|
instagramUrl: string;
|
|
followLabel: string;
|
|
sectionBackground: string;
|
|
footerPrefix: string;
|
|
hashtag: string;
|
|
footerSuffix: string;
|
|
hashtagColor: string;
|
|
tiles: LifestyleGridTileCms[];
|
|
};
|
|
|
|
type SanityLifestylePayload = {
|
|
eyebrow?: string | null;
|
|
headline?: string | null;
|
|
instagramUrl?: string | null;
|
|
followLabel?: string | null;
|
|
sectionBackground?: string | null;
|
|
footerPrefix?: string | null;
|
|
hashtag?: string | null;
|
|
footerSuffix?: string | null;
|
|
hashtagColor?: string | null;
|
|
tiles?: Array<{
|
|
caption?: string | null;
|
|
tall?: boolean | null;
|
|
externalImageUrl?: string | null;
|
|
image?: {
|
|
alt?: string | null;
|
|
asset?: {
|
|
_id?: string;
|
|
url?: string | null;
|
|
metadata?: {dimensions?: {width?: number | null; height?: number | null}} | null;
|
|
} | null;
|
|
} | null;
|
|
} | null> | null;
|
|
} | null;
|
|
|
|
function tileFromSanity(
|
|
row: NonNullable<NonNullable<SanityLifestylePayload>['tiles']>[number],
|
|
): LifestyleGridTileCms | null {
|
|
if (!row?.caption) return null;
|
|
const ext = row.externalImageUrl?.trim();
|
|
const url = row.image?.asset?.url;
|
|
const imageUrl = ext || url;
|
|
if (!imageUrl) return null;
|
|
return {
|
|
caption: row.caption,
|
|
tall: Boolean(row.tall),
|
|
imageUrl,
|
|
alt: row.image?.alt ?? row.caption,
|
|
width: row.image?.asset?.metadata?.dimensions?.width ?? undefined,
|
|
height: row.image?.asset?.metadata?.dimensions?.height ?? undefined,
|
|
};
|
|
}
|
|
|
|
/** From `React/src/app/components/LifestyleGrid.tsx` + copied PNGs. */
|
|
export const MOCK_LIFESTYLE_GRID_SECTION_CMS: LifestyleGridSectionCms = {
|
|
eyebrow: 'Community',
|
|
headline: '@abiir_skincare',
|
|
instagramUrl: 'https://www.instagram.com/abiir_skincare/',
|
|
followLabel: 'Follow Us',
|
|
sectionBackground: '#F8F4EF',
|
|
footerPrefix: 'Tag us with ',
|
|
hashtag: '#AbiiirSkin',
|
|
footerSuffix: ' to be featured in our community',
|
|
hashtagColor: '#C17D5C',
|
|
tiles: [
|
|
{
|
|
caption: '@abiir_skincare',
|
|
tall: true,
|
|
imageUrl: communityVerticalSrc,
|
|
alt: 'ABiiR Skincare — community',
|
|
width: 455,
|
|
height: 580,
|
|
},
|
|
{
|
|
caption: '@abiir_skincare',
|
|
tall: false,
|
|
imageUrl: communityVertical2Src,
|
|
alt: 'ABiiR Skincare — community',
|
|
width: 455,
|
|
height: 580,
|
|
},
|
|
{
|
|
caption: '@abiir_skincare',
|
|
tall: false,
|
|
imageUrl: communityVertical3Src,
|
|
alt: 'ABiiR Skincare — community',
|
|
width: 455,
|
|
height: 580,
|
|
},
|
|
],
|
|
};
|
|
|
|
export function normalizeLifestyleGridSanityPayload(
|
|
raw: unknown,
|
|
): LifestyleGridSectionCms | null {
|
|
if (raw == null || typeof raw !== 'object') return null;
|
|
const b = raw as SanityLifestylePayload;
|
|
if (!b?.eyebrow || !b?.headline || !b?.tiles?.length) return null;
|
|
|
|
const tiles: LifestyleGridTileCms[] = [];
|
|
for (const row of b.tiles) {
|
|
const t = tileFromSanity(row);
|
|
if (t) tiles.push(t);
|
|
}
|
|
if (tiles.length !== 3) return null;
|
|
|
|
return {
|
|
eyebrow: b.eyebrow,
|
|
headline: b.headline,
|
|
instagramUrl: b.instagramUrl ?? 'https://www.instagram.com/abiir_skincare/',
|
|
followLabel: b.followLabel ?? 'Follow Us',
|
|
sectionBackground: b.sectionBackground ?? '#F8F4EF',
|
|
footerPrefix: b.footerPrefix ?? 'Tag us with ',
|
|
hashtag: b.hashtag ?? '#AbiiirSkin',
|
|
footerSuffix: b.footerSuffix ?? ' to be featured in our community',
|
|
hashtagColor: b.hashtagColor ?? '#C17D5C',
|
|
tiles,
|
|
};
|
|
}
|
|
|
|
export async function loadLifestyleGridFromSanity(args: {
|
|
sanity: SanityContext;
|
|
groq: string;
|
|
}): Promise<{lifestyleGridCms: LifestyleGridSectionCms}> {
|
|
const initial: QueryResponseInitial<SanityLifestylePayload> = await args.sanity
|
|
.loadQuery<SanityLifestylePayload>(args.groq)
|
|
.catch(
|
|
(): QueryResponseInitial<SanityLifestylePayload> => ({
|
|
data: null,
|
|
sourceMap: undefined,
|
|
}),
|
|
);
|
|
const normalized = normalizeLifestyleGridSanityPayload(initial.data);
|
|
return {
|
|
lifestyleGridCms: normalized ?? MOCK_LIFESTYLE_GRID_SECTION_CMS,
|
|
};
|
|
}
|