import React, { useState } from "react"; import { useWorkspace } from "sanity"; import { Stack, Card, Flex, Text, Button, Code, Box, useTheme } from "@sanity/ui"; import { CodeBlockIcon } from "@sanity/icons"; /* eslint-disable @typescript-eslint/no-explicit-any */ function getImageUrl( asset: Record, projectId: string, dataset: string, ): string | null { if (!asset) return null; if (asset.url) return asset.url; if (asset._ref && projectId && dataset) { const ref = asset._ref; const match = ref.match(/^image-([a-z0-9]+)-(\d+x\d+)-(.+)$/); if (match) { const [, id, dims, format] = match; return `https://cdn.sanity.io/images/${projectId}/${dataset}/${id}-${dims}.${format}`; } } return null; } function buildMetaTags( value: Record | undefined, projectId: string, dataset: string, ): string { const v = value || {}; const lines: string[] = []; if (v.metaTitle) lines.push(`${v.metaTitle}`); if (v.metaDescription) lines.push(``); const robotsParts: string[] = []; if (v.nofollowAttributes) robotsParts.push("noindex", "nofollow"); if (Array.isArray(v.robotsMeta) && v.robotsMeta.length > 0) { v.robotsMeta.forEach((r: string) => { if (!robotsParts.includes(r)) robotsParts.push(r); }); } if (robotsParts.length > 0) lines.push(``); if (Array.isArray(v.seoKeywords) && v.seoKeywords.length > 0) lines.push(``); const og = v.openGraph || {}; if (og.title) lines.push(``); if (og.description) lines.push(``); if (og.siteName) lines.push(``); const ogImageUrl = og.image?.asset ? getImageUrl(og.image.asset, projectId, dataset) : null; if (ogImageUrl) { lines.push(``); } const tw = v.twitter || {}; if (tw.cardType) lines.push(``); if (tw.site) lines.push(``); if (tw.creator) lines.push(``); if (Array.isArray(v.hreflang)) { v.hreflang.forEach((h: any) => { if (h.locale && h.url) lines.push(``); }); } if (Array.isArray(v.additionalMetaTags)) { v.additionalMetaTags.forEach((tag: any) => { if (Array.isArray(tag.metaAttributes)) { tag.metaAttributes.forEach((attr: any) => { if (attr.attributeKey && attr.attributeValueString) { lines.push( ``, ); } else if (attr.attributeKey && attr.attributeValueImage?.asset) { const imageUrl = getImageUrl(attr.attributeValueImage.asset, projectId, dataset); if (imageUrl) { lines.push(``); } } }); } }); } return lines.join("\n"); } export default function MetaTagsPreview({ value }: { value: Record | undefined }) { const { projectId, dataset } = useWorkspace(); const [open, setOpen] = useState(false); const tags = buildMetaTags(value, projectId, dataset); const theme = useTheme(); const isDarkMode = theme.sanity.v2?.color._dark ?? (theme.sanity as unknown as { color: { dark: boolean } }).color.dark; return ( Meta Tags Preview