Compare commits

...

1 Commits

Author SHA1 Message Date
Jake Howell 35b8ff6fca fix(site): preserve autofill values during initial parameter sync
When the server has not yet evaluated a parameter (param.value.valid is
false), its value is meaningless — it only reflects template defaults,
not autofill or build parameter values. Previously, useSyncFormParameters
would overwrite the form with these empty server values, clearing
autofilled values like '12345' before the server had a chance to process
them.

Use param.value.valid as the discriminator: when false, preserve the
form's existing value; when true, respect the server's evaluated value
(even if empty, since that represents an intentional dynamic
recalculation).

Fixes coder/internal#1154
2026-02-18 14:20:52 +00:00
@@ -16,8 +16,9 @@ export function useSyncFormParameters({
formValues,
setFieldValue,
}: UseSyncFormParametersProps) {
// Form values only needs to be updated when parameters change
// Keep track of form values in a ref to avoid unnecessary updates to rich_parameter_values
// Form values only needs to be updated when parameters change.
// Keep track of form values in a ref to avoid unnecessary
// updates to rich_parameter_values.
const formValuesRef = useRef(formValues);
useEffect(() => {
@@ -27,16 +28,28 @@ export function useSyncFormParameters({
useEffect(() => {
if (!parameters) return;
const currentFormValues = formValuesRef.current;
const newParameterValues = parameters.map((param) => ({
name: param.name,
value: param.value.valid ? param.value.value : "",
}));
const currentFormValuesMap = new Map(
currentFormValues.map((value) => [value.name, value.value]),
);
const newParameterValues = parameters.map((param) => {
// When the server has not evaluated this parameter yet
// (valid=false), its value is meaningless — preserve
// whatever the form already holds (e.g. autofill or
// previous build values). When valid=true, the server
// has intentionally set this value and we respect it.
if (!param.value.valid) {
const currentValue = currentFormValuesMap.get(param.name);
if (currentValue) {
return { name: param.name, value: currentValue };
}
}
return {
name: param.name,
value: param.value.valid ? param.value.value : "",
};
});
const isChanged =
currentFormValues.length !== newParameterValues.length ||
newParameterValues.some(