import { useMetaData } from '@websolutespa/bom-mixer-hooks'; import { IMetaData, OpenGraphArticle, OpenGraphBook, OpenGraphImage, OpenGraphProduct, OpenGraphProfile, OpenGraphType } from '@websolutespa/bom-mixer-models'; import Head from 'next/head'; import React from 'react'; function generateImageTags(image: OpenGraphImage, index?: number): React.ReactElement[] { const tags: React.ReactElement[] = []; const suffix = index !== undefined ? `:${index}` : ''; tags.push(); if (image.secureUrl) { tags.push(); } if (image.type) { tags.push(); } if (image.width) { tags.push(); } if (image.height) { tags.push(); } if (image.alt) { tags.push(); } return tags; } export function renderOpenGraph(data: OpenGraphType): React.ReactElement[] { const tags: React.ReactElement[] = []; // Basic Open Graph tags if (data.title) { tags.push(); } if (data.type) { tags.push(); } if (data.url) { tags.push(); } if (data.description) { tags.push(); } if (data.siteName) { tags.push(); } if (data.locale) { tags.push(); } if (data.localeAlternate) { data.localeAlternate.forEach((locale, index) => { tags.push(); }); } // Handle images if (data.image) { if (typeof data.image === 'string') { tags.push(); } else if (Array.isArray(data.image)) { data.image.forEach((img, index) => { if (typeof img === 'string') { tags.push(); } else { tags.push(...generateImageTags(img, index)); } }); } else { tags.push(...generateImageTags(data.image)); } } // Type-specific tags switch (data.type) { case 'article': { const articleData = data as OpenGraphArticle; if (articleData.publishedTime) { tags.push( ); } if (articleData.modifiedTime) { tags.push( ); } if (articleData.expirationTime) { tags.push( ); } if (articleData.author) { const authors = Array.isArray(articleData.author) ? articleData.author : [articleData.author]; authors.forEach((author, index) => { tags.push(); }); } if (articleData.section) { tags.push(); } if (articleData.tag) { const articleTags = Array.isArray(articleData.tag) ? articleData.tag : [articleData.tag]; articleTags.forEach((tag, index) => { tags.push(); }); } break; } case 'book': { const bookData = data as OpenGraphBook; if (bookData.author) { const authors = Array.isArray(bookData.author) ? bookData.author : [bookData.author]; authors.forEach((author, index) => { tags.push(); }); } if (bookData.isbn) { tags.push(); } if (bookData.releaseDate) { tags.push(); } if (bookData.tag) { const bookTags = Array.isArray(bookData.tag) ? bookData.tag : [bookData.tag]; bookTags.forEach((tag, index) => { tags.push(); }); } break; } case 'profile': { const profileData = data as OpenGraphProfile; if (profileData.firstName) { tags.push(); } if (profileData.lastName) { tags.push(); } if (profileData.username) { tags.push(); } if (profileData.gender) { tags.push(); } break; } case 'product': { const productData = data as OpenGraphProduct; if (productData.price) { tags.push( ); tags.push( ); } if (productData.availability) { tags.push( ); } if (productData.condition) { tags.push(); } if (productData.retailerItemId) { tags.push( ); } if (productData.brand) { tags.push(); } break; } } return tags; } export function OpenGraph({ item }: { item?: (metaData: IMetaData) => OpenGraphType }) { const metaData = useMetaData(); const openGraph = item ? item(metaData) : metaData.openGraph; if (openGraph) { return ( {renderOpenGraph(openGraph)} ); } return <>; }