Gracefully handle bad mod setting types in Web UI

Make sure all cobinations of missmatched types and missing mod settings
are gracefully handled by the Web UI.

Fixes #609.
This commit is contained in:
Hornwitser committed 2024-04-28 16:59:03 +02:00
1 parent 3cf9f96233
commit 4e1116ae2d
7 files changed
+104 -3

No files matched your search

@@ -424,7 +424,15 @@ function InputColor(props: {
value: lib.ModSettingColor,
onChange: (value: lib.ModSettingColor) => void,
}) {
const value = props.value;
// In certain edge cases it's possible a value for this mod setting does not exist
// or is of the wrong type. Fallback to black if this is the case.
function isColor(input: unknown) {
if (typeof input !== "object" || input === null) {
return false;
}
return "r" in input && "g" in input && "b" in input && "a" in input;
}
const value = isColor(props.value) ? props.value : { r: 0, g: 0, b: 0, a: 1 };
return <ColorPicker
defaultFormat="rgb"
defaultValue={`rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, ${value.a}`}
@@ -774,7 +782,7 @@ export default function ModPackViewPage() {
continue;
}
}
if (modifiedModPack!.settings[scope].get(settingName)!.value !== value) {
if (modifiedModPack!.settings[scope].get(settingName)?.value !== value) {
pushChange({ type: "settings.set", scope, name: settingName, value: { value }});
}
}