import { useState, useEffect } from 'react' import { Link, Copy, Check, AlertTriangle, Clock, Globe, Sparkles, Shield, ChevronDown, ChevronUp, Loader2, ExternalLink, BarChart3, Eye, MousePointerClick } from 'lucide-react' export default function URLShortener() { const [url, setUrl] = useState('') const [customSlug, setCustomSlug] = useState('') const [expiresAt, setExpiresAt] = useState('') const [showAdvanced, setShowAdvanced] = useState(false) const [result, setResult] = useState(null) const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const [copied, setCopied] = useState(false) const [recentLinks, setRecentLinks] = useState([]) const [mounted, setMounted] = useState(false) const [showWarning, setShowWarning] = useState(null) useEffect(() => { setMounted(true) try { const stored = localStorage.getItem('sh0rtl_recent_links') if (stored) setRecentLinks(JSON.parse(stored)) } catch {} }, []) useEffect(() => { if (recentLinks.length > 0) { try { localStorage.setItem('sh0rtl_recent_links', JSON.stringify(recentLinks.slice(0, 10))) } catch {} } }, [recentLinks]) const handleSubmit = async (e) => { e.preventDefault() if (!url.trim()) return setLoading(true) setError(null) setResult(null) setShowWarning(null) try { const res = await fetch('/api/shorten', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: url.trim(), customSlug: customSlug.trim() || undefined, expiresAt: expiresAt || undefined }) }) const data = await res.json() if (!res.ok) { setError(data.error || 'Failed to shorten URL') setLoading(false) return } if (data.warning) { setShowWarning(data.warning) } setResult(data) setRecentLinks(prev => [data, ...prev]) setUrl('') setCustomSlug('') setExpiresAt('') } catch (err) { setError('Network error. Please try again.') } finally { setLoading(false) } } const copyToClipboard = async (text) => { try { await navigator.clipboard.writeText(text) setCopied(true) setTimeout(() => setCopied(false), 2000) } catch { setError('Failed to copy to clipboard') } } const getHostname = () => { if (typeof window !== 'undefined') return window.location.origin return 'https://sh0rtl.ink' } if (!mounted) return null return (

Shorten Your URL

Paste a long URL and get a clean, trackable short link in seconds.

setUrl(e.target.value)} placeholder="Paste your long URL here..." required className="w-full pl-12 pr-4 py-4 bg-slate-950 border border-slate-700 rounded-xl text-slate-100 placeholder-slate-500 focus:outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500/50 transition-all" />
{showAdvanced && (
setCustomSlug(e.target.value.replace(/[^a-zA-Z0-9_-]/g, ''))} placeholder="my-link" maxLength={32} className="w-full pl-10 pr-4 py-3 bg-slate-950 border border-slate-700 rounded-xl text-slate-100 placeholder-slate-600 focus:outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500/50 transition-all" />
setExpiresAt(e.target.value)} className="w-full pl-10 pr-4 py-3 bg-slate-950 border border-slate-700 rounded-xl text-slate-100 focus:outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500/50 transition-all" />
)}
{showWarning && (

Security Warning

{showWarning}

)} {error && (

Error

{error}

)} {result && (
URL Shortened Successfully

Original: {result.originalUrl}

)}
{recentLinks.length > 0 && (

Recent Links

{recentLinks.slice(0, 5).map((link, idx) => (
{link.shortUrl}

{link.originalUrl}

{link.clicks} clicks
{link.expiresAt && (
Expires
)}
))}
)}
) }