import { useState } from 'react'; import type { Article } from '../api/articles'; interface ShareButtonProps { article: Article; className?: string; } export function ShareButton({ article, className = "" }: ShareButtonProps) { const [isOpen, setIsOpen] = useState(false); const [copied, setCopied] = useState(false); const shareData = { title: article.title, text: article.summary ? article.summary.substring(0, 100) + '...' : article.title, url: article.url, }; const handleShare = async () => { // Try Native Share API first (Mobile & Supported Browsers) if (navigator.share) { try { await navigator.share(shareData); return; } catch (err) { console.log('Error sharing:', err); // If user cancelled, do nothing. If error, maybe fallback? // Usually if navigator.share exists, we trust it unless it throws. // But on some desktops it might exist but fail. } } // Fallback to custom menu setIsOpen(!isOpen); }; const copyToClipboard = () => { navigator.clipboard.writeText(article.url); setCopied(true); setTimeout(() => setCopied(false), 2000); setIsOpen(false); }; const openLink = (url: string) => { window.open(url, '_blank', 'noopener,noreferrer'); setIsOpen(false); }; return (
{/* Desktop Fallback Menu */} {isOpen && ( <>
setIsOpen(false)}>
)}
); }