From b7e3318aadd4403cd47861463609d27ad2cc72b2 Mon Sep 17 00:00:00 2001 From: syntaxbullet Date: Mon, 22 Jun 2026 13:15:13 +0200 Subject: [PATCH] feat: manage press fallback card media --- src/collections/Pages/pressIndexFields.ts | 2 ++ src/payload-types.ts | 4 ++++ src/scripts/preload-press-index-page-cms.ts | 22 +++++++++++++++++++++ src/spa/pages/Press.tsx | 14 +++++++------ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/collections/Pages/pressIndexFields.ts b/src/collections/Pages/pressIndexFields.ts index edbee9c..8809f79 100644 --- a/src/collections/Pages/pressIndexFields.ts +++ b/src/collections/Pages/pressIndexFields.ts @@ -35,6 +35,7 @@ const eventFields: Field[] = [ text('location', 'Location'), text('cat', 'Category'), text('status', 'Status'), + uploadField('image', 'Image'), text('img', 'Image URL or path'), textarea('desc', 'Description'), text('slug', 'Route'), @@ -49,6 +50,7 @@ const newsFields: Field[] = [ text('title', 'Title'), textarea('excerpt', 'Excerpt'), text('cat', 'Category'), + uploadField('image', 'Image'), text('img', 'Image URL or path'), text('slug', 'Route'), ] diff --git a/src/payload-types.ts b/src/payload-types.ts index 803b0c2..105a734 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -1543,6 +1543,7 @@ export interface Page { location?: string | null; cat?: string | null; status?: string | null; + image?: (number | null) | Media; img?: string | null; desc?: string | null; slug?: string | null; @@ -1569,6 +1570,7 @@ export interface Page { title?: string | null; excerpt?: string | null; cat?: string | null; + image?: (number | null) | Media; img?: string | null; slug?: string | null; id?: string | null; @@ -4443,6 +4445,7 @@ export interface PagesSelect { location?: T; cat?: T; status?: T; + image?: T; img?: T; desc?: T; slug?: T; @@ -4465,6 +4468,7 @@ export interface PagesSelect { title?: T; excerpt?: T; cat?: T; + image?: T; img?: T; slug?: T; id?: T; diff --git a/src/scripts/preload-press-index-page-cms.ts b/src/scripts/preload-press-index-page-cms.ts index 26e4afc..69e8be3 100644 --- a/src/scripts/preload-press-index-page-cms.ts +++ b/src/scripts/preload-press-index-page-cms.ts @@ -15,6 +15,22 @@ const mediaByFilename = async (payload: Payload, filename: string) => { return result.docs[0]?.id } +const filenameFromPath = (source: string) => source.split('?')[0]?.split('/').filter(Boolean).pop() + +const fallbackItemsWithImages = async ( + payload: Payload, + items: T[], +) => + Promise.all( + items.map(async (item) => { + const filename = item.img ? filenameFromPath(item.img) : undefined + return { + ...item, + image: filename ? await mediaByFilename(payload, filename) : undefined, + } + }), + ) + async function main() { const payload = await getPayload({ config }) @@ -41,6 +57,10 @@ async function main() { const { imageFilename: _heroFilename, ...heroContent } = pressIndexContent.hero const { collectionFallbackImageFilename: _eventFallbackFilename, ...eventsContent } = pressIndexContent.events const { collectionFallbackImageFilename: _newsFallbackFilename, ...newsContent } = pressIndexContent.news + const [eventFallbackItems, newsFallbackItems] = await Promise.all([ + fallbackItemsWithImages(payload, pressIndexContent.events.fallbackItems), + fallbackItemsWithImages(payload, pressIndexContent.news.fallbackItems), + ]) await payload.update({ collection: 'pages', @@ -57,10 +77,12 @@ async function main() { events: { ...eventsContent, collectionFallbackImage: eventFallbackImage, + fallbackItems: eventFallbackItems, }, news: { ...newsContent, collectionFallbackImage: newsFallbackImage, + fallbackItems: newsFallbackItems, }, downloads: { ...pressIndexContent.downloads, diff --git a/src/spa/pages/Press.tsx b/src/spa/pages/Press.tsx index 9796b5a..85506be 100644 --- a/src/spa/pages/Press.tsx +++ b/src/spa/pages/Press.tsx @@ -16,13 +16,13 @@ const FB = '"Inter", sans-serif'; type PressIndexCms = Partial & { hero?: Partial & { image?: unknown } - events?: Partial & { collectionFallbackImage?: unknown } - news?: Partial & { collectionFallbackImage?: unknown } + events?: Partial & { collectionFallbackImage?: unknown; fallbackItems?: PressEventItem[] } + news?: Partial & { collectionFallbackImage?: unknown; fallbackItems?: PressNewsItem[] } downloads?: Partial & { kitFile?: unknown } } -type PressEventItem = (typeof pressIndexContent.events.fallbackItems)[number] -type PressNewsItem = (typeof pressIndexContent.news.fallbackItems)[number] +type PressEventItem = (typeof pressIndexContent.events.fallbackItems)[number] & { image?: unknown } +type PressNewsItem = (typeof pressIndexContent.news.fallbackItems)[number] & { image?: unknown } const fallbackText = (value: unknown, fallback: string) => typeof value === 'string' && value.length > 0 ? value : fallback; const fallbackArray = (value: unknown, fallback: T[]) => Array.isArray(value) && value.length ? value as T[] : fallback; @@ -73,6 +73,8 @@ function Lines({ text }: { text: string }) { return <>{text.split('\n').map((line, i) => {i > 0 &&
}{line}
)} } +const fallbackCardImage = (item: PressEventItem | PressNewsItem) => mediaUrl(item.image, fallbackText(item.img, '')) + function NewsCard({ item, idx, isMobile, readMoreLabel, total }: { item: PressNewsItem; idx: number; isMobile: boolean; readMoreLabel: string; total: number }) { const [hovered, setHovered] = React.useState(false); return ( @@ -92,7 +94,7 @@ function NewsCard({ item, idx, isMobile, readMoreLabel, total }: { item: PressNe {/* image */}
{item.title} { > {/* Left – image cell */}
- {event.title} + {event.title}
{event.status}