'use client' import { useEffect, useRef, useState } from 'react' import { toPng } from 'html-to-image' import { toast } from 'react-hot-toast' import { EmbedModal } from './EmbedModal' interface ShareMenuProps { url: string title: string description?: string postId?: string } type ShareItem = { id: string label: string icon: React.ReactNode onClick: () => void dividerBefore?: boolean } /** Build a filesystem-safe filename from arbitrary text. */ function toFileSlug(text: string): string { const slug = text .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, '') .slice(0, 60) return slug || 'gitgram-share' } export function ShareMenu({ url, title, description, postId }: ShareMenuProps) { const [isOpen, setIsOpen] = useState(false) const [copied, setCopied] = useState(false) const [embedOpen, setEmbedOpen] = useState(false) const [isDownloading, setIsDownloading] = useState(false) const menuRef = useRef(null) const buttonRef = useRef(null) const captureRef = useRef(null) // Close on outside click useEffect(() => { if (!isOpen) return const handleOutsideClick = (event: MouseEvent) => { const target = event.target as Node if ( menuRef.current && !menuRef.current.contains(target) && buttonRef.current && !buttonRef.current.contains(target) ) { setIsOpen(false) } } document.addEventListener('mousedown', handleOutsideClick) return () => document.removeEventListener('mousedown', handleOutsideClick) }, [isOpen]) // Close on Escape useEffect(() => { if (!isOpen) return const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') setIsOpen(false) } document.addEventListener('keydown', handleKeyDown) return () => document.removeEventListener('keydown', handleKeyDown) }, [isOpen]) const handleCopyLink = async () => { try { await navigator.clipboard.writeText(url) setCopied(true) setTimeout(() => setCopied(false), 2000) } catch { // Fallback for older browsers const textarea = document.createElement('textarea') textarea.value = url document.body.appendChild(textarea) textarea.select() document.execCommand('copy') document.body.removeChild(textarea) setCopied(true) setTimeout(() => setCopied(false), 2000) } } const handleShareTwitter = () => { const encodedUrl = encodeURIComponent(url) const encodedText = encodeURIComponent(title) const twitterUrl = `https://twitter.com/intent/tweet?url=${encodedUrl}&text=${encodedText}` window.open(twitterUrl, '_blank', 'noopener,noreferrer,width=600,height=400') setIsOpen(false) } const handleShareLinkedIn = () => { const encodedUrl = encodeURIComponent(url) const encodedTitle = encodeURIComponent(title) const encodedDesc = encodeURIComponent(description ?? '') const linkedInUrl = `https://www.linkedin.com/sharing/share-offsite/?url=${encodedUrl}&title=${encodedTitle}&summary=${encodedDesc}` window.open(linkedInUrl, '_blank', 'noopener,noreferrer,width=600,height=500') setIsOpen(false) } const handleEmbed = () => { setIsOpen(false) setEmbedOpen(true) } const handleDownloadImage = async () => { if (!captureRef.current || isDownloading) return setIsDownloading(true) try { const dataUrl = await toPng(captureRef.current, { pixelRatio: 2, cacheBust: true, backgroundColor: '#0d1117', }) const link = document.createElement('a') link.download = `${toFileSlug(title)}.png` link.href = dataUrl document.body.appendChild(link) link.click() document.body.removeChild(link) toast.success('Image downloaded') setIsOpen(false) } catch { toast.error('Could not generate image. Please try again.') } finally { setIsDownloading(false) } } const menuItems: ShareItem[] = [ { id: 'copy-link', label: copied ? 'Copied!' : 'Copy link', icon: copied ? ( ) : ( ), onClick: handleCopyLink, }, { id: 'twitter', label: 'Share on Twitter / X', icon: ( ), onClick: handleShareTwitter, }, { id: 'linkedin', label: 'Share on LinkedIn', icon: ( ), onClick: handleShareLinkedIn, }, { id: 'embed', label: 'Embed', icon: ( ), onClick: handleEmbed, dividerBefore: true, }, { id: 'download', label: isDownloading ? 'Generating image…' : 'Download as image', icon: ( ), onClick: () => { void handleDownloadImage() }, }, ] return ( <>
{/* Trigger button */} {/* Dropdown */} {isOpen && (
{menuItems.map((item) => (
{item.dividerBefore && (
)}
))}
)}
{/* Embed modal — rendered outside dropdown so it's not clipped */} {postId && ( setEmbedOpen(false)} id={postId} /> )} {/* Off-screen share card. Rendered far off-screen (not display:none, which breaks html-to-image layout) so it can be captured to a PNG on demand. */} ) }