import { ShareIcon } from '@heroicons/react/24/solid' import { useRef } from 'react' import ChevronSVG from '#assets/chevron.svg' import ClipboardSVG from '#assets/share/clipboard.svg' import FacebookSVG from '#assets/share/facebook.svg' import LinkedinSVG from '#assets/share/linkedin.svg' import TelegramSVG from '#assets/share/telegram.svg' import TwitterSVG from '#assets/share/twitter.svg' import WhatsappSVG from '#assets/share/whatsapp.svg' import XingSVG from '#assets/share/xing.svg' import { useShareLogic } from './hooks' import type { Item } from '#types/Item' import type { PlatformConfig, SharePlatformConfigs } from './types' interface ShareButtonProps { item: Item dropdownDirection?: 'up' | 'down' } export function ShareButton({ item, dropdownDirection = 'down' }: ShareButtonProps) { const { shareUrl, shareTitle, copyLink, getShareUrl } = useShareLogic(item) const detailsRef = useRef(null) const closeDropdown = () => { if (detailsRef.current) { detailsRef.current.open = false } } const handleCopyLink = () => { copyLink() closeDropdown() } const canUseNativeShare = typeof navigator !== 'undefined' && typeof navigator.share !== 'undefined' const handleNativeShare = () => { void navigator .share({ title: shareTitle, url: shareUrl, }) .then(closeDropdown) .catch(() => { // User cancelled or error occurred - ignore }) } const platformConfigs: SharePlatformConfigs = { facebook: { shareUrl: 'https://www.facebook.com/sharer/sharer.php?u={url}', icon: Facebook, label: 'Facebook', bgColor: '#3b5998', }, twitter: { shareUrl: 'https://twitter.com/intent/tweet?text={title}:%20{url}', icon: Twitter, label: 'Twitter', bgColor: '#55acee', }, linkedin: { shareUrl: 'http://www.linkedin.com/shareArticle?mini=true&url={url}&title={title}', icon: Linkedin, label: 'LinkedIn', bgColor: '#4875b4', }, whatsapp: { shareUrl: 'https://api.whatsapp.com/send?text={title}%20{url}', icon: Whatsapp, label: 'WhatsApp', bgColor: '#25D366', }, telegram: { shareUrl: 'https://t.me/share/url?url={url}&text={title}', icon: Telegram, label: 'Telegram', bgColor: '#0088cc', }, xing: { shareUrl: 'https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url={url}', icon: Xing, label: 'Xing', bgColor: '#026466', }, } const dropdownClass = dropdownDirection === 'up' ? 'tw:dropdown-top' : '' // If native share is available, render a simple button instead of dropdown if (canUseNativeShare) { return ( ) } // Otherwise, render the dropdown with manual share options return (
) }