11import React , { useEffect } from 'react' ;
22import { View } from 'react-native' ;
3+ import type { TupleToUnion } from 'type-fest' ;
34import ActivityIndicator from '@components/ActivityIndicator' ;
45import Badge from '@components/Badge' ;
56import Icon from '@components/Icon' ;
67import MenuItem from '@components/MenuItem' ;
78import OfflineWithFeedback from '@components/OfflineWithFeedback' ;
9+ import SearchBar from '@components/SearchBar' ;
810import Section from '@components/Section' ;
911import Text from '@components/Text' ;
1012import useConfirmModal from '@hooks/useConfirmModal' ;
@@ -16,11 +18,13 @@ import useLocalize from '@hooks/useLocalize';
1618import useNetwork from '@hooks/useNetwork' ;
1719import useOnyx from '@hooks/useOnyx' ;
1820import useResponsiveLayout from '@hooks/useResponsiveLayout' ;
21+ import useSearchResults from '@hooks/useSearchResults' ;
1922import useStyleUtils from '@hooks/useStyleUtils' ;
2023import useTheme from '@hooks/useTheme' ;
2124import useThemeStyles from '@hooks/useThemeStyles' ;
2225import { openPolicyExpensifyCardsPage } from '@libs/actions/Policy/Policy' ;
2326import Navigation from '@libs/Navigation/Navigation' ;
27+ import tokenizedSearch from '@libs/tokenizedSearch' ;
2428import variables from '@styles/variables' ;
2529import CONST from '@src/CONST' ;
2630import ONYXKEYS from '@src/ONYXKEYS' ;
@@ -45,6 +49,13 @@ function SpendRulesSection({policyID}: SpendRulesSectionProps) {
4549 const [ expensifyCardSettings ] = useOnyx ( `${ ONYXKEYS . COLLECTION . PRIVATE_EXPENSIFY_CARD_SETTINGS } ${ defaultFundID } ` ) ;
4650 const { cardRules, isLoadingCardRules} = useExpensifyCardRules ( policyID ) ;
4751
52+ const filterCardRules = ( cardRule : TupleToUnion < typeof cardRules > , searchInput : string ) => {
53+ const results = tokenizedSearch ( [ cardRule ] , searchInput , ( option ) => option . searchTokens ) ;
54+ return results . length > 0 ;
55+ } ;
56+
57+ const [ inputValue , setInputValue , filteredCardRules ] = useSearchResults ( cardRules , filterCardRules ) ;
58+
4859 useEffect ( ( ) => {
4960 if ( ! defaultFundID || defaultFundID === CONST . DEFAULT_NUMBER_ID ) {
5061 return ;
@@ -132,8 +143,17 @@ function SpendRulesSection({policyID}: SpendRulesSectionProps) {
132143 isCentralPane
133144 subtitleMuted
134145 >
146+ { cardRules . length >= CONST . STANDARD_LIST_ITEM_LIMIT && (
147+ < SearchBar
148+ label = { translate ( 'workspace.card.searchRules' ) }
149+ inputValue = { inputValue }
150+ onChangeText = { setInputValue }
151+ shouldShowEmptyState = { ! isLoadingCardRules && filteredCardRules . length === 0 }
152+ style = { [ styles . mb0 , styles . mt5 , styles . mh0 ] }
153+ />
154+ ) }
135155 < MenuItem
136- wrapperStyle = { [ styles . borderedContentCard , styles . mt6 , styles . ph4 , styles . pv4 ] }
156+ wrapperStyle = { [ styles . borderedContentCard , styles . mt5 , styles . ph4 , styles . pv4 ] }
137157 titleComponent = { menuItemBody }
138158 accessibilityLabel = { `${ descriptionLabel } . ${ blockLabel } ${ defaultRuleTitle } ` }
139159 sentryLabel = { CONST . SENTRY_LABEL . WORKSPACE . RULES . SPEND_RULE_ITEM }
@@ -152,7 +172,7 @@ function SpendRulesSection({policyID}: SpendRulesSectionProps) {
152172 />
153173 </ View >
154174 ) : (
155- cardRules . map ( ( rule ) => (
175+ filteredCardRules . map ( ( rule ) => (
156176 < OfflineWithFeedback
157177 key = { rule . ruleID }
158178 pendingAction = { rule . pendingAction }
0 commit comments