Files
klz-cables.com/lib/pages.ts
Marc Mintel 3de13b4fb3
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 7s
Build & Deploy / 🧪 QA (push) Failing after 55s
Build & Deploy / 🏗️ Build (push) Has been skipped
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / ⚡ Performance & Accessibility (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
chore: remove legacy mdx artifacts and dependencies
2026-02-26 01:47:30 +01:00

117 lines
3.2 KiB
TypeScript

import { getPayload } from 'payload';
import configPromise from '@payload-config';
export interface PageFrontmatter {
title: string;
excerpt: string;
featuredImage: string | null;
focalX?: number;
focalY?: number;
layout?: 'default' | 'fullBleed';
public?: boolean;
}
export interface PageData {
slug: string;
frontmatter: PageFrontmatter;
content: any; // Lexical AST Document
}
function mapDoc(doc: any): PageData {
return {
slug: doc.slug,
frontmatter: {
title: doc.title,
excerpt: doc.excerpt || '',
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
focalX:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalX
: 50,
focalY:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalY
: 50,
layout: doc.layout || 'default',
} as PageFrontmatter,
content: doc.content as any,
};
}
export async function getPageBySlug(slug: string, locale: string): Promise<PageData | null> {
try {
const payload = await getPayload({ config: configPromise });
const result = await payload.find({
collection: 'pages' as any,
where: {
slug: { equals: slug },
},
locale: locale as any,
limit: 1,
});
const docs = result.docs as any[];
if (!docs || docs.length === 0) return null;
return mapDoc(docs[0]);
} catch (error) {
console.error(`[Payload] getPageBySlug failed for ${slug}:`, error);
return null;
}
}
export async function getAllPages(locale: string): Promise<PageData[]> {
try {
const payload = await getPayload({ config: configPromise });
const result = await payload.find({
collection: 'pages' as any,
locale: locale as any,
limit: 100,
});
return (result.docs as any[]).map(mapDoc);
} catch (error) {
console.error(`[Payload] getAllPages failed for ${locale}:`, error);
return [];
}
}
export async function getAllPagesMetadata(locale: string): Promise<Partial<PageData>[]> {
try {
const payload = await getPayload({ config: configPromise });
const result = await payload.find({
collection: 'pages' as any,
locale: locale as any,
limit: 100,
});
return (result.docs as any[]).map((doc: any) => ({
slug: doc.slug,
frontmatter: {
title: doc.title,
excerpt: doc.excerpt || '',
featuredImage:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
: null,
focalX:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalX
: 50,
focalY:
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
? doc.featuredImage.focalY
: 50,
} as PageFrontmatter,
}));
} catch (error) {
console.error(`[Payload] getAllPagesMetadata failed for ${locale}:`, error);
return [];
}
}