Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

When a custom palette is selected and the page reloads, updateSelectedPalette() is called before custom palettes are added to the DOM, causing TypeError: Cannot read properties of null (reading 'querySelector').

Root Cause

During initialization, the sequence is:

  1. loadPalettes() builds palette list with base palettes only
  2. requestJson()readState()updateUI()updateSelectedPalette()
  3. Custom palettes haven't been added yet (requires lastinfo.cpalcount from step 2)

Changes

Added early return when palette element doesn't exist:

function updateSelectedPalette(s) {
  var parent = gId('pallist');
  var selPaletteInput = parent.querySelector(`input[name="palette"][value="${s}"]`);
  if (selPaletteInput) selPaletteInput.checked = true;

  var selElement = parent.querySelector('.selected');
  if (selElement) selElement.classList.remove('selected');

  var selectedPalette = parent.querySelector(`.lstI[data-id="${s}"]`);
  if (!selectedPalette) return; // palette not yet loaded (custom palette on initial load)
  selectedPalette.classList.add('selected');
  // ... rest of function
}

The palette is correctly selected later when populatePalettes() re-runs with custom palettes.

Original prompt

This section details on the original issue you should resolve

<issue_title>UI loading is delayed if custom palette is selected upon page load</issue_title>
<issue_description>### What happened?

There is a circular dependency in index.js:
when the UI is loaded and the selected palette is a custom palette, the call to requestJson() throws an error because it calls readState() but the selected palette is not yet known because the palette list can only be filled including custom palettes after the lastinfo is fetched, which happens in requestJson(). So the UI throws this error:

To Reproduce Bug

select a custom palette, reload the UI

Expected Behavior

resolve custom palettes without throwing the error.

Install Method

Binary from WLED.me

What version of WLED?

nightly build

Which microcontroller/board are you seeing the problem on?

ESP32-C3

Relevant log/trace output

TypeError: Cannot read properties of null (reading 'querySelector')
    at updateSelectedPalette ((index):7:63528)
    at updateUI ((index):7:62253)
    at readState ((index):7:68130)
    at (index):7:71556

Anything else?

the UI still load correctly, it just takes longer due to timeouts and there is the error thrown.

Code of Conduct

  • I agree to follow this project's Code of Conduct</issue_description>

<agent_instructions>please check if there is a way to avoid this error without adding a lot of code. keep it brief.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 18, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI changed the title [WIP] Fix UI loading delay for custom palette selection Fix TypeError when loading UI with custom palette selected Dec 18, 2025
Copilot AI requested a review from DedeHai December 18, 2025 19:03
@DedeHai DedeHai marked this pull request as ready for review December 18, 2025 19:16
Copy link
Collaborator

@DedeHai DedeHai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested working

@DedeHai DedeHai merged commit 65f1d8d into main Dec 18, 2025
28 checks passed
softhack007 pushed a commit to MoonModules/WLED-MM that referenced this pull request Dec 18, 2025
* Add null check to fix circular dependency with custom palettes

Co-authored-by: DedeHai <[email protected]>

* Refactor: move null check earlier for better efficiency

Co-authored-by: DedeHai <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: DedeHai <[email protected]>
@netmindz netmindz deleted the copilot/fix-ui-loading-delay branch December 19, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UI loading is delayed if custom palette is selected upon page load

2 participants