'use client'; import { FaqItem, Faqs } from '@/components/faq'; import { FeatureCardContainer } from '@/components/feature-card'; import { SectionHeader } from '@/components/section'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { cn } from '@/lib/utils'; import { AlertCircle, Building2, Globe, Loader2, MapPin, Network, Search, Server, } from 'lucide-react'; import { useEffect, useState } from 'react'; interface IPInfo { ip: string; location: { country: string | undefined; city: string | undefined; region: string | undefined; latitude: number | undefined; longitude: number | undefined; }; isp: string | null; asn: string | null; organization: string | null; hostname: string | null; isLocalhost: boolean; isPrivate: boolean; } export default function IPLookupPage() { const [ip, setIp] = useState(''); const [loading, setLoading] = useState(false); const [autoDetecting, setAutoDetecting] = useState(true); const [error, setError] = useState(null); const [isRateLimited, setIsRateLimited] = useState(false); const [result, setResult] = useState(null); // Auto-detect IP on page load useEffect(() => { const detectIP = async () => { setAutoDetecting(true); try { const response = await fetch('/api/tools/ip-lookup'); const data = await response.json(); if (!response.ok) { if (response.status === 429) { setIsRateLimited(true); throw new Error( 'Rate limit exceeded. Please wait a minute before trying again.', ); } setIsRateLimited(false); throw new Error(data.error || 'Failed to detect IP'); } setIsRateLimited(false); setResult(data); setIp(data.ip); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to detect IP'); } finally { setAutoDetecting(false); } }; detectIP(); }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!ip.trim()) return; setLoading(true); setError(null); setResult(null); try { const response = await fetch( `/api/tools/ip-lookup?ip=${encodeURIComponent(ip.trim())}`, ); const data = await response.json(); if (!response.ok) { if (response.status === 429) { setIsRateLimited(true); throw new Error( 'Rate limit exceeded. Please wait a minute before trying again.', ); } setIsRateLimited(false); throw new Error(data.error || 'Failed to lookup IP'); } setIsRateLimited(false); setResult(data); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setLoading(false); } }; const InfoCard = ({ icon, label, value, className, }: { icon: React.ReactNode; label: string; value: React.ReactNode; className?: string; }) => (
{icon}
{label}
{value || '—'}
); const getCountryFlag = (countryCode?: string): string => { if (!countryCode || countryCode.length !== 2) return '🌐'; // Convert country code to flag emoji const codePoints = countryCode .toUpperCase() .split('') .map((char) => 127397 + char.charCodeAt(0)); return String.fromCodePoint(...codePoints); }; return (
setIp(e.target.value)} className="flex-1" size="lg" />
{error && (
{error}
{isRateLimited && (
You can make up to 20 requests per minute. Please try again shortly.
)}
)} {result && (
{/* IP Address Display */}
{autoDetecting ? 'Detected IP Address' : 'IP Address'}
{result.ip}
{(result.isLocalhost || result.isPrivate) && (
{result.isLocalhost ? 'This is a localhost address' : 'This is a private IP address'}
)}
{/* Location Information */} {result.location.country && (

Location Information

} label="Country" value={ {getCountryFlag(result.location.country)} {result.location.country} } /> {result.location.city && ( } label="City" value={result.location.city} /> )} {result.location.region && ( } label="Region/State" value={result.location.region} /> )} {result.location.latitude && result.location.longitude && ( } label="Coordinates" value={`${result.location.latitude.toFixed(4)}, ${result.location.longitude.toFixed(4)}`} /> )}
)} {/* Network Information */} {(result.isp || result.asn || result.organization || result.hostname) && (

Network Information

{result.isp && ( } label="ISP" value={result.isp} /> )} {result.asn && ( } label="ASN" value={result.asn} /> )} {result.organization && ( } label="Organization" value={result.organization} /> )} {result.hostname && ( } label="Hostname" value={result.hostname} /> )}
)} {/* Map Preview */} {result.location.latitude && result.location.longitude && (

Map Location