| 1 | 'use client'; |
| 2 | |
| 3 | import { useState, useEffect, useCallback } from 'react'; |
| 4 | import { motion, AnimatePresence } from 'framer-motion'; |
| 5 | import { Share2, Copy, Check, Link2, X, Globe, MapPin } from 'lucide-react'; |
| 6 | |
| 7 | interface SharePanelProps { |
| 8 | mapView: { zoom: number; latitude: number; longitude?: number }; |
| 9 | activeLayers: Record<string, boolean>; |
| 10 | mouseCoords?: { lat: number; lng: number } | null; |
| 11 | } |
| 12 | |
| 13 | export default function SharePanel({ mapView, activeLayers, mouseCoords }: SharePanelProps) { |
| 14 | const [isOpen, setIsOpen] = useState(false); |
| 15 | const [copied, setCopied] = useState(false); |
| 16 | |
| 17 | const generateShareUrl = useCallback(() => { |
| 18 | const params = new URLSearchParams(); |
| 19 | const lat = mouseCoords?.lat ?? mapView.latitude ?? 20; |
| 20 | const lng = mouseCoords?.lng ?? mapView.longitude ?? 0; |
| 21 | params.set('lat', lat.toFixed(4)); |
| 22 | params.set('lon', lng.toFixed(4)); |
| 23 | params.set('zoom', mapView.zoom.toFixed(2)); |
| 24 | |
| 25 | // Encode active layers as compact string |
| 26 | const layerKeys = Object.entries(activeLayers) |
| 27 | .filter(([, v]) => v) |
| 28 | .map(([k]) => k) |
| 29 | .join(','); |
| 30 | if (layerKeys) params.set('layers', layerKeys); |
| 31 | |
| 32 | const base = typeof window !== 'undefined' ? window.location.origin : 'https://osiris.vercel.app'; |
| 33 | return `${base}/?${params.toString()}`; |
| 34 | }, [mapView, activeLayers, mouseCoords]); |
| 35 | |
| 36 | const copyToClipboard = useCallback(async () => { |
| 37 | const url = generateShareUrl(); |
| 38 | try { |
| 39 | await navigator.clipboard.writeText(url); |
| 40 | setCopied(true); |
| 41 | setTimeout(() => setCopied(false), 2000); |
| 42 | } catch { |
| 43 | // Fallback |
| 44 | const input = document.createElement('input'); |
| 45 | input.value = url; |
| 46 | document.body.appendChild(input); |
| 47 | input.select(); |
| 48 | document.execCommand('copy'); |
| 49 | document.body.removeChild(input); |
| 50 | setCopied(true); |
| 51 | setTimeout(() => setCopied(false), 2000); |
| 52 | } |
| 53 | }, [generateShareUrl]); |
| 54 | |
| 55 | // Keyboard shortcut |
| 56 | useEffect(() => { |
| 57 | const handler = (e: KeyboardEvent) => { |
| 58 | if (e.key === 's' && !e.ctrlKey && !e.metaKey && !['INPUT', 'TEXTAREA'].includes((e.target as Element)?.tagName)) { |
| 59 | setIsOpen(p => !p); |
| 60 | } |
| 61 | }; |
| 62 | window.addEventListener('keydown', handler); |
| 63 | return () => window.removeEventListener('keydown', handler); |
| 64 | }, []); |
| 65 | |
| 66 | return ( |
| 67 | <> |
| 68 | {/* Share Button */} |
| 69 | <motion.button |
| 70 | onClick={() => setIsOpen(!isOpen)} |
| 71 | whileHover={{ scale: 1.1 }} |
| 72 | whileTap={{ scale: 0.95 }} |
| 73 | className="glass-panel w-8 h-8 flex items-center justify-center pointer-events-auto hover:border-[var(--gold-primary)] transition-colors" |
| 74 | title="Share view (S)" |
| 75 | > |
| 76 | <Share2 className="w-3.5 h-3.5 text-[var(--gold-primary)]" /> |
| 77 | </motion.button> |
| 78 | |
| 79 | {/* Share Panel */} |
| 80 | <AnimatePresence> |
| 81 | {isOpen && ( |
| 82 | <motion.div |
| 83 | initial={{ opacity: 0, y: -10, scale: 0.95 }} |
| 84 | animate={{ opacity: 1, y: 0, scale: 1 }} |
| 85 | exit={{ opacity: 0, y: -10, scale: 0.95 }} |
| 86 | className="absolute top-12 right-0 w-72 glass-panel p-4 pointer-events-auto osiris-glow z-[300]" |
| 87 | > |
| 88 | <div className="flex items-center justify-between mb-3"> |
| 89 | <div className="flex items-center gap-2"> |
| 90 | <Globe className="w-3.5 h-3.5 text-[var(--gold-primary)]" /> |
| 91 | <span className="hud-text text-[10px] text-[var(--text-primary)]">SHARE VIEW</span> |
| 92 | </div> |
| 93 | <button onClick={() => setIsOpen(false)} className="text-[var(--text-muted)] hover:text-[var(--text-primary)]"> |
| 94 | <X className="w-3 h-3" /> |
| 95 | </button> |
| 96 | </div> |
| 97 | |
| 98 | {/* Current View Info */} |
| 99 | <div className="mb-3 p-2 rounded-lg bg-[var(--bg-void)] border border-[var(--border-primary)]"> |
| 100 | <div className="flex items-center gap-1.5 mb-1"> |
| 101 | <MapPin className="w-2.5 h-2.5 text-[var(--gold-primary)]" /> |
| 102 | <span className="text-[7px] font-mono text-[var(--text-muted)] tracking-widest">CURRENT VIEW</span> |
| 103 | </div> |
| 104 | <div className="text-[8px] font-mono text-[var(--text-secondary)]"> |
| 105 | {mouseCoords ? `${mouseCoords.lat.toFixed(4)}°, ${mouseCoords.lng.toFixed(4)}°` : '—'} · Zoom {mapView.zoom.toFixed(1)} |
| 106 | </div> |
| 107 | <div className="text-[7px] font-mono text-[var(--text-muted)] mt-1"> |
| 108 | {Object.values(activeLayers).filter(Boolean).length} layers active |
| 109 | </div> |
| 110 | </div> |
| 111 | |
| 112 | {/* Share URL */} |
| 113 | <div className="mb-3"> |
| 114 | <div className="flex items-center gap-1.5 mb-1"> |
| 115 | <Link2 className="w-2.5 h-2.5 text-[var(--text-muted)]" /> |
| 116 | <span className="text-[7px] font-mono text-[var(--text-muted)] tracking-widest">SHAREABLE LINK</span> |
| 117 | </div> |
| 118 | <div className="flex gap-1.5"> |
| 119 | <div className="flex-1 p-1.5 rounded bg-[var(--bg-void)] border border-[var(--border-primary)] text-[7px] font-mono text-[var(--gold-primary)] truncate"> |
| 120 | {generateShareUrl()} |
| 121 | </div> |
| 122 | <button |
| 123 | onClick={copyToClipboard} |
| 124 | className={`px-3 py-1.5 rounded text-[7px] font-mono tracking-widest transition-all ${copied ? 'bg-[var(--alert-green)]/20 text-[var(--alert-green)] border border-[var(--alert-green)]/30' : 'bg-[var(--gold-primary)]/10 text-[var(--gold-primary)] border border-[var(--gold-primary)]/30 hover:bg-[var(--gold-primary)]/20'}`} |
| 125 | > |
| 126 | {copied ? <Check className="w-3 h-3" /> : <Copy className="w-3 h-3" />} |
| 127 | </button> |
| 128 | </div> |
| 129 | </div> |
| 130 | |
| 131 | {/* Quick Share */} |
| 132 | <div className="flex gap-2"> |
| 133 | <a |
| 134 | href={`https://twitter.com/intent/tweet?text=${encodeURIComponent('🏛️ OSIRIS — Global Intelligence Dashboard')}&url=${encodeURIComponent(generateShareUrl())}`} |
| 135 | target="_blank" |
| 136 | className="flex-1 text-center py-1.5 rounded text-[7px] font-mono tracking-wider text-[var(--text-muted)] border border-[var(--border-primary)] hover:border-[#1DA1F2] hover:text-[#1DA1F2] transition-colors" |
| 137 | > |
| 138 | 𝕏 POST |
| 139 | </a> |
| 140 | <a |
| 141 | href={`https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(generateShareUrl())}`} |
| 142 | target="_blank" |
| 143 | className="flex-1 text-center py-1.5 rounded text-[7px] font-mono tracking-wider text-[var(--text-muted)] border border-[var(--border-primary)] hover:border-[#0A66C2] hover:text-[#0A66C2] transition-colors" |
| 144 | > |
| 145 | IN SHARE |
| 146 | </a> |
| 147 | <a |
| 148 | href={`https://reddit.com/submit?url=${encodeURIComponent(generateShareUrl())}&title=${encodeURIComponent('OSIRIS — Open Source Global Intelligence Platform')}`} |
| 149 | target="_blank" |
| 150 | className="flex-1 text-center py-1.5 rounded text-[7px] font-mono tracking-wider text-[var(--text-muted)] border border-[var(--border-primary)] hover:border-[#FF4500] hover:text-[#FF4500] transition-colors" |
| 151 | > |
| 152 | REDDIT |
| 153 | </a> |
| 154 | </div> |
| 155 | |
| 156 | <div className="mt-3 text-center text-[6px] font-mono text-[var(--text-muted)] tracking-widest"> |
| 157 | PRESS [S] TO TOGGLE · SHAREABLE LINKS PRESERVE VIEW STATE |
| 158 | </div> |
| 159 | </motion.div> |
| 160 | )} |
| 161 | </AnimatePresence> |
| 162 | </> |
| 163 | ); |
| 164 | } |