Skip to content

Commit

Permalink
bug: additional checks for card editor entities array
Browse files Browse the repository at this point in the history
avoids calling `some` function in case entities is not an array

avoids calling `some` function in case entities is not an array

This wouldn't result in any visual errors, but the console would get filled with `console.error()`

small refactoring

Update hui-card-element-editor.ts

chore: update build file

docs: update readme with disclaimer

Update README.md

fix: revert changes unrelated to PR

Delete settings.json

Discard changes to card-mod.js
  • Loading branch information
flixlix authored and thomasloven committed Nov 22, 2023
1 parent 1c802ed commit e7405bf
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/patch/hui-card-element-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ customElements.whenDefined("hui-card-element-editor").then(() => {
if (retval) {
const _setConfig = retval.setConfig;
try {
retval.setConfig = function (config: any, ...rest) {
// Strip card_mod from the data that's sent to the config element
// and put it back after the config has been checked
const newConfig = JSON.parse(JSON.stringify(config));
this._cardModData = {
card: newConfig.card_mod,
entities: [],
};
if (newConfig.entities) {
for (const [i, e] of newConfig.entities?.entries()) {
this._cardModData.entities[i] = e.card_mod;
delete e.card_mod;
}
retval.setConfig = function (config: any, ...rest) {
// Strip card_mod from the data that's sent to the config element
// and put it back after the config has been checked
const newConfig = JSON.parse(JSON.stringify(config));
this._cardModData = {
card: newConfig.card_mod,
entities: [],
};
if (Array.isArray(newConfig.entities)) {
for (const [i, e] of newConfig.entities?.entries()) {
this._cardModData.entities[i] = e.card_mod;
delete e.card_mod;
}
delete newConfig.card_mod;
}
delete newConfig.card_mod;

_setConfig.bind(this)(newConfig, ...rest);
if (newConfig.entities) {
for (const [i, e] of newConfig.entities?.entries()) {
if (this._cardModData.entities[i])
e.card_mod = this._cardModData.entities[i];
}
_setConfig.bind(this)(newConfig, ...rest);
if (Array.isArray(newConfig.entities)) {
for (const [i, e] of newConfig.entities?.entries()) {
if (this._cardModData.entities[i])
e.card_mod = this._cardModData.entities[i];
}
};
}
};
} catch (error) {
console.warn(error);
}
Expand Down Expand Up @@ -79,7 +79,8 @@ customElements.whenDefined("hui-dialog-edit-card").then(() => {
button.appendChild(this._cardModIcon);
if (
this._cardConfig?.card_mod ||
this._cardConfig?.entities?.some((e: any) => e.card_mod)
(Array.isArray(this._cardConfig.entities) ??
this._cardConfig?.entities?.some((e: any) => e.card_mod))
) {
this._cardModIcon.style.visibility = "visible";
} else {
Expand Down

0 comments on commit e7405bf

Please sign in to comment.