Calculators: never store NaN as the Kelvin temperature.
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

Clearing the temperature input stored parseFloat('') = NaN, rendering
every Kelvin cell as NaN and persisting the invalid value in localStorage
across reloads. Clearing now falls back to room temperature, non-numeric
input is ignored, and a bad stored value decodes to the default.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-09 15:27:08 -06:00 • committed by lrnselfreliance
parent 53f9386c85
commit b172a4c9d6

View file

@ -455,15 +455,27 @@ export const calcKelvinMilliVoltDrop = (amps, resistancePerKiloLength, length,
const FourWireKelvinCalculator = ({wireType, isSAE, length}) => {
const [ampsRange] = useState([1, 5, 10]);
// Stored in Celsius; displayed in Fahrenheit when SAE. Default is room temperature.
const decodeTemperature = (value) => {
const temperature = parseFloat(value);
return Number.isFinite(temperature) ? temperature : REFERENCE_TEMPERATURE_C;
};
const [temperatureC, setTemperatureC] = useLocalStorage(
'calculators.kelvin.temperature_c', REFERENCE_TEMPERATURE_C, parseFloat, (num) => num.toString());
'calculators.kelvin.temperature_c', REFERENCE_TEMPERATURE_C, decodeTemperature, (num) => num.toString());
const resistances = isSAE ? resistancesPerKFeet[wireType]
: resistancesPerKm[wireType];
const displayTemperature = isSAE ? roundDigits(temperatureC * 9 / 5 + 32, 1) : roundDigits(temperatureC, 1);
const handleTemperatureChange = (value) => {
if (value === '') {
// Never store NaN; fall back to room temperature when the input is cleared.
setTemperatureC(REFERENCE_TEMPERATURE_C);
return;
}
const temperature = parseFloat(value);
if (!Number.isFinite(temperature)) {
return;
}
setTemperatureC(isSAE ? (temperature - 32) * 5 / 9 : temperature);
};