'use client'; import { useState } from 'react'; interface URLFormProps { onSubmit: (url: string) => void; disabled: boolean; } export default function URLForm({ onSubmit, disabled }: URLFormProps) { // Get example URL from environment variables or use a default const exampleUrl = process.env.NEXT_PUBLIC_EXAMPLE_URL || 'https://example.com'; const [url, setUrl] = useState(''); const [error, setError] = useState(''); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // Trim the URL to remove any whitespace const trimmedUrl = url.trim(); if (!trimmedUrl) { setError('Please enter a URL'); return; } // Validate URL format try { const parsedUrl = new URL(trimmedUrl); // Check for protocol if (!parsedUrl.protocol.startsWith('http')) { setError('URL must use HTTP or HTTPS protocol'); return; } // Check for hostname if (!parsedUrl.hostname || parsedUrl.hostname.length < 3) { setError('URL must contain a valid hostname'); return; } // Basic sanity check for common invalid URLs if (parsedUrl.href === 'http://localhost' || parsedUrl.href === 'https://localhost') { setError('Please enter a valid public URL, not localhost'); return; } // All validations passed setError(''); onSubmit(trimmedUrl); } catch (error) { console.error(error); setError('Please enter a valid URL (e.g., https://example.com)'); } }; return (
setUrl(e.target.value)} placeholder={exampleUrl} className="w-full p-3 rounded-md transition-colors" style={{ background: 'var(--card-bg)', border: '1px solid var(--input-border)', color: 'var(--foreground)', opacity: disabled ? '0.6' : '1' }} disabled={disabled} />
{url && ( )}
{error && (

{error}

)}
); }