import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { MinusCircle, Plus } from "lucide-react"; import { useState } from "react"; type UpstreamEntry = { protocol: string; address: string; }; function parseUpstream(upstream: string): UpstreamEntry { if (upstream.startsWith("https://")) { return { protocol: "https://", address: upstream.slice(8) }; } if (upstream.startsWith("http://")) { return { protocol: "http://", address: upstream.slice(7) }; } return { protocol: "http://", address: upstream }; } export function UpstreamInput({ defaultUpstreams = [], name = "upstreams" }: { defaultUpstreams?: string[]; name?: string; }) { const initialEntries: UpstreamEntry[] = defaultUpstreams.length > 0 ? defaultUpstreams.map(parseUpstream) : [{ protocol: "http://", address: "" }]; const [entries, setEntries] = useState(initialEntries); const handleProtocolChange = (index: number, newProtocol: string) => { const updated = [...entries]; updated[index].protocol = newProtocol || "http://"; setEntries(updated); }; const handleAddressChange = (index: number, newAddress: string) => { const updated = [...entries]; // Strip protocol if user pasted a full URL if (newAddress.startsWith("https://")) { updated[index].protocol = "https://"; updated[index].address = newAddress.slice(8); } else if (newAddress.startsWith("http://")) { updated[index].protocol = "http://"; updated[index].address = newAddress.slice(7); } else { updated[index].address = newAddress; } setEntries(updated); }; const handleAdd = () => { setEntries([...entries, { protocol: "http://", address: "" }]); }; const handleRemove = (index: number) => { if (entries.length === 1) return; setEntries(entries.filter((_, i) => i !== index)); }; const serializedValue = entries .filter(e => e.address.trim() !== "") .map(e => `${e.protocol}${e.address.trim()}`) .join("\n"); return (

Upstreams

{entries.map((entry, index) => (
handleAddressChange(index, e.target.value)} placeholder="10.0.0.5:8080" className="flex-1" required={index === 0} />
))}
Backend servers to proxy requests to
); }