Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 57 additions & 28 deletions src/components/Search/SearchPageHeader/SearchFiltersBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {emailSelector} from '@selectors/Session';
import {searchResultsErrorSelector} from '@selectors/Snapshot';
import React, {useCallback, useContext, useEffect, useMemo, useRef} from 'react';
import type {ReactNode} from 'react';
import {View} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {ScrollView as RNScrollView} from 'react-native';
import {FlatList, View} from 'react-native';
import Button from '@components/Button';
import ButtonWithDropdownMenu from '@components/ButtonWithDropdownMenu';
import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types';
Expand All @@ -15,7 +13,6 @@ import {KYCWallContext} from '@components/KYCWall/KYCWallContext';
import type {PaymentMethodType} from '@components/KYCWall/types';
import {LockedAccountContext} from '@components/LockedAccountModalProvider';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import ScrollView from '@components/ScrollView';
import type {SearchDateValues} from '@components/Search/FilterComponents/DatePresetFilterBase';
import DateSelectPopup from '@components/Search/FilterDropdowns/DateSelectPopup';
import type {PopoverComponentProps} from '@components/Search/FilterDropdowns/DropdownButton';
Expand Down Expand Up @@ -48,6 +45,7 @@ import {getActiveAdminWorkspaces, getAllTaxRates, isPaidGroupPolicy} from '@libs
import {isExpenseReport} from '@libs/ReportUtils';
import {buildFilterFormValuesFromQuery, buildQueryStringFromFilterFormValues, isFilterSupported, isSearchDatePreset} from '@libs/SearchQueryUtils';
import {getDatePresets, getFeedOptions, getGroupByOptions, getGroupCurrencyOptions, getHasOptions, getStatusOptions, getTypeOptions, getWithdrawalTypeOptions} from '@libs/SearchUIUtils';
import shouldAdjustScroll from '@libs/shouldAdjustScroll';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -86,7 +84,7 @@ function SearchFiltersBar({
latestBankItems,
}: SearchFiltersBarProps) {
const isFocused = useIsFocused();
const scrollRef = useRef<RNScrollView>(null);
const scrollRef = useRef<FlatList<FilterItem>>(null);
const currentPolicy = usePolicy(currentSelectedPolicyID);
const [isUserValidated] = useOnyx(ONYXKEYS.ACCOUNT, {selector: isUserValidatedSelector, canBeMissing: true});
// type, groupBy and status values are not guaranteed to respect the ts type as they come from user input
Expand Down Expand Up @@ -737,6 +735,52 @@ function SearchFiltersBar({
});
}, [filterFormValues, filters, typeFiltersKeys]);

const adjustScroll = useCallback((info: {distanceFromEnd: number}) => {
// Workaround for a known React Native bug on Android (https://github.com/facebook/react-native/issues/27504):
// When the FlatList is scrolled to the end and the last item is deleted, a blank space is left behind.
// To fix this, we detect when onEndReached is triggered due to an item deletion,
// and programmatically scroll to the end to fill the space.
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (!shouldAdjustScroll || info.distanceFromEnd > 0) {
return;
}
scrollRef.current?.scrollToEnd();
}, []);

const renderFilterItem = useCallback(
// eslint-disable-next-line react/no-unused-prop-types
({item}: {item: FilterItem}) => (
<DropdownButton
label={item.label}
value={item.value}
PopoverComponent={item.PopoverComponent}
/>
),
[],
);

const filterButtonText = useMemo(
() => translate('search.filtersHeader') + (hiddenSelectedFilters.length > 0 ? ` (${hiddenSelectedFilters.length})` : ''),
[translate, hiddenSelectedFilters.length],
);

const renderListFooter = useCallback(
() => (
<Button
link
small
shouldUseDefaultHover={false}
text={filterButtonText}
iconFill={theme.link}
iconHoverFill={theme.linkHover}
icon={expensifyIcons.Filter}
textStyles={[styles.textMicroBold]}
onPress={openAdvancedFilters}
/>
),
[filterButtonText, theme.link, theme.linkHover, styles.textMicroBold, openAdvancedFilters, expensifyIcons],
);

if (hasErrors) {
return null;
}
Expand Down Expand Up @@ -802,35 +846,20 @@ function SearchFiltersBar({
)}
</KYCWall>
) : (
<ScrollView
<FlatList
horizontal
keyboardShouldPersistTaps="always"
style={[styles.flexRow, styles.overflowScroll, styles.flexGrow0]}
contentContainerStyle={[styles.flexRow, styles.flexGrow0, styles.gap2, styles.ph5]}
ref={scrollRef}
showsHorizontalScrollIndicator={false}
>
{filters.map((filter) => (
<DropdownButton
key={filter.label}
label={filter.label}
value={filter.value}
PopoverComponent={filter.PopoverComponent}
/>
))}

<Button
link
small
shouldUseDefaultHover={false}
text={translate('search.filtersHeader') + (hiddenSelectedFilters.length > 0 ? ` (${hiddenSelectedFilters.length})` : '')}
iconFill={theme.link}
iconHoverFill={theme.linkHover}
icon={expensifyIcons.Filter}
textStyles={[styles.textMicroBold]}
onPress={openAdvancedFilters}
/>
</ScrollView>
data={filters}
keyExtractor={(item) => item.label}
renderItem={renderFilterItem}
ListFooterComponent={renderListFooter}
onEndReached={adjustScroll}
onEndReachedThreshold={0.75}
/>
)}
</View>
);
Expand Down
Loading