import type { ConfigDeviceForm, ParameterSpec } from "../types";
import { PROTOCOL_SPECS } from "../constants/protocols";
interface ConfigFormProps {
deviceId?: number;
configForm: ConfigDeviceForm;
configFeedback: { type: "success" | "error"; message: string } | null;
isConfigMutating: boolean;
hasExistingConfig: boolean;
currentProtocolSpec: (typeof PROTOCOL_SPECS)[number] | undefined;
availableParameterSpecs: ParameterSpec[];
onUpdateField: (field: "name" | "protocol" | "model", value: string) => void;
onUpdateParameterRow: (
index: number,
field: "key" | "value",
value: string
) => void;
onAddParameterRow: () => void;
onRemoveParameterRow: (index: number) => void;
onSave: (deviceId?: number) => void;
onReset: (deviceId?: number) => void;
onCancel: () => void;
onDelete?: (deviceId: number) => void;
}
const isEmpty = (value: string) =>
value.trim() === "" || value === "null" || value === "0";
export const ConfigForm = ({
deviceId,
configForm,
configFeedback,
isConfigMutating,
hasExistingConfig,
currentProtocolSpec,
availableParameterSpecs,
onUpdateField,
onUpdateParameterRow,
onAddParameterRow,
onRemoveParameterRow,
onSave,
onReset,
onCancel,
onDelete,
}: ConfigFormProps) => {
const idSuffix = deviceId ? `-${deviceId}` : "-new";
return (
{deviceId ? `Configure Device #${deviceId}` : "Add New Device"}
{hasExistingConfig && deviceId && onDelete && (
)}
onUpdateField("name", event.target.value)}
placeholder="Living room lamp"
/>
{currentProtocolSpec?.models ? (
) : (
onUpdateField("model", event.target.value)}
placeholder="Optional model name"
disabled={!configForm.protocol}
/>
)}
Parameters
{configForm.parameters.map((parameter, index) => {
const paramSpec = availableParameterSpecs.find(
(spec) => spec.name === parameter.key
);
console.log(
"parameter:",
parameter,
"paramSpec:",
paramSpec,
!isEmpty(parameter.value)
);
if (!paramSpec && isEmpty(parameter.value)) {
return null;
}
return (
{availableParameterSpecs.length > 0 ? (
) : (
onUpdateParameterRow(index, "key", event.target.value)
}
disabled={!configForm.protocol}
/>
)}
onUpdateParameterRow(index, "value", event.target.value)
}
disabled={!parameter.key}
/>
);
})}
{configFeedback && (
{configFeedback.message}
)}
);
};