Skip to content

Modernize JS and tighten things#3388

Merged
PromoFaux merged 4 commits intopi-hole:developmentfrom
XhmikosR:xmr/js
Apr 24, 2025
Merged

Modernize JS and tighten things#3388
PromoFaux merged 4 commits intopi-hole:developmentfrom
XhmikosR:xmr/js

Conversation

@XhmikosR
Copy link
Copy Markdown
Contributor

@XhmikosR XhmikosR commented Apr 5, 2025

This is #3338 rebased.

The changes are split but I want to squash the autofix-patches before merging.

Non-whitespace diff: https://github.com/pi-hole/web/pull/3388/files?w=1

TODO for this PR, feel free to push to this branch:

  • we should really fix the TODOs I've added, but I need help from someone more familiar with these parts of the code

TODO for later, but feel free to push to this branch:

  • reduce for...in usage. We should use for...of along with Object methods because this way we only loop through own keys only so no need to check for Object.hasOwn Already tackled in my jQuery branch
  • we should stop using hidden global variables. If something is supposed to be global, we need to use globalThis so that it's clear
  • NEVER ever use var anymore from now on; this is now enforced by the xo rules
  • we should enable strict mode; I'll try to fix this later, but xo by default enforces type: module so it might be tricky
  • I also have a branch based on this one that I started the work to reduce the jQuery usage in our code, see https://github.com/XhmikosR/web/compare/xmr/js...XhmikosR:web:xmr/reduce-jquery?w=1. I plan to open a PR later after this PR is merged

Thank you for your contribution to the Pi-hole Community!

Please read the comments below to help us consider your Pull Request.

We are all volunteers and completing the process outlined will help us review your commits quicker.

Please make sure you

  1. Base your code and PRs against the repositories developmental branch.
  2. Sign Off all commits as we enforce the DCO for all contributions
  3. Sign all your commits as they must have verified signatures
  4. File a pull request for any change that requires changes to our documentation at our documentation repo

What does this PR aim to accomplish?:

How does this PR accomplish the above?:

Link documentation PRs if any are needed to support this PR:


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code and I have tested my changes.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)
  6. I have checked that another pull request for this purpose does not exist.
  7. I have considered, and confirmed that this submission will be valuable to others.
  8. I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  9. I give this submission freely, and claim no ownership to its content.

  • I have read the above and my PR is ready for review. Check this box to confirm

@github-actions
Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions
Copy link
Copy Markdown
Contributor

Conflicts have been resolved.

@XhmikosR
Copy link
Copy Markdown
Contributor Author

This is ready for review. I don't mark it as ready because I want to rebase to squash the autofix patches before the merge.

Can someone please review so that we move forward?

@dschaper
Copy link
Copy Markdown
Member

Tests are not passing and it's in draft status. It is not known if these failing conditions are intentional or not.

@XhmikosR
Copy link
Copy Markdown
Contributor Author

XhmikosR commented Apr 22, 2025

All tests pass, it's CodeFactor that complains about the two/three TODO comments I have added that would be nice to address, but we can live with it for now if it helps moving forward.

The PR is still in draft because I want to rebase the autofix patches before the merge. I only split them to make review easier.

@dschaper
Copy link
Copy Markdown
Member

Okay, I don't remember offhand if changing from Draft to Ready will remove any existing approvals. Would it be better to set the PR as Ready with Squash for the merge strategy?

@XhmikosR
Copy link
Copy Markdown
Contributor Author

XhmikosR commented Apr 22, 2025 via email

Copy link
Copy Markdown
Member

@PromoFaux PromoFaux left a comment

Choose a reason for hiding this comment

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

My eyes hurt now.

const XForwardedFor = globalThis.atob(advancedInfoSource.data("xff") ?? "");
const starttime = parseFloat(advancedInfoSource.data("starttime"));
const endtime = parseFloat(advancedInfoSource.data("endtime"));
const XForwardedFor = globalThis.atob(advancedInfoSource.data("xff") || "") || null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I note we go from a "If null then blank string" to "if null then blank string, but if that's null then null"

What difference is made here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was wrong before. I changed it so that it gets properly set to null thus the line where it's checked can use the ?? operator.

$("#client-id").text(XForwardedFor ?? clientIP);

timeLineChart.data.datasets.push({
data: [],
// If we ran out of colors, make a random one
backgroundColor: colors[i],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know this isn't part of your changeset, but how does colors[i] make a random one? Maybe a comment that no longer matches the code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure if the comment is right TBH.

web/scripts/js/index.js

Lines 165 to 168 in f7a07c7

backgroundColor:
i < THEME_COLORS.length
? THEME_COLORS[i]
: "#" + (0x1_00_00_00 + Math.random() * 0xff_ff_ff).toString(16).substr(1, 6),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's possible that either the comment is wrong or the code wasn't copied from the other location fully.

Someone closer to the code here might be best placed to answer -though it's not a blocker for this PR.

@rdwebdesign / @DL6ER

Copy link
Copy Markdown
Member

@rdwebdesign rdwebdesign Apr 22, 2025

Choose a reason for hiding this comment

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

This was a copy paste error.

If you look at line 51 you will see colors[] has only 4 items. This is used for the first graphic (containing up to 4 items: Blocked, permitted, cached and other). This will never need extra random colors.

The comment about "creating random colors" was copied from lines 161-174, where random colors are actually created if there are more clients than available colors in THEME_COLORS[i].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's possible that either the comment is wrong or the code wasn't copied from the other location fully.

I think the code was initially copied (probably as a "template") and changed, but the comment was left.

The comment can be removed.

@PromoFaux
Copy link
Copy Markdown
Member

No blockers from a code point of view but I have not had time to test it yet (though I can't see anything that would cause any issues per se)

Happy to ignore the TODO warnings from codefactor.

I'm also not precious about squashing the commits, makes it easier to follow each change.

XhmikosR added 2 commits April 23, 2025 07:53
Signed-off-by: XhmikosR <xhmikosr@gmail.com>
Signed-off-by: XhmikosR <xhmikosr@gmail.com>
@XhmikosR
Copy link
Copy Markdown
Contributor Author

Your call. I would squash the autofix patches because that's what I initially did, but not going to lose my sleep over it either :)

@XhmikosR
Copy link
Copy Markdown
Contributor Author

This is the rebased branch development...XhmikosR:web:xmr/js-2. Let me know and I can force push it here.

@PromoFaux
Copy link
Copy Markdown
Member

OK, given each page a look through - and everything appears to still be working. Please FP the branch you mentioned

XhmikosR added 2 commits April 23, 2025 20:50
Signed-off-by: XhmikosR <xhmikosr@gmail.com>
Signed-off-by: XhmikosR <xhmikosr@gmail.com>
@PromoFaux PromoFaux closed this Apr 24, 2025
@PromoFaux PromoFaux reopened this Apr 24, 2025
@PromoFaux
Copy link
Copy Markdown
Member

@XhmikosR can you push something to this PR please? Codefactor is being... annoying. Maybe close it, delete the branch and then recreate the PR?

@PromoFaux PromoFaux closed this Apr 24, 2025
@PromoFaux PromoFaux reopened this Apr 24, 2025
@XhmikosR
Copy link
Copy Markdown
Contributor Author

XhmikosR commented Apr 24, 2025 via email

@PromoFaux
Copy link
Copy Markdown
Member

You will need to bypass Code factor because of the newly added TODO

Yep - that's what I was trying to do, but had to uninstall it from the org and add it again. It's being really stupid. But I've got it back working for me at least now (nobody else can access the config though)

@PromoFaux PromoFaux merged commit d9a7acf into pi-hole:development Apr 24, 2025
16 checks passed
@PromoFaux
Copy link
Copy Markdown
Member

Holy crap that was hard work

@PromoFaux PromoFaux mentioned this pull request May 6, 2025
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.

4 participants