import React, { useState } from "react"; import { useWorkspace } from "sanity"; import { getPluginConfig } from "../config"; /* eslint-disable @typescript-eslint/no-explicit-any */ interface Props { value: Record | undefined; } 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 ImageSlot({ url }: { url: string | null }) { return (
{url ? ( preview ) : ( )}
); } function TwitterCard({ og, imageUrl, domain, }: { og: any; imageUrl: string | null; domain: string; }) { return (
{domain}
{og?.title || "Page title"}
{og?.description || "Page description"}
); } function FacebookCard({ og, imageUrl, domain, }: { og: any; imageUrl: string | null; domain: string; }) { return (
{domain}
{og?.title || "Page title"}
{og?.description || "Page description"}
); } function LinkedInCard({ og, imageUrl, domain, }: { og: any; imageUrl: string | null; domain: string; }) { return (
{og?.title || "Page title"}
{domain}
); } function WhatsAppCard({ og, imageUrl, domain, }: { og: any; imageUrl: string | null; domain: string; }) { return (
{og?.title || "Page title not set"}
{og?.description || "No description"}
{domain}
); } const PLATFORMS = [ { label: "X / Twitter", color: "var(--card-fg-color)" }, { label: "Facebook", color: "#1877f2" }, { label: "LinkedIn", color: "#0a66c2" }, { label: "WhatsApp", color: "#25d366" }, ]; export default function SocialPreviewCard({ value: og }: Props) { const [activeTab, setActiveTab] = useState(0); const { projectId, dataset } = useWorkspace(); const config = getPluginConfig(); const baseUrl = config.baseUrl || "https://example.com"; const domain = baseUrl.replace(/^(https?:\/\/)?(www\.)?/, "").split("/")[0] || "example.com"; const imageUrl = og?.image?.asset ? getImageUrl(og.image.asset, projectId, dataset) : null; return (
SOCIAL SHARE PREVIEW
{PLATFORMS.map((p, i) => ( ))}
{activeTab === 0 && } {activeTab === 1 && } {activeTab === 2 && } {activeTab === 3 && }
Preview is approximate — actual appearance varies by platform.
); }