'use client'; import { useState } from 'react'; interface ShareButtonsProps { title: string; description: string; url?: string; } export default function ShareButtons({ title, description, url }: ShareButtonsProps) { const [showShareMenu, setShowShareMenu] = useState(false); const shareUrl = url || (typeof window !== 'undefined' ? window.location.href : ''); // localhost 체크 const isLocalhost = shareUrl.includes('localhost') || shareUrl.includes('127.0.0.1'); const handleKakaoShare = () => { if (isLocalhost) { // localhost인 경우 텍스트로 공유 const shareText = `${title}\n\n${description}\n\n🔮 사주보기 - 쟁승메이드\n(배포 후 링크가 제공됩니다)`; if (typeof window !== 'undefined' && (window as any).Kakao) { (window as any).Kakao.Share.sendDefault({ objectType: 'text', text: shareText, link: { mobileWebUrl: 'https://jaengseung-made.com', webUrl: 'https://jaengseung-made.com', }, }); } else { alert('카카오톡 공유 기능을 사용할 수 없습니다.'); } } else { // 배포된 URL인 경우 정상 공유 if (typeof window !== 'undefined' && (window as any).Kakao) { (window as any).Kakao.Share.sendDefault({ objectType: 'feed', content: { title: title, description: description, imageUrl: 'https://developers.kakao.com/assets/img/about/logos/kakaolink/kakaolink_btn_medium.png', link: { mobileWebUrl: shareUrl, webUrl: shareUrl, }, }, buttons: [ { title: '자세히 보기', link: { mobileWebUrl: shareUrl, webUrl: shareUrl, }, }, ], }); } else { alert('카카오톡 공유 기능을 사용할 수 없습니다.'); } } }; const handleFacebookShare = () => { const facebookUrl = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(shareUrl)}`; window.open(facebookUrl, '_blank', 'width=600,height=400'); }; const handleTwitterShare = () => { const twitterUrl = `https://twitter.com/intent/tweet?text=${encodeURIComponent(title)}&url=${encodeURIComponent(shareUrl)}`; window.open(twitterUrl, '_blank', 'width=600,height=400'); }; const handleCopyLink = async () => { try { if (isLocalhost) { // localhost인 경우 텍스트 정보 복사 const shareText = `${title}\n\n${description}\n\n🔮 사주보기 - 쟁승메이드\n(배포 후 링크가 제공됩니다)`; await navigator.clipboard.writeText(shareText); alert('사주 정보가 복사되었습니다!\n(개발 환경이므로 URL 대신 텍스트 정보가 복사됩니다)'); } else { await navigator.clipboard.writeText(shareUrl); alert('링크가 복사되었습니다!'); } setShowShareMenu(false); } catch (err) { alert('복사에 실패했습니다.'); } }; const handleNativeShare = async () => { if (navigator.share) { try { await navigator.share({ title: title, text: description, url: shareUrl, }); setShowShareMenu(false); } catch (err) { console.log('Share cancelled or failed', err); } } else { setShowShareMenu(true); } }; return (
{/* 공유 메뉴 (모바일에서 네이티브 공유가 안 될 때) */} {showShareMenu && ( <> {/* 배경 오버레이 */}
setShowShareMenu(false)} >
{/* 공유 메뉴 */}

공유하기

{/* 카카오톡 */} {/* 페이스북 */} {/* 트위터 */} {/* 링크 복사 */}
)}
); }