galloween / custom-sidebar-v2

Home Assistant (HACS) plugin that allows you to rearrange, hide, and add sidebar menu items. Refactor of https://github.com/Villhellm/custom-sidebar
21 stars 13 forks source link

Plugin not working for non-admin users #12

Open user2684 opened 2 years ago

user2684 commented 2 years ago

Hi, first of all thanks for refactoring this plugin and make it working with the latest version of HA :-) I noticed everything works fine for admin users but not for non administrators. The issue seems related to getSidebarItem() when called upon startup returning undefined since looking for "data-panel" equals to "config" which is not there for a non admin user (https://github.com/galloween/custom-sidebar-v2/blob/main/dist/custom-sidebar-v2.js#L145). As a workaround a tried picking up the latest element (e.g. .at(-1)) of window.$customSidebarV2.SideBarElement which works but I'm sure it breaks out other things. Thanks!

seanmccabe commented 2 years ago

+1 have the same issue

MelvinSnijders commented 1 year ago

Same issue here!

XxInvictus commented 1 year ago

Took a look myself as I had the same issue, .at(-1) tends to get the blank space element so I grabbed the array in Console and found where config is not present media-browser is the next last array item.

Let me know if the below works for you as well (because I am not sure if media-browser is a default), this is a full drop in for the getSidebarItem function:

  function getSidebarItem(root) {
    if (window.$customSidebarV2.SidebarItemElement) {
      return window.$customSidebarV2.SidebarItemElement;
    }
    if (!root || !root.children) {
      return;
    }
    //return Array.from(root.children).at(-1)
    //return Array.from(root.children).find((element) => {
    //    return (
    //      element.tagName == 'A' && element.getAttribute('data-panel') == 'config'
    //    );
    //  }
    let lastdatapanel = Array.from(root.children).find((element) => {
      return (
        element.tagName == 'A' && element.getAttribute('data-panel') == 'config'
      );
    });

    if (!lastdatapanel) {
      lastpanel = Array.from(root.children).find((element) => {
        return (
          element.tagName == 'A' && element.getAttribute('data-panel') == 'media-browser'
        );
      });
    }

    return lastpanel;
  }
XxInvictus commented 1 year ago

Also logged a PR with the above, though it may need cleaning up this was a 5-second fix and could probably be cleaner 👍