'use client'; import { useState, useRef, useEffect } from 'react'; import { FiShare2, FiLink, FiRefreshCw } from 'react-icons/fi'; import { FaTwitter, FaFacebook, FaLinkedin, FaReddit } from 'react-icons/fa'; import toast from 'react-hot-toast'; interface ShareButtonProps { badgeId: string | undefined; shareTitle: string; shareText: string; buttonText: string; className?: string; isPublic?: boolean; onTogglePrivacy?: () => Promise | void; } const ShareButton = ({ badgeId, shareTitle, shareText, buttonText, className, isPublic = true, onTogglePrivacy }: ShareButtonProps) => { const [isMenuOpen, setIsMenuOpen] = useState(false); const [isToggling, setIsToggling] = useState(false); const menuRef = useRef(null); const [isNativeShareSupported, setIsNativeShareSupported] = useState(false); const [url, setUrl] = useState(''); useEffect(() => { setIsNativeShareSupported(!!navigator.share); if (badgeId) { setUrl(`${window.location.origin}/share/badge/${badgeId}`); } }, [badgeId]); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(event.target as Node)) { setIsMenuOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [menuRef]); const handleButtonClick = async () => { if (!isPublic && onTogglePrivacy) { setIsToggling(true); try { await onTogglePrivacy(); setIsMenuOpen(true); } catch (error) { // The parent component is expected to show a toast. console.error("Failed to make badge public for sharing.", error); } finally { setIsToggling(false); } } else { setIsMenuOpen(!isMenuOpen); } }; const handleCopyLink = async () => { if (url) { try { await navigator.clipboard.writeText(url); toast.success('Link copied to clipboard!'); } catch (error) { console.error('Failed to copy link:', error); toast.error('Failed to copy link.'); } } setIsMenuOpen(false); }; const socialPlatforms = [ { name: 'Twitter', icon: FaTwitter, url: `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(shareText)}`, color: 'text-sky-500' }, { name: 'Facebook', icon: FaFacebook, url: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`, color: 'text-blue-600' }, { name: 'LinkedIn', icon: FaLinkedin, url: `https://www.linkedin.com/shareArticle?mini=true&url=${encodeURIComponent(url)}&title=${encodeURIComponent(shareTitle)}&summary=${encodeURIComponent(shareText)}`, color: 'text-sky-700' }, { name: 'Reddit', icon: FaReddit, url: `https://www.reddit.com/submit?url=${encodeURIComponent(url)}&title=${encodeURIComponent(shareTitle)}`, color: 'text-orange-500' } ]; const handleNativeShare = async () => { if (url && navigator.share) { const shareData = { title: shareTitle, text: shareText, url: url, }; try { await navigator.share(shareData); toast.success('Assessment shared successfully!'); } catch (error) { if (!(error instanceof Error && error.name === 'AbortError')) { console.error('Native share failed:', error); toast.error('Could not share.'); } } } else if (!navigator.share) { toast.error('Native sharing is not supported on this browser.'); } else { toast.error('Could not find a valid badge to share.'); } setIsMenuOpen(false); }; return (
{isMenuOpen && (
{socialPlatforms.map((platform) => { const Icon = platform.icon; return ( setIsMenuOpen(false)} className="flex items-center w-full px-4 py-2 text-sm hover:bg-black/5" style={{ color: 'var(--text-main)' }} > Share on {platform.name} ); })}
{isNativeShareSupported && ( <>
)}
)}
); }; export default ShareButton;