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

#765 Results Page: Persist the column filters in the results table in the url #778

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Netacci
Copy link

@Netacci Netacci commented Oct 9, 2024

Description

This PR addresses the issue where the results table resets during a page reload after a user applies column filters (e.g., platform, status, confidence). The filtered values do not persist, causing the results table to revert to its default state.

Before

perf.mp4

The problem occurred because the filters were not stored in the URL, leading to their loss upon page reload or when the URL is shared. As a result, the filtered state is not retained, and users have to reapply their filters each time a page reloads.

Test cases

  1. By default, all filter values are checked, and as a result, all filter values are appended to the URL parameters.

    To achieve this, I used useEffect to set an initial filter state. This useEffect sets all the checked filter values to the URL.

defau-1.mp4
  1. When specific filters are applied or modified, the relevant filter values are added or updated in the URL.

    I adjusted the onToggle function to ensure that filter values are added to the URL when they are checked and removed when
    unchecked. This helps in maintaining persistence on page reload.

add-remove-filters-1.mp4
  1. On page reload or URL sharing, filtered values appended to the URL remain the same thereby persisting the results table filtered state.
page-reload-1.mp4
  1. If all filters are unchecked, the results table retains the unfiltered state, and the URL maintains the filter parameter keys with empty values.

This was challenging to implement at first, because when all filter values are unchecked and the page reloads, the default behavior (from test case 1) would normally re-check all filters and set them back to checked. To resolve this, instead of removing the parameter keys entirely from the URL, I set their values to empty. I also updated the useEffect to run once on page load and check if there is existing checked values to be applied before setting the default. That way there's a significant difference between when a user has unchecked all filters and when the default filter settings should apply, ensuring the correct behavior on page load.

all-unchecked-1.mp4
  1. When the clear filter button is clicked, filtered values are added back to the URL.
    I updated the clearFilter function to set all filter values to the URL.
clear-filter-1.mp4

Steps to test

  • Navigate to results table page
  • Apply Column filters by platform, status and/or confidence
  • Refresh the page and observe the filters persist both in the URL and the results table
  • Copy the URL to another tab and observe the filtered state is still maintained.

Copy link

netlify bot commented Oct 9, 2024

Deploy Preview for mozilla-perfcompare ready!

Name Link
🔨 Latest commit 01d28de
🔍 Latest deploy log https://app.netlify.com/sites/mozilla-perfcompare/deploys/671a50eb5ebfdb0008bec433
😎 Deploy Preview https://deploy-preview-778--mozilla-perfcompare.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

@syedbarimanjan syedbarimanjan left a comment

Choose a reason for hiding this comment

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

Left some comments.

@Netacci Netacci marked this pull request as draft October 10, 2024 11:00
@Netacci Netacci force-pushed the persist-result-table-filters branch from b618a8d to 2ae22b0 Compare October 10, 2024 12:39
Copy link

codecov bot commented Oct 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.55%. Comparing base (01b30d1) to head (64fd51e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #778      +/-   ##
==========================================
+ Coverage   91.53%   91.55%   +0.02%     
==========================================
  Files          88       88              
  Lines        2350     2369      +19     
  Branches      443      449       +6     
==========================================
+ Hits         2151     2169      +18     
- Misses        176      177       +1     
  Partials       23       23              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Netacci Netacci marked this pull request as ready for review October 10, 2024 13:02
@Netacci Netacci force-pushed the persist-result-table-filters branch 2 times, most recently from cdb7795 to 3b37a51 Compare October 10, 2024 18:25
@Netacci Netacci closed this Oct 11, 2024
@Netacci Netacci reopened this Oct 11, 2024
Copy link
Contributor

@julienw julienw left a comment

Choose a reason for hiding this comment

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

Thanks, this looks pretty good! The implementation is a bit complex though, so I left some comments about it, tell me what you think!

src/types/types.ts Outdated Show resolved Hide resolved
src/components/CompareResults/ResultsTable.tsx Outdated Show resolved Hide resolved
src/components/CompareResults/ResultsTable.tsx Outdated Show resolved Hide resolved
src/components/CompareResults/ResultsTable.tsx Outdated Show resolved Hide resolved
src/components/CompareResults/ResultsTable.tsx Outdated Show resolved Hide resolved
src/components/CompareResults/ResultsTable.tsx Outdated Show resolved Hide resolved
src/__tests__/CompareResults/ResultsView.test.tsx Outdated Show resolved Hide resolved
Comment on lines 132 to 134
useEffect(() => {
accessURlParams();
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using an effect, it would be better to use the 3rd version of useState (as described in the react documentation: https://react.dev/reference/react/useState#usestate).

This would set the initial value of tableFilters by using the URL Params directly.

By using an effect, instead, this impairs the performance. Indeed this works like this:

  1. React renders the component (and the rest of the tree)
  2. React runs the effect.
  3. If the effect changes the state, React renders the component again... and the rest of the tree as well!

We want to avoid 2 and 3. By setting the right state right away at the first render time, only the first step is run, so this is much faster.

I'd like to stress that you need to use the 3rd version of useState, not the first or second. Why? Because we want the computation to happen just once. This is explained in this part of their documentation: https://react.dev/reference/react/useState#avoiding-recreating-the-initial-state

Copy link
Author

Choose a reason for hiding this comment

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

@julienw Thank you for the detailed explanation!

I’ve updated the code to use lazy initialization for tableFilters based on the URL Params as suggested. This way, the computation only happens once, and React renders the component only once during the initial render.

Thanks again for pointing this out, and for the useful documentation links!

Copy link
Author

Choose a reason for hiding this comment

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

Hi @julienw I have implemented these changes and waiting for a re-review

@Netacci Netacci requested a review from julienw October 22, 2024 07:51
@Netacci Netacci force-pushed the persist-result-table-filters branch from f12d5fe to 64fd51e Compare October 23, 2024 07:26
- If URL is empty, then all table filters are checked, otherwise set the
  table filters based off that. This helps results table to persist upon
  reload and url copying
- Created a function that sets the filter to the url params on toggle
@Netacci Netacci force-pushed the persist-result-table-filters branch from 64fd51e to 01d28de Compare October 24, 2024 13:51
Copy link
Contributor

@julienw julienw left a comment

Choose a reason for hiding this comment

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

Thanks, this looks pretty good!

I just noticed a few small things that could be improved, and also a test for the functionality is missing.

That's OK to not fix this PR, please focus on your new task, but I still wanted to give you this last feedback.

};

useEffect(() => {
if (tableFilters.size > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This if condition shouldn't be necessary: indeed you also want to update the URL when the value changes and is empty :-)

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I see what you mean. I'll update this

Comment on lines +140 to +141
rawSearchParams.delete(`filter-${columnId}`);
updateRawSearchParams(rawSearchParams);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be unnecessary with the effect above especially if you remove the if.

cellsConfiguration.forEach(({ key, possibleValues }) => {
if (!possibleValues) return;
const paramValue = rawSearchParams.get(`filter-${key}`);
initialFilters.set(key, new Set(paramValue?.split(',')));
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be good to not set anything if paramValue is not present

@Carla-Moz
Copy link
Contributor

@Netacci Hi I'd like to let you know you've been selected to go on the next round! Congrats!
Please see the matrix channel for details https://chat.mozilla.org/#/room/#perfcompare-outreachy:mozilla.org

@Netacci
Copy link
Author

Netacci commented Oct 24, 2024

@Netacci Hi I'd like to let you know you've been selected to go on the next round! Congrats!
Please see the matrix channel for details https://chat.mozilla.org/#/room/#perfcompare-outreachy:mozilla.org

Thanks @Carla-Moz this is great news 🥲 I appreciate this opportunity 🙏

@Netacci
Copy link
Author

Netacci commented Oct 24, 2024

Thanks, this looks pretty good!

I just noticed a few small things that could be improved, and also a test for the functionality is missing.

That's OK to not fix this PR, please focus on your new task, but I still wanted to give you this last feedback.

Okay thanks for this.

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.

4 participants