import { useState } from 'react' import type { ProxyHost } from '../api/proxyHosts' import { useRemoteServers } from '../hooks/useRemoteServers' import { useDocker } from '../hooks/useDocker' interface ProxyHostFormProps { host?: ProxyHost onSubmit: (data: Partial) => Promise onCancel: () => void } export default function ProxyHostForm({ host, onSubmit, onCancel }: ProxyHostFormProps) { const [formData, setFormData] = useState({ domain_names: host?.domain_names || '', forward_scheme: host?.forward_scheme || 'http', forward_host: host?.forward_host || '', forward_port: host?.forward_port || 80, ssl_forced: host?.ssl_forced ?? false, http2_support: host?.http2_support ?? false, hsts_enabled: host?.hsts_enabled ?? false, hsts_subdomains: host?.hsts_subdomains ?? false, block_exploits: host?.block_exploits ?? true, websocket_support: host?.websocket_support ?? false, advanced_config: host?.advanced_config || '', enabled: host?.enabled ?? true, }) const { servers: remoteServers } = useRemoteServers() const [dockerHost, setDockerHost] = useState('') const [showDockerHost, setShowDockerHost] = useState(false) const { containers: dockerContainers, isLoading: dockerLoading, error: dockerError } = useDocker(dockerHost) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError(null) try { await onSubmit(formData) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to save proxy host') } finally { setLoading(false) } } const handleServerSelect = (serverUuid: string) => { const server = remoteServers.find(s => s.uuid === serverUuid) if (server) { setFormData({ ...formData, forward_host: server.host, forward_port: server.port, forward_scheme: 'http', }) } } const handleContainerSelect = (containerId: string) => { const container = dockerContainers.find(c => c.id === containerId) if (container) { // Prefer internal IP if available, otherwise use container name const host = container.ip || container.names[0] // Use the first exposed port if available, otherwise default to 80 const port = container.ports && container.ports.length > 0 ? container.ports[0].private_port : 80 setFormData({ ...formData, forward_host: host, forward_port: port, forward_scheme: 'http', }) } } return (

{host ? 'Edit Proxy Host' : 'Add Proxy Host'}

{error && (
{error}
)} {/* Domain Names */}
setFormData({ ...formData, domain_names: e.target.value })} placeholder="example.com, www.example.com" className="w-full bg-gray-900 border border-gray-700 rounded-lg px-4 py-2 text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Remote Server Quick Select */} {remoteServers.length > 0 && (
)} {/* Docker Container Quick Select */}
{showDockerHost && ( setDockerHost(e.target.value)} className="w-full bg-gray-900 border border-gray-700 rounded-lg px-4 py-2 text-white text-sm mb-2 focus:outline-none focus:ring-2 focus:ring-blue-500" /> )} {dockerError && (

Failed to connect: {(dockerError as Error).message}

)}
{/* Forward Details */}
setFormData({ ...formData, forward_host: e.target.value })} placeholder="192.168.1.100" className="w-full bg-gray-900 border border-gray-700 rounded-lg px-4 py-2 text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
setFormData({ ...formData, forward_port: parseInt(e.target.value) })} className="w-full bg-gray-900 border border-gray-700 rounded-lg px-4 py-2 text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* SSL & Security Options */}
{/* Advanced Config */}