-
-
Notifications
You must be signed in to change notification settings - Fork 260
LB-1761 : Save Fresh Releases filters in browser storage #3281
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
base: master
Are you sure you want to change the base?
LB-1761 : Save Fresh Releases filters in browser storage #3281
Conversation
| setCoverartOnly: (value: boolean) => void; | ||
| } | ||
|
|
||
| export default function useFilterPersistence( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name here is a bit too generic, you can imagine there are other places in the codebase where we might have filters.
Maybe useFreshReleasesFilterPersistence ?
| includeVariousArtists: boolean; | ||
| coverartOnly: boolean; | ||
|
|
||
| setCheckedList: (list: Array<string | undefined>) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a cumbersome way to go about saving the filters state, and makes it harder to extend in the future.
I think the hook should instead expose a freshReleasesFilters object with all the filters, a setFreshReleasesFilters function that accepts an object of type StoredFilters, as well as your existing clearSavedFilters .
What this means is moving the state management of these filter options inside this hook component instead of in the ReleaseFilters component instead of passing the values and their respective setters to the hook.
In ReleaseFilters you can use the values exposed by the hook, something like :
const { freshReleasesFilters, setFreshReleasesFilters, clearSavedFilters } = useFilterPersistence();
const {
checkedList,
releaseTagsCheckList,
releaseTagsExcludeCheckList,
includeVariousArtists,
coverartOnly
} = freshReleasesFilters
The other approach is to create a hook that only persists one value, and use that hook for each filter value instead of useState.
Something along the lines of
function usePersistentState(key, defaultValue) {
const [value, setValue] = useState(() => {
const storedValue = // get value from localForage here
return storedValue ?? defaultValue;
});
useEffect(() => {
// set value using localForage here
}, [key, value]);
return [value, setValue];
}
And then in ReleaseFilters:
const [checkedList, setCheckedList] = usePersistentState(`${RELEASE_FILTERS_STORAGE_KEY}-${pageType}-checkedList`, []);
Which might be easier to use, but does mean as many localForage calls as we have options, when we load the component. I would say test it out and see if there is an impact on performance at all?
| checkedList: checkedList.filter( | ||
| (item): item is string => item !== undefined | ||
| ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This syntax can be made a lot shorter like so to filter out falsy values (will also filter out empty strings but that's good in this case):
| checkedList: checkedList.filter( | |
| (item): item is string => item !== undefined | |
| ), | |
| checkedList: checkedList.filter(Boolean), |
| driver: [localforage.INDEXEDDB, localforage.LOCALSTORAGE], | ||
| storeName: "fresh-releases", | ||
| }); | ||
| const RELEASE_FILTERS_STORAGE_KEY = "release-filters"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the core of your issue with the filters resetting when you change between "for you" and general releases.
I think the ideal solution is to save the settings separately (more flexibility for users), using this key as a prefix, and adding the pageType as a suffix, something like:
key = `${RELEASE_FILTERS_STORAGE_KEY}-${pageType}`;
Which will result in the keys "release-filters-user" and "release-filters-sitewide" to store the filters separately.
|
Thank you @MonkeyDo for such detailed explanation. Will do the necessary changes.😃 |
|
Hello @Mshahnawaz1 ! |
Hi @MonkeyDo ! |
Problem
Currently as discussed in ticket LB-1761 we don't store the state of filter in fresh-releases so when user refreshes the page all the choices of user get reset.
Solution
So to solve this issue we are storing the filter state in local storage using localForage library.
In this we are storing these states:
We are using a custom hook
userfilterpersistence.tscreated inlistenbrainz/hooks/userfilterpersistence.tsto define the operations for filter data.Action
This needs to be checked if all the parameters that needs to be stored are correct. Also filters gets reset when we toggle from
AlltoFor youand also when refreshed.