Repository navigation
Perf/spend filters hover #97697
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
Perf/spend filters hover #97697
Changes from all commits
b745fb8
fa50d80
42db692
544b133
b462141
58da995
67ca78b
8413798
245a13f
20efc0d
8b4421f
5f2c043
68ac880
bc3e89e
11c0ee5
2f9349b
84dd129
347335f
3ee04be
64f9c63
957014e
b389daa
4acd75e
cb36f2c
6f18ca9
4aa3727
4ec6b1e
27c3174
f94a354
7a28f61
b49f964
c6a5547
b9a517e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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. These changes only kick in on wide web — the popover is web-only, and small-screen/mobile go through the separate full-page filters flow, which still rebuilds each time. So the PR both leaves mobile perf unchanged and introduces a behavior split: e.g. on wide web the From/To/Amount/... search text is preserved across filter switches, on small device it isn't. @sumo-slonik Could we make the behavior consistent across form factors?
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. Fair point on the routing, but the gain isn't web-only. The hover intent and the kept panes are popover-bound, but memoizing the personal detail derivations is platform-neutral: The behaviour split only exists within a single opening of the popover, since that's how long the kept panes live, reopen the filters and the search box is empty, same as on a small screen |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,18 +4,21 @@ import SearchAdvancedFiltersContent from '@components/Search/FilterComponents/Ad | |
| import useUpdateFilterQuery from '@components/Search/hooks/useUpdateFilterQuery'; | ||
| import type {SearchQueryJSON} from '@components/Search/types'; | ||
|
|
||
| import {useDebounceWithControls} from '@hooks/useDebounce'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import usePointerMovement from '@hooks/usePointerMovement'; | ||
| import useStyleUtils from '@hooks/useStyleUtils'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import useWindowDimensions from '@hooks/useWindowDimensions'; | ||
|
|
||
| import {getFilterNegatableValue} from '@libs/SearchUIUtils'; | ||
| import {getFilterNegatableValue, hasFilterContentValuesChanged} from '@libs/SearchUIUtils'; | ||
| import type {SearchFilter} from '@libs/SearchUIUtils'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {SearchAdvancedFiltersForm} from '@src/types/form'; | ||
|
|
||
| import React, {useRef, useState} from 'react'; | ||
| import React, {Activity, useRef, useState} from 'react'; | ||
| import {View} from 'react-native'; | ||
|
|
||
| import AmountFilterContentPopupWrapper from './AmountFilterContentPopupWrapper'; | ||
|
|
@@ -28,43 +31,197 @@ type SearchAdvancedFiltersPopupProps = { | |
| queryJSON: SearchQueryJSON; | ||
| }; | ||
|
|
||
| /** Which filter contents are mounted and what each was given. */ | ||
| type MountedFilterState = { | ||
| /** The filter whose content is shown. */ | ||
| activeFilter: SearchFilter['key']; | ||
|
|
||
| /** The mounted contents, in visit order. They stay mounted until the popover closes. */ | ||
| mountedFilters: Array<SearchFilter['key']>; | ||
|
|
||
| /** The filter values each mounted instance was built from. A revisit compares against it. */ | ||
| formAtMount: Partial<Record<SearchFilter['key'], Partial<SearchAdvancedFiltersForm>>>; | ||
|
|
||
| /** The filter values each content was last shown with. It goes on rendering with them while hidden. */ | ||
| formWhileHidden: Partial<Record<SearchFilter['key'], Partial<SearchAdvancedFiltersForm>>>; | ||
|
|
||
| /** How many times each content has been remounted. Part of its key, so a bump replaces the instance. */ | ||
| contentVersions: Partial<Record<SearchFilter['key'], number>>; | ||
|
|
||
| /** The deferred filters now allowed to derive. One left out stands as its own loading state. */ | ||
| readyFilters: Array<SearchFilter['key']>; | ||
| }; | ||
|
|
||
| const filterComponents = { | ||
| List: ListFilterContentPopupWrapper, | ||
| Text: TextInputFilterContentPopupWrapper, | ||
| Amount: AmountFilterContentPopupWrapper, | ||
| Date: DateFilterContentPopupWrapper, | ||
| ReportField: ReportFieldFilterContentPopupWrapper, | ||
| } as const; | ||
|
|
||
| /** The filter the popover opens on. */ | ||
| const INITIAL_FILTER = CONST.SEARCH.SYNTAX_FILTER_KEYS.TYPE; | ||
|
|
||
| /** The filters whose contents are derived from the whole contact list, the one derivation heavy enough to be felt. */ | ||
| const DEFERRED_CONTENT_FILTERS = new Set<SearchFilter['key']>([ | ||
| CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, | ||
| CONST.SEARCH.SYNTAX_FILTER_KEYS.TO, | ||
| CONST.SEARCH.SYNTAX_FILTER_KEYS.ATTENDEE, | ||
| CONST.SEARCH.SYNTAX_FILTER_KEYS.ASSIGNEE, | ||
| ]); | ||
|
|
||
| type MountedFilterContentProps = { | ||
| /** The filter whose content this is. */ | ||
| filterKey: SearchFilter['key']; | ||
|
|
||
| /** The filter values the content reads. */ | ||
| values: Partial<SearchAdvancedFiltersForm> | undefined; | ||
|
|
||
| /** Whether the content may derive itself, or has to stand as its own loading state for now. */ | ||
| ready: boolean; | ||
|
|
||
| /** Called with the values a change in the content produces. */ | ||
| onChange: (values: Partial<SearchAdvancedFiltersForm>) => void; | ||
| }; | ||
|
|
||
| // React Compiler caches the whole `mountedFilters.map()` in one slot, so every element is rebuilt on each change. Each | ||
| // content sits in its own component, so a hover ends the render at the boundary of those whose props did not change. | ||
| // `onChange` changes with the form, so an edit still re-renders them all. | ||
| function MountedFilterContent({filterKey, values, ready, onChange}: MountedFilterContentProps) { | ||
| const styles = useThemeStyles(); | ||
|
|
||
| return ( | ||
| <View style={styles.flex1}> | ||
| <SearchAdvancedFiltersContent | ||
| values={values} | ||
| baseFilterKey={filterKey} | ||
| components={filterComponents} | ||
| ready={ready} | ||
| onChange={onChange} | ||
| /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function SearchAdvancedFiltersPopup({queryJSON}: SearchAdvancedFiltersPopupProps) { | ||
| const styles = useThemeStyles(); | ||
| const StyleUtils = useStyleUtils(); | ||
| const {windowHeight} = useWindowDimensions(); | ||
| const [selectedFilter, setSelectedFilter] = useState<SearchFilter['key']>(CONST.SEARCH.SYNTAX_FILTER_KEYS.TYPE); | ||
| const filterContentRef = useRef<View>(null); | ||
| const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); | ||
|
|
||
| const filterContentRef = useRef<View>(null); | ||
| const [mountedFilterState, setMountedFilterState] = useState<MountedFilterState>(() => ({ | ||
| activeFilter: INITIAL_FILTER, | ||
| mountedFilters: [INITIAL_FILTER], | ||
| formAtMount: {[INITIAL_FILTER]: searchAdvancedFiltersForm}, | ||
| formWhileHidden: {}, | ||
| contentVersions: {}, | ||
| readyFilters: [], | ||
| })); | ||
| const {activeFilter, mountedFilters, formWhileHidden, contentVersions, readyFilters} = mountedFilterState; | ||
| const {updateFilterQueryParams} = useUpdateFilterQuery(queryJSON); | ||
|
|
||
| // Shows a filter's content. A visited one stays mounted, so a return reveals it rather than building it again. | ||
| const showFilter = (filterKey: SearchFilter['key']) => { | ||
| setMountedFilterState((currentState) => { | ||
| if (currentState.activeFilter === filterKey) { | ||
| return currentState; | ||
| } | ||
|
|
||
| const isMounted = currentState.mountedFilters.includes(filterKey); | ||
| // A content decides its selection pinning on mount, so one whose values moved on since it was built is | ||
| // remounted rather than revealed. Only the values that content reads count. | ||
| const isStale = isMounted && hasFilterContentValuesChanged(filterKey, currentState.formAtMount[filterKey], searchAdvancedFiltersForm); | ||
| const isBuilt = !isMounted || isStale; | ||
|
|
||
| return { | ||
| activeFilter: filterKey, | ||
| mountedFilters: isMounted ? currentState.mountedFilters : [...currentState.mountedFilters, filterKey], | ||
| formAtMount: isBuilt ? {...currentState.formAtMount, [filterKey]: searchAdvancedFiltersForm} : currentState.formAtMount, | ||
| // The filter being left keeps the values it had while shown, so hiding it costs no render. | ||
| formWhileHidden: {...currentState.formWhileHidden, [currentState.activeFilter]: searchAdvancedFiltersForm}, | ||
| contentVersions: isStale ? {...currentState.contentVersions, [filterKey]: (currentState.contentVersions[filterKey] ?? 0) + 1} : currentState.contentVersions, | ||
| // A remounted content derives itself from scratch, so it waits for the cursor again like a new one. | ||
| readyFilters: isStale ? currentState.readyFilters.filter((key) => key !== filterKey) : currentState.readyFilters, | ||
| }; | ||
| }); | ||
| }; | ||
|
|
||
| /** Lets the shown filter derive what it withheld. It reads the filter from the updater, so a wait outlives its render. */ | ||
| const markShownFilterReady = () => { | ||
| setMountedFilterState((currentState) => { | ||
| if (!DEFERRED_CONTENT_FILTERS.has(currentState.activeFilter) || currentState.readyFilters.includes(currentState.activeFilter)) { | ||
| return currentState; | ||
| } | ||
|
|
||
| return {...currentState, readyFilters: [...currentState.readyFilters, currentState.activeFilter]}; | ||
| }); | ||
| }; | ||
|
|
||
| // Elapses once the cursor comes to rest on a row. | ||
| const {invoke: debouncedMarkShownFilterReady, cancel: cancelReadyWait} = useDebounceWithControls(markShownFilterReady, CONST.TIMING.SEARCH_FILTER_HOVER_INTENT_DELAY); | ||
|
|
||
| const hoverFilter = (filterKey: SearchFilter['key']) => { | ||
| showFilter(filterKey); | ||
| debouncedMarkShownFilterReady(); | ||
| }; | ||
|
|
||
| const {trackMovement: trackPointerMovement, stopTracking} = usePointerMovement(CONST.SEARCH.HOVER_INTENT_REST_RADIUS_PX, debouncedMarkShownFilterReady); | ||
|
|
||
| // The direction comes from the last tracked position rather than the exit point, because `SafeTriangle` covers the | ||
| // path to the content with an overlay and the cursor leaves the list above it as often as through its edge. | ||
| const stopTrackingPointer = (event: {clientX: number}) => { | ||
| const lastMovement = stopTracking(); | ||
|
|
||
| if (lastMovement && event.clientX <= lastMovement.x) { | ||
| return; | ||
| } | ||
|
|
||
| cancelReadyWait(); | ||
| markShownFilterReady(); | ||
|
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.
Sweeping the cursor off the bottom or left of the filter list — or out of the popover entirely — marks the last row ready and builds the whole contact list for a pane nobody is going to look at. Could we only release when the cursor leaves toward the content pane (e.g. checking
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. Done, but not via
sumo-slonik marked this conversation as resolved.
Resolved
|
||
| }; | ||
|
|
||
| // Moving the focus is deliberate and never passes over rows on the way, so nothing is withheld from it. | ||
| const focusFilter = (filterKey: SearchFilter['key']) => { | ||
| cancelReadyWait(); | ||
| showFilter(filterKey); | ||
| markShownFilterReady(); | ||
| }; | ||
|
|
||
| const mayDeriveContent = (filterKey: SearchFilter['key']) => !DEFERRED_CONTENT_FILTERS.has(filterKey) || readyFilters.includes(filterKey); | ||
|
|
||
| return ( | ||
| <SafeTriangle submenuRef={filterContentRef}> | ||
| <View style={[styles.flexRow, StyleUtils.getHeight(Math.min(windowHeight, CONST.ADVANCED_FILTERS_POPOVER_HEIGHT))]}> | ||
| <FilterList | ||
| style={[styles.typeFiltersPopupContainer]} | ||
| type={searchAdvancedFiltersForm?.type} | ||
| policyID={getFilterNegatableValue(CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID, searchAdvancedFiltersForm)} | ||
| selectedFilter={selectedFilter} | ||
| onHoverIn={setSelectedFilter} | ||
| onFocus={setSelectedFilter} | ||
| selectedFilter={activeFilter} | ||
| onHoverIn={hoverFilter} | ||
| onPointerMove={trackPointerMovement} | ||
| onPointerLeave={stopTrackingPointer} | ||
| onFocus={focusFilter} | ||
| /> | ||
| <View | ||
| ref={filterContentRef} | ||
| style={[styles.filterContentContainer]} | ||
| > | ||
| <SearchAdvancedFiltersContent | ||
| values={searchAdvancedFiltersForm} | ||
| baseFilterKey={selectedFilter} | ||
| components={{ | ||
| List: ListFilterContentPopupWrapper, | ||
| Text: TextInputFilterContentPopupWrapper, | ||
| Amount: AmountFilterContentPopupWrapper, | ||
| Date: DateFilterContentPopupWrapper, | ||
| ReportField: ReportFieldFilterContentPopupWrapper, | ||
| }} | ||
| onChange={updateFilterQueryParams} | ||
| /> | ||
| {mountedFilters.map((filterKey) => ( | ||
| // `Activity` keeps a hidden content mounted while taking it out of layout and unmounting its | ||
| // effects, so it holds no Onyx subscriptions until it is shown again. | ||
| <Activity | ||
| key={`${filterKey}-${contentVersions[filterKey] ?? 0}`} | ||
| mode={filterKey === activeFilter ? 'visible' : 'hidden'} | ||
| > | ||
|
sumo-slonik marked this conversation as resolved.
Resolved
|
||
| <MountedFilterContent | ||
| filterKey={filterKey} | ||
| values={filterKey === activeFilter ? searchAdvancedFiltersForm : formWhileHidden[filterKey]} | ||
| ready={mayDeriveContent(filterKey)} | ||
| onChange={updateFilterQueryParams} | ||
| /> | ||
| </Activity> | ||
| ))} | ||
| </View> | ||
| </View> | ||
| </SafeTriangle> | ||
|
|
||
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.
Is
30the intended final value? In PR description, I saw that it is 80