-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix/79139 - Add sticky filters to the reports page #80247
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
Changes from all commits
c29d286
5a73135
6dc34d8
6a5529e
bf0ebb1
bff245a
4938e97
0e1bd77
497badd
efacb4a
f208fda
fd90e2d
2aecd0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import {useMemo} from 'react'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {SearchAdvancedFiltersForm} from '@src/types/form'; | ||
| import {getEmptyObject} from '@src/types/utils/EmptyObject'; | ||
| import useOnyx from './useOnyx'; | ||
|
|
||
| const allSearchAdvancedFilters: {current: Partial<SearchAdvancedFiltersForm>} = {current: {}}; | ||
| const prevSearchAdvancedFiltersFormsByType: {current: Record<string, Partial<SearchAdvancedFiltersForm> | undefined>} = {current: {}}; | ||
| /** | ||
| * This hook helps retain all filter values and will only update the filters that have changed | ||
| */ | ||
| export default function useStickySearchFilters(shouldUpdate?: boolean) { | ||
| const [searchAdvancedFiltersForm = getEmptyObject<Partial<SearchAdvancedFiltersForm>>()] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM, {canBeMissing: true}); | ||
|
|
||
| const currentAllSearchAdvancedFilters = useMemo(() => { | ||
| if (!shouldUpdate || !searchAdvancedFiltersForm.type) { | ||
| return allSearchAdvancedFilters.current; | ||
| } | ||
|
|
||
| const prevSearchAdvancedFiltersForm = prevSearchAdvancedFiltersFormsByType.current[searchAdvancedFiltersForm.type]; | ||
| const allKeys = new Set([...Object.keys(searchAdvancedFiltersForm), ...Object.keys(prevSearchAdvancedFiltersForm ?? {})]) as Set<keyof typeof searchAdvancedFiltersForm>; | ||
| const changedKeys: Array<keyof typeof searchAdvancedFiltersForm> = []; | ||
| for (const key of allKeys) { | ||
| const currentValue = searchAdvancedFiltersForm[key]; | ||
| const previousValue = prevSearchAdvancedFiltersForm?.[key]; | ||
| if (Array.isArray(currentValue) && Array.isArray(previousValue)) { | ||
| if (currentValue.sort().join(',') === previousValue.sort().join(',')) { | ||
| continue; | ||
| } | ||
| } else if (Object.is(currentValue, previousValue)) { | ||
| continue; | ||
| } | ||
|
|
||
| changedKeys.push(key); | ||
| } | ||
|
|
||
| for (const key of changedKeys) { | ||
| if (!prevSearchAdvancedFiltersForm && allSearchAdvancedFilters.current[key]) { | ||
| continue; | ||
| } | ||
| (allSearchAdvancedFilters.current[key] as unknown) = searchAdvancedFiltersForm[key] ?? undefined; | ||
| } | ||
| allSearchAdvancedFilters.current = {...allSearchAdvancedFilters.current, type: searchAdvancedFiltersForm.type}; | ||
| prevSearchAdvancedFiltersFormsByType.current[searchAdvancedFiltersForm.type] = searchAdvancedFiltersForm; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think that we can use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially, I also intended to use
|
||
|
|
||
| return allSearchAdvancedFilters.current; | ||
| // Here we only rely on `searchAdvancedFiltersForm`, without triggering when `shouldUpdate`, | ||
| // because `shouldUpdate` is just a flag indicating that an update can happen, | ||
| // and the actual update only occurs when `searchAdvancedFiltersForm` has truly been updated. | ||
| // And since `shouldUpdate` is a value derived from queryJSON data, | ||
| // when `searchAdvancedFiltersForm` is updated via useOnyx, | ||
| // `shouldUpdate` has already been updated beforehand, | ||
| // so there’s no concern about having an incorrect value. | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [searchAdvancedFiltersForm]); | ||
|
|
||
| return currentAllSearchAdvancedFilters; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.