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8 |
+
Globe,
|
| 9 |
+
Sparkles,
|
| 10 |
+
Shield,
|
| 11 |
+
ChevronDown,
|
| 12 |
+
ChevronUp,
|
| 13 |
+
Loader2,
|
| 14 |
+
ExternalLink,
|
| 15 |
+
BarChart3,
|
| 16 |
+
Eye,
|
| 17 |
+
MousePointerClick
|
| 18 |
+
} from 'lucide-react'
|
| 19 |
+
|
| 20 |
+
export default function URLShortener() {
|
| 21 |
+
const [url, setUrl] = useState('')
|
| 22 |
+
const [customSlug, setCustomSlug] = useState('')
|
| 23 |
+
const [expiresAt, setExpiresAt] = useState('')
|
| 24 |
+
const [showAdvanced, setShowAdvanced] = useState(false)
|
| 25 |
+
const [result, setResult] = useState(null)
|
| 26 |
+
const [error, setError] = useState(null)
|
| 27 |
+
const [loading, setLoading] = useState(false)
|
| 28 |
+
const [copied, setCopied] = useState(false)
|
| 29 |
+
const [recentLinks, setRecentLinks] = useState([])
|
| 30 |
+
const [mounted, setMounted] = useState(false)
|
| 31 |
+
const [showWarning, setShowWarning] = useState(null)
|
| 32 |
+
|
| 33 |
+
useEffect(() => {
|
| 34 |
+
setMounted(true)
|
| 35 |
+
try {
|
| 36 |
+
const stored = localStorage.getItem('sh0rtl_recent_links')
|
| 37 |
+
if (stored) setRecentLinks(JSON.parse(stored))
|
| 38 |
+
} catch {}
|
| 39 |
+
}, [])
|
| 40 |
+
|
| 41 |
+
useEffect(() => {
|
| 42 |
+
if (recentLinks.length > 0) {
|
| 43 |
+
try {
|
| 44 |
+
localStorage.setItem('sh0rtl_recent_links', JSON.stringify(recentLinks.slice(0, 10)))
|
| 45 |
+
} catch {}
|
| 46 |
+
}
|
| 47 |
+
}, [recentLinks])
|
| 48 |
+
|
| 49 |
+
const handleSubmit = async (e) => {
|
| 50 |
+
e.preventDefault()
|
| 51 |
+
if (!url.trim()) return
|
| 52 |
+
|
| 53 |
+
setLoading(true)
|
| 54 |
+
setError(null)
|
| 55 |
+
setResult(null)
|
| 56 |
+
setShowWarning(null)
|
| 57 |
+
|
| 58 |
+
try {
|
| 59 |
+
const res = await fetch('/api/shorten', {
|
| 60 |
+
method: 'POST',
|
| 61 |
+
headers: { 'Content-Type': 'application/json' },
|
| 62 |
+
body: JSON.stringify({ url: url.trim(), customSlug: customSlug.trim() || undefined, expiresAt: expiresAt || undefined })
|
| 63 |
+
})
|
| 64 |
+
const data = await res.json()
|
| 65 |
+
|
| 66 |
+
if (!res.ok) {
|
| 67 |
+
setError(data.error || 'Failed to shorten URL')
|
| 68 |
+
setLoading(false)
|
| 69 |
+
return
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
if (data.warning) {
|
| 73 |
+
setShowWarning(data.warning)
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
setResult(data)
|
| 77 |
+
setRecentLinks(prev => [data, ...prev])
|
| 78 |
+
setUrl('')
|
| 79 |
+
setCustomSlug('')
|
| 80 |
+
setExpiresAt('')
|
| 81 |
+
} catch (err) {
|
| 82 |
+
setError('Network error. Please try again.')
|
| 83 |
+
} finally {
|
| 84 |
+
setLoading(false)
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
const copyToClipboard = async (text) => {
|
| 89 |
+
try {
|
| 90 |
+
await navigator.clipboard.writeText(text)
|
| 91 |
+
setCopied(true)
|
| 92 |
+
setTimeout(() => setCopied(false), 2000)
|
| 93 |
+
} catch {
|
| 94 |
+
setError('Failed to copy to clipboard')
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
const getHostname = () => {
|
| 99 |
+
if (typeof window !== 'undefined') return window.location.origin
|
| 100 |
+
return 'https://sh0rtl.ink'
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
if (!mounted) return null
|
| 104 |
+
|
| 105 |
+
return (
|
| 106 |
+
<section id="shorten" className="relative py-20">
|
| 107 |
+
<div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
|
| 108 |
+
<div className="text-center mb-12">
|
| 109 |
+
<h2 className="text-3xl sm:text-4xl font-bold mb-4">Shorten Your URL</h2>
|
| 110 |
+
<p className="text-slate-400">Paste a long URL and get a clean, trackable short link in seconds.</p>
|
| 111 |
+
</div>
|
| 112 |
+
|
| 113 |
+
<div className="bg-slate-900/80 backdrop-blur-sm border border-slate-800 rounded-2xl p-6 sm:p-8 shadow-2xl shadow-slate-950/50">
|
| 114 |
+
<form onSubmit={handleSubmit} className="space-y-5">
|
| 115 |
+
<div className="relative">
|
| 116 |
+
<div className="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
|
| 117 |
+
<Link className="w-5 h-5 text-slate-500" />
|
| 118 |
+
</div>
|
| 119 |
+
<input
|
| 120 |
+
type="url"
|
| 121 |
+
value={url}
|
| 122 |
+
onChange={(e) => setUrl(e.target.value)}
|
| 123 |
+
placeholder="Paste your long URL here..."
|
| 124 |
+
required
|
| 125 |
+
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"
|
| 126 |
+
/>
|
| 127 |
+
</div>
|
| 128 |
+
|
| 129 |
+
<button
|
| 130 |
+
type="button"
|
| 131 |
+
onClick={() => setShowAdvanced(!showAdvanced)}
|
| 132 |
+
className="flex items-center gap-2 text-sm text-slate-400 hover:text-emerald-400 transition-colors"
|
| 133 |
+
>
|
| 134 |
+
{showAdvanced ? <ChevronUp className="w-4 h-4" /> : <ChevronDown className="w-4 h-4" />}
|
| 135 |
+
Advanced Options
|
| 136 |
+
</button>
|
| 137 |
+
|
| 138 |
+
{showAdvanced && (
|
| 139 |
+
<div className="grid sm:grid-cols-2 gap-4 animate-slide-up">
|
| 140 |
+
<div>
|
| 141 |
+
<label className="block text-sm font-medium text-slate-400 mb-2">
|
| 142 |
+
Custom Slug <span className="text-slate-600">(optional)</span>
|
| 143 |
+
</label>
|
| 144 |
+
<div className="relative">
|
| 145 |
+
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
| 146 |
+
<Sparkles className="w-4 h-4 text-slate-500" />
|
| 147 |
+
</div>
|
| 148 |
+
<input
|
| 149 |
+
type="text"
|
| 150 |
+
value={customSlug}
|
| 151 |
+
onChange={(e) => setCustomSlug(e.target.value.replace(/[^a-zA-Z0-9_-]/g, ''))}
|
| 152 |
+
placeholder="my-link"
|
| 153 |
+
maxLength={32}
|
| 154 |
+
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"
|
| 155 |
+
/>
|
| 156 |
+
</div>
|
| 157 |
+
</div>
|
| 158 |
+
<div>
|
| 159 |
+
<label className="block text-sm font-medium text-slate-400 mb-2">
|
| 160 |
+
Expires At <span className="text-slate-600">(optional)</span>
|
| 161 |
+
</label>
|
| 162 |
+
<div className="relative">
|
| 163 |
+
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
| 164 |
+
<Clock className="w-4 h-4 text-slate-500" />
|
| 165 |
+
</div>
|
| 166 |
+
<input
|
| 167 |
+
type="datetime-local"
|
| 168 |
+
value={expiresAt}
|
| 169 |
+
onChange={(e) => setExpiresAt(e.target.value)}
|
| 170 |
+
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"
|
| 171 |
+
/>
|
| 172 |
+
</div>
|
| 173 |
+
</div>
|
| 174 |
+
</div>
|
| 175 |
+
)}
|
| 176 |
+
|
| 177 |
+
<button
|
| 178 |
+
type="submit"
|
| 179 |
+
disabled={loading || !url.trim()}
|
| 180 |
+
className="w-full py-4 rounded-xl bg-gradient-to-r from-emerald-600 to-teal-600 text-white font-semibold shadow-lg shadow-emerald-500/20 hover:shadow-emerald-500/40 disabled:opacity-50 disabled:cursor-not-allowed hover:scale-[1.01] transition-all flex items-center justify-center gap-2"
|
| 181 |
+
>
|
| 182 |
+
{loading ? (
|
| 183 |
+
<>
|
| 184 |
+
<Loader2 className="w-5 h-5 animate-spin" />
|
| 185 |
+
Shortening...
|
| 186 |
+
</>
|
| 187 |
+
) : (
|
| 188 |
+
<>
|
| 189 |
+
<Sparkles className="w-5 h-5" />
|
| 190 |
+
Shorten URL
|
| 191 |
+
</>
|
| 192 |
+
)}
|
| 193 |
+
</button>
|
| 194 |
+
</form>
|
| 195 |
+
|
| 196 |
+
{showWarning && (
|
| 197 |
+
<div className="mt-5 p-4 rounded-xl bg-amber-500/10 border border-amber-500/20 flex items-start gap-3 animate-fade-in">
|
| 198 |
+
<AlertTriangle className="w-5 h-5 text-amber-400 shrink-0 mt-0.5" />
|
| 199 |
+
<div>
|
| 200 |
+
<p className="text-sm font-medium text-amber-400">Security Warning</p>
|
| 201 |
+
<p className="text-sm text-amber-300/80 mt-1">{showWarning}</p>
|
| 202 |
+
</div>
|
| 203 |
+
</div>
|
| 204 |
+
)}
|
| 205 |
+
|
| 206 |
+
{error && (
|
| 207 |
+
<div className="mt-5 p-4 rounded-xl bg-red-500/10 border border-red-500/20 flex items-start gap-3 animate-fade-in">
|
| 208 |
+
<AlertTriangle className="w-5 h-5 text-red-400 shrink-0 mt-0.5" />
|
| 209 |
+
<div>
|
| 210 |
+
<p className="text-sm font-medium text-red-400">Error</p>
|
| 211 |
+
<p className="text-sm text-red-300/80 mt-1">{error}</p>
|
| 212 |
+
</div>
|
| 213 |
+
</div>
|
| 214 |
+
)}
|
| 215 |
+
|
| 216 |
+
{result && (
|
| 217 |
+
<div className="mt-5 p-5 rounded-xl bg-emerald-500/5 border border-emerald-500/20 animate-fade-in">
|
| 218 |
+
<div className="flex items-center gap-2 mb-3">
|
| 219 |
+
<Shield className="w-4 h-4 text-emerald-400" />
|
| 220 |
+
<span className="text-sm font-medium text-emerald-400">URL Shortened Successfully</span>
|
| 221 |
+
</div>
|
| 222 |
+
<div className="flex flex-col sm:flex-row items-stretch sm:items-center gap-3">
|
| 223 |
+
<div className="flex-1 min-w-0">
|
| 224 |
+
<p className="text-sm text-slate-500 mb-1">Short Link</p>
|
| 225 |
+
<a
|
| 226 |
+
href={result.shortUrl}
|
| 227 |
+
target="_blank"
|
| 228 |
+
rel="noopener noreferrer"
|
| 229 |
+
className="font-mono text-lg text-emerald-400 hover:text-emerald-300 transition-colors truncate block"
|
| 230 |
+
>
|
| 231 |
+
{result.shortUrl}
|
| 232 |
+
</a>
|
| 233 |
+
</div>
|
| 234 |
+
<button
|
| 235 |
+
onClick={() => copyToClipboard(result.shortUrl)}
|
| 236 |
+
className="shrink-0 px-4 py-2.5 rounded-lg bg-slate-800 hover:bg-slate-700 border border-slate-700 text-slate-300 transition-all flex items-center justify-center gap-2"
|
| 237 |
+
>
|
| 238 |
+
{copied ? <Check className="w-4 h-4 text-emerald-400" /> : <Copy className="w-4 h-4" />}
|
| 239 |
+
{copied ? 'Copied!' : 'Copy'}
|
| 240 |
+
</button>
|
| 241 |
+
</div>
|
| 242 |
+
<div className="mt-3 pt-3 border-t border-slate-800">
|
| 243 |
+
<p className="text-xs text-slate-500 truncate">
|
| 244 |
+
Original: <span className="text-slate-400">{result.originalUrl}</span>
|
| 245 |
+
</p>
|
| 246 |
+
</div>
|
| 247 |
+
</div>
|
| 248 |
+
)}
|
| 249 |
+
</div>
|
| 250 |
+
|
| 251 |
+
{recentLinks.length > 0 && (
|
| 252 |
+
<div className="mt-8">
|
| 253 |
+
<h3 className="text-lg font-semibold mb-4 flex items-center gap-2">
|
| 254 |
+
<Clock className="w-5 h-5 text-slate-400" />
|
| 255 |
+
Recent Links
|
| 256 |
+
</h3>
|
| 257 |
+
<div className="space-y-3">
|
| 258 |
+
{recentLinks.slice(0, 5).map((link, idx) => (
|
| 259 |
+
<div
|
| 260 |
+
key={link.slug + idx}
|
| 261 |
+
className="bg-slate-900/60 border border-slate-800 rounded-xl p-4 hover:border-slate-700 transition-colors group"
|
| 262 |
+
>
|
| 263 |
+
<div className="flex flex-col sm:flex-row sm:items-center gap-3">
|
| 264 |
+
<div className="flex-1 min-w-0">
|
| 265 |
+
<a
|
| 266 |
+
href={link.shortUrl}
|
| 267 |
+
target="_blank"
|
| 268 |
+
rel="noopener noreferrer"
|
| 269 |
+
className="font-mono text-sm text-emerald-400 hover:text-emerald-300 transition-colors flex items-center gap-1"
|
| 270 |
+
>
|
| 271 |
+
{link.shortUrl}
|
| 272 |
+
<ExternalLink className="w-3 h-3 opacity-0 group-hover:opacity-100 transition-opacity" />
|
| 273 |
+
</a>
|
| 274 |
+
<p className="text-xs text-slate-500 truncate mt-1">{link.originalUrl}</p>
|
| 275 |
+
</div>
|
| 276 |
+
<div className="flex items-center gap-4 shrink-0">
|
| 277 |
+
<div className="flex items-center gap-1.5 text-xs text-slate-400">
|
| 278 |
+
<MousePointerClick className="w-3.5 h-3.5" />
|
| 279 |
+
<span>{link.clicks} clicks</span>
|
| 280 |
+
</div>
|
| 281 |
+
{link.expiresAt && (
|
| 282 |
+
<div className="flex items-center gap-1.5 text-xs text-amber-400">
|
| 283 |
+
<Clock className="w-3.5 h-3.5" />
|
| 284 |
+
<span>Expires</span>
|
| 285 |
+
</div>
|
| 286 |
+
)}
|
| 287 |
+
<button
|
| 288 |
+
onClick={() => copyToClipboard(link.shortUrl)}
|
| 289 |
+
className="p-1.5 rounded-md hover:bg-slate-800 text-slate-500 hover:text-slate-300 transition-colors"
|
| 290 |
+
title="Copy link"
|
| 291 |
+
>
|
| 292 |
+
<Copy className="w-3.5 h-3.5" />
|
| 293 |
+
</button>
|
| 294 |
+
</div>
|
| 295 |
+
</div>
|
| 296 |
+
</div>
|
| 297 |
+
))}
|
| 298 |
+
</div>
|
| 299 |
+
</div>
|
| 300 |
+
)}
|
| 301 |
+
</div>
|
| 302 |
+
</section>
|
| 303 |
+
)
|
| 304 |
+
}
|