Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: match query params when determining highlighted items #7139

Merged
merged 7 commits into from
Feb 22, 2024

Conversation

vursen
Copy link
Contributor

@vursen vursen commented Feb 21, 2024

Description

The PR adds new logic to side-nav to match query params when determining highlighted items. A side-nav item is highlighted if the browser URL has the same base origin, the same path, and at least contains all the query parameters from the item's path.

Here is a table that illustrates which browser URLs would match which item URLs with the new logic:

Item URL ➡️
Browser URL ⬇️
/products /products?c=socks /products?c= /products?c=socks&c=pants /products?socks
/products
/products?c=socks
/products?c=pants
/products?c=
/products?c=socks&item=5
/products?item=5&c=socks
/products?c=socks&c=pants
/products?socks
/products?socks=

Warning

Note that it's a breaking change for some configurations. When using an item's URL to set defaults for dynamic params, such as for filtering or pagination, then changing those params would break matching. For example, if you configure an item with a URL like /products?pageSize=10 and then the page size is changed, the item wouldn't match anymore. Could be fixed on the application side by adding a path alias without the dynamic query params, or applying the defaults based on their absence in the URL.

Fixes #6778

Type of change

  • Feature

@vursen vursen changed the title feat: match query params in side nav item paths feat: match query params in side nav paths Feb 21, 2024
@vursen vursen changed the title feat: match query params in side nav paths feat: match query params when computing higlighted item Feb 21, 2024
@vursen vursen changed the title feat: match query params when computing higlighted item feat: match query params when determining higlighted items Feb 21, 2024
@vursen vursen changed the title feat: match query params when determining higlighted items feat!: match query params when determining higlighted items Feb 21, 2024
@vursen vursen marked this pull request as ready for review February 21, 2024 12:29
const paths = ['', '/', '/path', 'base/path'];

beforeEach(() => {
documentBaseURI = sinon.stub(document, 'baseURI').value('http://localhost/');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: web-test-runner adds a unique query parameter with a session ID to the base URI, which causes some tests to fail because they don't expect it. Stubbing the baseURI solves this problem.

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

this.pathAliases.some((alias) => matchPaths(document.location.pathname, alias))
);

const browserPath = `${document.location.pathname}${document.location.search}`;
Copy link
Contributor Author

@vursen vursen Feb 22, 2024

Choose a reason for hiding this comment

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

I haven't found a trivial way to stub document.location properties since they can't be rewritten, so I only verified this change through manual testing. Any ideas how to test it are welcome.

@vursen vursen requested a review from sissbruecker February 22, 2024 10:00
@vursen
Copy link
Contributor Author

vursen commented Feb 22, 2024

Snippet for manual testing:

<vaadin-side-nav>
  <span slot="label">Menu</span>

  <vaadin-side-nav-item path="/dev/side-nav.html">
    /dev/side-nav.html
  </vaadin-side-nav-item>

  <vaadin-side-nav-item path="/dev/side-nav.html?view">
    /dev/side-nav.html?view
  </vaadin-side-nav-item>

  <vaadin-side-nav-item path="/dev/side-nav.html?view=users">
    /dev/side-nav.html?view=users
  </vaadin-side-nav-item>

  <vaadin-side-nav-item path="/dev/side-nav.html?view=products">
    /dev/side-nav.html?view=products
  </vaadin-side-nav-item>

  <vaadin-side-nav-item path="/dev/side-nav.html?view=products&product[]=1">
    /dev/side-nav.html?view=products&product[]=1
  </vaadin-side-nav-item>

  <vaadin-side-nav-item path="/dev/side-nav.html?view=products&product[]=1&product[]=2">
    /dev/side-nav.html?view=products&product[]=1&product[]=2
  </vaadin-side-nav-item>
</vaadin-side-nav>

Copy link
Contributor

@sissbruecker sissbruecker left a comment

Choose a reason for hiding this comment

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

LGTM! Regarding the following comment from the issue:

When using an item's URL to set defaults for dynamic params, such as for filtering or pagination, then changing those params would break matching. For example, if you configure an item with a URL like /products?pageSize=10 and then the page size is changed, the item wouldn't match anymore. Could be fixed on the application side by adding a path alias without the dynamic query params, or applying the defaults based on their absence in the URL.

As this could be a breaking change for some, let's note this down somehow, so that we can add something to the release notes.

expect(matchPaths('/products?c=socks', '/products?c=socks')).to.be.true;
expect(matchPaths('/products?c=socks&item=5', '/products?c=socks')).to.be.true;
expect(matchPaths('/products?item=5&c=socks', '/products?c=socks')).to.be.true;
expect(matchPaths('/products?c=socks&c=pants', '/products?c=socks')).to.be.true;
Copy link
Contributor Author

@vursen vursen Feb 22, 2024

Choose a reason for hiding this comment

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

note: The browser URL /products?c=socks&c=pants matches the item path /products?c=socks in my implementation, which deviates from @sissbruecker's proposal – see the case that is marked with a question mark in the table. In my opinion, the implemented behavior is more consistent with other cases.

@vursen vursen merged commit c86e1d3 into main Feb 22, 2024
9 checks passed
@vursen vursen deleted the feat/side-nav-query-params-matching branch February 22, 2024 17:58
@vursen vursen changed the title feat!: match query params when determining higlighted items feat!: match query params when determining highlighted items Feb 22, 2024
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with Vaadin 24.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Current vaadin-side-nav-item ignores query string
3 participants