import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Copy, Mail, MessageCircle, Send, Share2, Link as LinkIcon } from 'lucide-react'; import { trackEvent } from '@/services/analytics'; type ShareVariant = 'page' | 'result'; interface SharePanelProps { title: string; text: string; url: string; variant?: ShareVariant; className?: string; } interface ShareTarget { key: string; label: string; href: string; } function openShareWindow(url: string) { window.open(url, '_blank', 'noopener,noreferrer'); } export default function SharePanel({ title, text, url, variant = 'page', className = '', }: SharePanelProps) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [copied, setCopied] = useState(false); const canNativeShare = typeof navigator !== 'undefined' && typeof navigator.share === 'function'; if (!url) return null; const encodedUrl = encodeURIComponent(url); const encodedTitle = encodeURIComponent(title); const encodedText = encodeURIComponent(text); const targets: ShareTarget[] = [ { key: 'whatsapp', label: t('share.targets.whatsapp'), href: `https://wa.me/?text=${encodeURIComponent(`${title}\n${url}`)}`, }, { key: 'facebook', label: t('share.targets.facebook'), href: `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}`, }, { key: 'telegram', label: t('share.targets.telegram'), href: `https://t.me/share/url?url=${encodedUrl}&text=${encodedTitle}`, }, { key: 'x', label: t('share.targets.x'), href: `https://twitter.com/intent/tweet?url=${encodedUrl}&text=${encodedTitle}`, }, { key: 'linkedin', label: t('share.targets.linkedin'), href: `https://www.linkedin.com/sharing/share-offsite/?url=${encodedUrl}`, }, { key: 'email', label: t('share.targets.email'), href: `mailto:?subject=${encodedTitle}&body=${encodedText}%0A%0A${encodedUrl}`, }, ]; const handleNativeShare = async () => { if (!canNativeShare) return; try { await navigator.share({ title, text, url }); trackEvent('share_clicked', { variant, target: 'native' }); } catch { // Ignore cancelation and rely on the fallback actions below. } }; const handleCopy = async () => { try { await navigator.clipboard.writeText(url); setCopied(true); window.setTimeout(() => setCopied(false), 1800); trackEvent('share_clicked', { variant, target: 'copy' }); } catch { setCopied(false); } }; return (
{variant === 'result' ? t('share.resultLabel') : t('share.toolLabel')}
{title}
{text}
{t('share.note')}