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

fix(createURL): ensure refined value gets removed #5912

Merged
merged 12 commits into from
Nov 14, 2023
Merged

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Oct 27, 2023

Summary

If there's only one refinement left, we used to keep uiState purposely as-is, although actually it needs to be cleared from the refinements.

This also has an impact (positive, removing stray refinements) on dispose with the new disposeMode likely, but to investigate.

This also needs to be done and tested for all other refinement widgets.

We possibly also want to remove the refinementList object if it no longer has values (unless routing already deals with empty objects fine)

This behaviour became obvious after #5696 when routing became based on uiState instead of searchParameters

Result

getWidgetUiState removes no-longer-valid values from uiState, instead of leaving the object as-is if there are no refinements.

Original reason for that code was to avoid recreating the object if nothing has changed, but that's not correct. Not sure if this has a perf impact, but I think it's unavoidable

Sandbox that shows the issue for refinementList: https://codesandbox.io/s/algolia-kickstart-userefinementlist-72kryv?file=/src/index.js

FX-2667

If there's only one refinement left, we used to keep `uiState` purposely as-is, although actually it needs to be cleared from the refinements.

This also has an impact (positive, removing stray refinements) on dispose with the new disposeMode likely, but to investigate.

This also needs to be done and tested for all other refinement widgets
@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 27, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 456cb43:

Sandbox Source
InstantSearch.js Configuration
react-instantsearch-app Configuration
example-react-instantsearch-default-theme Configuration
example-react-instantsearch-next-app-dir-example Configuration
example-vue-instantsearch-default-theme Configuration
Algolia Kickstart - useRefinementList PR

@@ -562,4 +559,27 @@ As this is not supported, please make sure to only use this attribute with one o
};
};

function removeEmptyRefinementsFromUiState(indexUiState: IndexUiState) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

does removing this function break any tests?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it adds empty values in widget's ui states, which are not expected by tests, and can in some instance show with the default router. I've added this method in each connector for now, but I'd like to see if I can clean these empty refinements from a single location instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe it can be done in the index widget after getUiState has been called, so you only need to iterate once, instead of for every widget (although getWidgetUiState is called in multiple places of course)

Copy link
Member

Choose a reason for hiding this comment

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

It wasn't feasible to remove empty attributes at a later point in the index widget, as:

  • some parts of the call directly use results from connectors' getWidgetUiState()
  • the predicates used to determine wether attributes are considered empty differs between connectors

@dhayab dhayab marked this pull request as ready for review November 13, 2023 10:38
@dhayab dhayab requested review from a team, dhayab, sarahdayan and aymeric-giraudet and removed request for a team and dhayab November 13, 2023 10:39
Copy link
Member

@aymeric-giraudet aymeric-giraudet left a comment

Choose a reason for hiding this comment

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

🎉

Copy link
Member

@sarahdayan sarahdayan left a comment

Choose a reason for hiding this comment

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

Looks correct!

Optional, but we could probably factorize all the removeEmptyRefinementsFromUiState functions into one with a variable key (and maybe some more logic to pass for the reduce) and see if this has an impact on the increased bundle size.

@dhayab
Copy link
Member

dhayab commented Nov 14, 2023

Looks correct!

Optional, but we could probably factorize all the removeEmptyRefinementsFromUiState functions into one with a variable key (and maybe some more logic to pass for the reduce) and see if this has an impact on the increased bundle size.

Tried it initially but I got so many frustrations with types I went back to this state. Ideally the refactored function would accept the connector indexUiState as a generic type which extends a base type, and a predicate argument that returns whether the value is empty or not. But the type intersections for IndexUiState are difficult to work with.

@dhayab dhayab enabled auto-merge (squash) November 14, 2023 13:27
@dhayab dhayab merged commit 77919a9 into master Nov 14, 2023
12 checks passed
@dhayab dhayab deleted the fix/create-url-refine branch November 14, 2023 13:38
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.

None yet

4 participants