import React, { useState, useEffect } from 'react'; import { FacebookLogoIcon, XLogoIcon, LinkedinLogoIcon, LinkSimpleIcon, WhatsappLogoIcon, RedditLogoIcon, } from '@phosphor-icons/react'; import { useToast } from '../hooks/useToast'; const ShareButtons = ({ title, url }) => { const { addToast } = useToast(); const [isMobile, setIsMobile] = useState(false); useEffect(() => { const userAgent = typeof window.navigator === 'undefined' ? '' : navigator.userAgent; const mobile = Boolean( userAgent.match( /Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i, ), ); setIsMobile(mobile); }, []); // Fallback for copying text to clipboard when navigator.clipboard is not available const fallbackCopyTextToClipboard = (text) => { const textArea = document.createElement('textarea'); textArea.value = text; textArea.style.position = 'fixed'; // Make it invisible textArea.style.left = '-9999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { document.execCommand('copy'); addToast({ title: 'Success', message: 'Link copied to clipboard!', duration: 3000, }); } catch (err) { addToast({ title: 'Error', message: 'Failed to copy link!', duration: 3000, }); console.error('Fallback: Could not copy text: ', err); } document.body.removeChild(textArea); }; const handleShare = (platformUrl, platformName) => { console.log(`Attempting to share on ${platformName}:`, { title, url, platformUrl, }); if (navigator.share && isMobile) { // Only use Web Share API on mobile console.log(`Web Share API available for ${platformName} on mobile.`); navigator .share({ title: title, text: title, url: url, }) .then(() => { console.log( `Shared successfully via Web Share API on ${platformName}`, ); }) .catch((error) => { console.error( `Error sharing via Web Share API on ${platformName}:`, error, ); // Fallback to platform-specific sharing if Web Share API fails or is cancelled console.log( `Web Share API failed or cancelled, falling back for ${platformName}`, ); if (platformName === 'Copy Link') { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(url).then( () => { addToast({ title: 'Success', message: 'Link copied to clipboard!', duration: 3000, }); }, (err) => { addToast({ title: 'Error', message: 'Failed to copy link!', duration: 3000, }); console.error('Async: Could not copy text: ', err); }, ); } else { fallbackCopyTextToClipboard(url); } } else { window.open(platformUrl, '_blank'); } }); } else { // Fallback for browsers that do not support Web Share API or on desktop console.log( `Web Share API not supported or on desktop, falling back for ${platformName}`, ); if (platformName === 'Copy Link') { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(url).then( () => { addToast({ title: 'Success', message: 'Link copied to clipboard!', duration: 3000, }); }, (err) => { addToast({ title: 'Error', message: 'Failed to copy link!', duration: 3000, }); console.error('Async: Could not copy text: ', err); }, ); } else { fallbackCopyTextToClipboard(url); } } else { window.open(platformUrl, '_blank'); } } }; return (
Share: {isMobile ? ( ) : ( <> )}
); }; export default ShareButtons;