248 lines
7.0 KiB
TypeScript
248 lines
7.0 KiB
TypeScript
import type {QueryResponseInitial} from '@sanity/react-loader';
|
|
import type {SanityContext} from 'hydrogen-sanity';
|
|
import {
|
|
blogCategories as MOCK_JOURNAL_CATEGORY_LABELS,
|
|
blogPosts as MOCK_JOURNAL_POSTS_SEED,
|
|
type BlogPost,
|
|
} from '~/lib/journalBlogPostsSeed';
|
|
|
|
export type AbiirJournalImageCms = {
|
|
id?: string;
|
|
url: string;
|
|
alt: string;
|
|
width?: number;
|
|
height?: number;
|
|
};
|
|
|
|
/** One journal article — matches `Journal.tsx` / `JournalPost.tsx` data shape. */
|
|
export type AbiirJournalPostCms = BlogPost & {
|
|
/** Resolved image for `<Image />` (local Vite URL or CDN). */
|
|
image: string;
|
|
};
|
|
|
|
export type AbiirJournalIndexCms = {
|
|
seoTitle: string;
|
|
seoDescription: string;
|
|
eyebrow: string;
|
|
headline: string;
|
|
description: string;
|
|
categoryFilters: string[];
|
|
};
|
|
|
|
type SanityImageField = {
|
|
alt?: string | null;
|
|
asset?: {
|
|
_id?: string;
|
|
url?: string | null;
|
|
metadata?: {dimensions?: {width?: number | null; height?: number | null}} | null;
|
|
} | null;
|
|
} | null;
|
|
|
|
type SanityJournalPostPayload = {
|
|
/** Flattened from GROQ `"slug": slug.current` or nested Sanity slug. */
|
|
slug?: string | {current?: string | null} | null;
|
|
title?: string | null;
|
|
subtitle?: string | null;
|
|
excerpt?: string | null;
|
|
cardExcerpt?: string | null;
|
|
content?: string | null;
|
|
author?: string | null;
|
|
authorRole?: string | null;
|
|
date?: string | null;
|
|
readTime?: string | null;
|
|
category?: string | null;
|
|
featured?: boolean | null;
|
|
tags?: Array<string | null> | null;
|
|
image?: SanityImageField;
|
|
} | null;
|
|
|
|
type SanityJournalIndexPayload = {
|
|
seo?: {title?: string | null; description?: string | null} | null;
|
|
eyebrow?: string | null;
|
|
headline?: string | null;
|
|
description?: string | null;
|
|
categoryFilters?: Array<string | null> | null;
|
|
} | null;
|
|
|
|
function isRemoteUrl(url: string) {
|
|
return /^https?:\/\//i.test(url);
|
|
}
|
|
|
|
export function abiirJournalImageIsRemote(url: string) {
|
|
return isRemoteUrl(url);
|
|
}
|
|
|
|
function normalizeSanityImage(
|
|
field: SanityImageField,
|
|
fallbackUrl: string,
|
|
fallbackAlt: string,
|
|
): AbiirJournalImageCms {
|
|
const url = field?.asset?.url;
|
|
if (typeof url === 'string' && url.length > 0) {
|
|
return {
|
|
id: field?.asset?._id,
|
|
url,
|
|
alt: typeof field?.alt === 'string' ? field.alt : fallbackAlt,
|
|
width: field?.asset?.metadata?.dimensions?.width ?? undefined,
|
|
height: field?.asset?.metadata?.dimensions?.height ?? undefined,
|
|
};
|
|
}
|
|
return {
|
|
url: fallbackUrl,
|
|
alt: fallbackAlt,
|
|
};
|
|
}
|
|
|
|
export const MOCK_ABIIR_JOURNAL_INDEX: AbiirJournalIndexCms = {
|
|
seoTitle: 'Journal | ABiiR',
|
|
seoDescription:
|
|
'Stories, science, and rituals from the ABiiR team — skin knowledge, beautifully told.',
|
|
eyebrow: 'The ABiiR Journal',
|
|
headline: 'Skin Intelligence',
|
|
description:
|
|
'Stories, science, and rituals from our team of experts. Skin knowledge, beautifully told.',
|
|
categoryFilters: [...MOCK_JOURNAL_CATEGORY_LABELS],
|
|
};
|
|
|
|
/** Local preview — same posts as `React/src/app/data/blogPosts.ts` with bundled images. */
|
|
export const MOCK_ABIIR_JOURNAL_POSTS: AbiirJournalPostCms[] =
|
|
MOCK_JOURNAL_POSTS_SEED.map((p) => ({
|
|
...p,
|
|
image: p.image,
|
|
}));
|
|
|
|
export function normalizeAbiirJournalIndexPayload(
|
|
raw: unknown,
|
|
): AbiirJournalIndexCms | null {
|
|
if (raw == null || typeof raw !== 'object') return null;
|
|
const d = raw as SanityJournalIndexPayload;
|
|
const m = MOCK_ABIIR_JOURNAL_INDEX;
|
|
const filters = (d.categoryFilters ?? [])
|
|
.map((x) => (typeof x === 'string' ? x.trim() : ''))
|
|
.filter(Boolean);
|
|
return {
|
|
seoTitle: d.seo?.title?.trim() || m.seoTitle,
|
|
seoDescription: d.seo?.description?.trim() || m.seoDescription,
|
|
eyebrow: d.eyebrow ?? m.eyebrow,
|
|
headline: d.headline ?? m.headline,
|
|
description: d.description ?? m.description,
|
|
categoryFilters: filters.length ? filters : m.categoryFilters,
|
|
};
|
|
}
|
|
|
|
function normalizeOneSanityPost(
|
|
raw: SanityJournalPostPayload,
|
|
imageFallback: AbiirJournalImageCms,
|
|
): AbiirJournalPostCms | null {
|
|
const slug =
|
|
typeof raw.slug === 'string'
|
|
? raw.slug.trim()
|
|
: raw.slug?.current?.trim();
|
|
const title = raw?.title?.trim();
|
|
const content = raw?.content?.trim();
|
|
const category = raw?.category?.trim();
|
|
if (!slug || !title || !content || !category) return null;
|
|
|
|
const img = normalizeSanityImage(
|
|
raw.image ?? null,
|
|
imageFallback.url,
|
|
imageFallback.alt || title,
|
|
);
|
|
|
|
const tags = (raw.tags ?? [])
|
|
.map((t) => (typeof t === 'string' ? t.trim() : ''))
|
|
.filter(Boolean);
|
|
|
|
return {
|
|
id: slug,
|
|
title,
|
|
subtitle: raw.subtitle?.trim() ?? '',
|
|
excerpt: raw.excerpt?.trim() ?? '',
|
|
cardExcerpt: raw.cardExcerpt?.trim() || undefined,
|
|
content,
|
|
author: raw.author?.trim() ?? '',
|
|
authorRole: raw.authorRole?.trim() ?? '',
|
|
date: raw.date?.trim() ?? '',
|
|
readTime: raw.readTime?.trim() ?? '5 min read',
|
|
category,
|
|
featured: Boolean(raw.featured),
|
|
tags,
|
|
image: img.url,
|
|
};
|
|
}
|
|
|
|
export function normalizeAbiirJournalPostsPayload(
|
|
raw: unknown,
|
|
): AbiirJournalPostCms[] | null {
|
|
if (!Array.isArray(raw)) return null;
|
|
const m = MOCK_ABIIR_JOURNAL_POSTS;
|
|
const out: AbiirJournalPostCms[] = [];
|
|
for (let i = 0; i < raw.length; i++) {
|
|
const row = raw[i] as SanityJournalPostPayload;
|
|
const fallback = m[i % m.length];
|
|
const normalized = normalizeOneSanityPost(row, {
|
|
url: fallback.image,
|
|
alt: fallback.title,
|
|
});
|
|
if (normalized) out.push(normalized);
|
|
}
|
|
return out.length ? out : null;
|
|
}
|
|
|
|
export async function loadJournalPageFromSanity(args: {
|
|
sanity: SanityContext;
|
|
groq: string;
|
|
}): Promise<{
|
|
indexCms: AbiirJournalIndexCms;
|
|
postsCms: AbiirJournalPostCms[];
|
|
}> {
|
|
type Bundle = {
|
|
index: SanityJournalIndexPayload;
|
|
posts: SanityJournalPostPayload[] | null;
|
|
};
|
|
const initial: QueryResponseInitial<Bundle> = await args.sanity
|
|
.loadQuery<Bundle>(args.groq)
|
|
.catch(
|
|
(): QueryResponseInitial<Bundle> => ({
|
|
data: null,
|
|
sourceMap: undefined,
|
|
}),
|
|
);
|
|
const data = initial.data;
|
|
const index =
|
|
normalizeAbiirJournalIndexPayload(data?.index ?? null) ??
|
|
MOCK_ABIIR_JOURNAL_INDEX;
|
|
const posts =
|
|
normalizeAbiirJournalPostsPayload(data?.posts ?? null) ??
|
|
MOCK_ABIIR_JOURNAL_POSTS;
|
|
return {indexCms: index, postsCms: posts};
|
|
}
|
|
|
|
export async function loadJournalPostFromSanity(args: {
|
|
sanity: SanityContext;
|
|
groq: string;
|
|
}): Promise<{postCms: AbiirJournalPostCms | null}> {
|
|
const initial: QueryResponseInitial<SanityJournalPostPayload | null> =
|
|
await args.sanity.loadQuery<SanityJournalPostPayload | null>(args.groq).catch(
|
|
(): QueryResponseInitial<SanityJournalPostPayload | null> => ({
|
|
data: null,
|
|
sourceMap: undefined,
|
|
}),
|
|
);
|
|
if (initial.data == null) {
|
|
return {postCms: null};
|
|
}
|
|
const m = MOCK_ABIIR_JOURNAL_POSTS[0]!;
|
|
const normalized = normalizeOneSanityPost(initial.data, {
|
|
url: m.image,
|
|
alt: m.title,
|
|
});
|
|
return {postCms: normalized};
|
|
}
|
|
|
|
export function getMockJournalPostBySlug(
|
|
slug: string,
|
|
): AbiirJournalPostCms | null {
|
|
return MOCK_ABIIR_JOURNAL_POSTS.find((p) => p.id === slug) ?? null;
|
|
}
|