"use client"; import * as React from "react"; import { Modal } from "./Modal"; import { Copy, Check, Share2, Download } from "lucide-react"; import { useState, useEffect } from "react"; import IconBlack from "../assets/logo/Icon-Black-Transparent.svg"; interface ShareModalProps { isOpen: boolean; onClose: () => void; url: string; qrCodeData?: string; title?: string; description?: string; diagramImage?: string; } export function ShareModal({ isOpen, onClose, url, qrCodeData, title, description, diagramImage, }: ShareModalProps) { const [copied, setCopied] = useState(false); const [imagePreview, setImagePreview] = useState(null); // Open Graph data for global article sharing const [ogData, setOgData] = useState<{ image?: string, title?: string, description?: string } | null>(null); const [timestamp] = useState( new Date().toLocaleTimeString("de-DE", { hour: "2-digit", minute: "2-digit", second: "2-digit", }), ); useEffect(() => { // Only extract OG data if we are sharing the global page (no specific diagram or QR) if (isOpen && !diagramImage && !qrCodeData && typeof document !== "undefined") { const ogImage = document.querySelector('meta[property="og:image"]')?.getAttribute("content") || undefined; const ogTitle = document.querySelector('meta[property="og:title"]')?.getAttribute("content") || title; const ogDesc = document.querySelector('meta[property="og:description"]')?.getAttribute("content") || undefined; if (ogImage || ogTitle || ogDesc) { setOgData({ image: ogImage, title: ogTitle, description: ogDesc }); } } else { // Force clear OG data if we HAVE a diagramImage, to prevent fallback flashes setOgData(null); } }, [isOpen, diagramImage, qrCodeData, title]); useEffect(() => { if (diagramImage && isOpen) { const isDataUrl = diagramImage.startsWith("data:image/"); if (isDataUrl) { // If it's already a Data URL (e.g. from html-to-image), we can display it immediately setImagePreview(diagramImage); } else { // It's probably an SVG string, convert to Data URL const svgBlob = new Blob([diagramImage], { type: "image/svg+xml;charset=utf-8" }); const svgUrl = URL.createObjectURL(svgBlob); setImagePreview(svgUrl); } } }, [diagramImage, isOpen]); const handleCopy = () => { navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const handleNativeShare = async () => { if (navigator.share) { try { const shareData: ShareData = { title: title || "Mintel Architektur Insights", text: description || "Schauen Sie sich diesen Beitrag an.", url: url, }; // If we have a diagram image, try to include it as a file if (imagePreview) { try { // Convert base64 preview back to a File object const response = await fetch(imagePreview); const blob = await response.blob(); const file = new File( [blob], `${title?.replace(/\s+/g, "-").toLowerCase() || "diagram"}.png`, { type: "image/png" }, ); // Check if sharing files is supported if (navigator.canShare && navigator.canShare({ files: [file] })) { shareData.files = [file]; } } catch (fileError) { console.error("Could not prepare file for sharing", fileError); } } await navigator.share(shareData); } catch (_e) { console.error("Share failed", _e); } } }; const handleDownloadImage = () => { if (!imagePreview) return; const link = document.createElement("a"); link.download = `${title?.replace(/\s+/g, "-").toLowerCase() || "diagram"}.png`; link.href = imagePreview; link.click(); }; const handleShareX = () => { const text = encodeURIComponent(`${description ? description + " " : ""}${title || "Mintel Architektur Insights"}`); const shareUrl = `https://twitter.com/intent/tweet?text=${text}&url=${encodeURIComponent(url)}`; window.open(shareUrl, "_blank", "width=550,height=420"); }; const handleShareLinkedIn = () => { // LinkedIn share doesn't accept text directly via URL for personal profiles easily, but we pass url const shareUrl = `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`; window.open(shareUrl, "_blank", "width=550,height=420"); }; const modalTitle = diagramImage ? "Diagramm teilen" : title ? "Artikel teilen" : "Konfiguration teilen"; return (
{imagePreview ? (
{/* Social Post Preview Section */}
{/* Industrial Blueprint Preview */}
{title
SYS:{" "} {Math.random() .toString(36) .substring(7) .toUpperCase()} MINTEL.ME
{timestamp}

Diagramm Vorschau

) : ( title && !ogData ? (

"{title}"

) : ogData && (
{ogData.image && (
{ogData.title
)}

{ogData.title || title}

{ogData.description && (

{ogData.description}

)}
MINTEL.ME
) )} {!diagramImage && qrCodeData && (
QR Code

QR-Code scannen

)}
{typeof navigator !== "undefined" && !!navigator.share && ( )}
{imagePreview && ( )}
); }