import { useEffect, useState } from "react"; import type { PortMapping } from "../../lib/types"; import Button from "../ui/Button"; import { monoInputClass, selectClass } from "../ui/Field"; interface Props { portMappings: PortMapping[]; disabled: boolean; disabledReason?: string; onSave: (mappings: PortMapping[]) => Promise; } export default function PortMappingsEditor({ portMappings: initial, disabled, disabledReason, onSave, }: Props) { const [mappings, setMappings] = useState(initial); useEffect(() => { setMappings(initial); }, [initial]); const updatePort = ( index: number, field: "host_port" | "container_port", value: string, ) => { const updated = [...mappings]; const num = parseInt(value, 10); updated[index] = { ...updated[index], [field]: isNaN(num) ? 0 : num }; setMappings(updated); }; return (
{disabled && disabledReason && (

{disabledReason}

)} {mappings.length === 0 && (

No port mappings configured.

)} {mappings.length > 0 && (
Host port Container port Protocol
)} {mappings.map((pm, i) => (
updatePort(i, "host_port", e.target.value)} onBlur={() => onSave(mappings)} placeholder="8080" aria-label={`Host port ${i + 1}`} disabled={disabled} className={`w-[28%] ${monoInputClass}`} /> updatePort(i, "container_port", e.target.value)} onBlur={() => onSave(mappings)} placeholder="8080" aria-label={`Container port ${i + 1}`} disabled={disabled} className={`w-[28%] ${monoInputClass}`} />
))}
); }