"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; diagramImage?: string; } export function ShareModal({ isOpen, onClose, url, qrCodeData, title, diagramImage, }: ShareModalProps) { const [copied, setCopied] = useState(false); const [imagePreview, setImagePreview] = useState(null); const [timestamp] = useState( new Date().toLocaleTimeString("de-DE", { hour: "2-digit", minute: "2-digit", second: "2-digit", }), ); 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); } // Optional: If we want to strictly apply the watermark via Canvas, we would do it here. // But for the sake of getting the preview to work reliably first, just setting the imagePreview // directly to the data URL or SVG blob URL is the safest approach. The watermark logic was // likely failing because `IconBlack` wasn't resolving correctly or `img.onload` wasn't firing // properly in all environments. } }, [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 Diagramm", 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(title || "Mintel Diagramm"); const shareUrl = `https://twitter.com/intent/tweet?text=${text}&url=${encodeURIComponent(url)}`; window.open(shareUrl, "_blank", "width=550,height=420"); }; const handleShareLinkedIn = () => { 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 && (

"{title}"

) )} {!diagramImage && qrCodeData && (
QR Code

QR-Code scannen

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