-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SearchRouterList.tsx
195 lines (170 loc) · 7.49 KB
/
SearchRouterList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, {forwardRef, useCallback} from 'react';
import type {ForwardedRef} from 'react';
import {useOnyx} from 'react-native-onyx';
import * as Expensicons from '@components/Icon/Expensicons';
import {usePersonalDetails} from '@components/OnyxProvider';
import type {SearchQueryJSON} from '@components/Search/types';
import SelectionList from '@components/SelectionList';
import SearchQueryListItem from '@components/SelectionList/Search/SearchQueryListItem';
import type {SearchQueryItem, SearchQueryListItemProps} from '@components/SelectionList/Search/SearchQueryListItem';
import type {SectionListDataType, SelectionListHandle, UserListItemProps} from '@components/SelectionList/types';
import UserListItem from '@components/SelectionList/UserListItem';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import Performance from '@libs/Performance';
import {getAllTaxRates} from '@libs/PolicyUtils';
import type {OptionData} from '@libs/ReportUtils';
import * as SearchUtils from '@libs/SearchUtils';
import * as Report from '@userActions/Report';
import Timing from '@userActions/Timing';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
type ItemWithQuery = {
query: string;
};
type SearchRouterListProps = {
/** currentQuery value computed coming from parsed TextInput value */
currentQuery: SearchQueryJSON | undefined;
/** Recent searches */
recentSearches: ItemWithQuery[] | undefined;
/** Recent reports */
recentReports: OptionData[];
/** Callback to submit query when selecting a list item */
onSearchSubmit: (query: SearchQueryJSON | undefined) => void;
/** Context present when opening SearchRouter from a report, invoice or workspace page */
reportForContextualSearch?: OptionData;
/** Callback to update search query when selecting contextual suggestion */
updateUserSearchQuery: (newSearchQuery: string) => void;
/** Callback to close and clear SearchRouter */
closeAndClearRouter: () => void;
};
const setPerformanceTimersEnd = () => {
Timing.end(CONST.TIMING.SEARCH_ROUTER_RENDER);
Performance.markEnd(CONST.TIMING.SEARCH_ROUTER_RENDER);
};
function isSearchQueryItem(item: OptionData | SearchQueryItem): item is SearchQueryItem {
if ('singleIcon' in item && item.singleIcon && 'query' in item && item.query) {
return true;
}
return false;
}
function isSearchQueryListItem(listItem: UserListItemProps<OptionData> | SearchQueryListItemProps): listItem is SearchQueryListItemProps {
return isSearchQueryItem(listItem.item);
}
function SearchRouterItem(props: UserListItemProps<OptionData> | SearchQueryListItemProps) {
const styles = useThemeStyles();
if (isSearchQueryListItem(props)) {
return (
<SearchQueryListItem
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
);
}
return (
<UserListItem
pressableStyle={[styles.br2]}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
);
}
function SearchRouterList(
{currentQuery, reportForContextualSearch, recentSearches, recentReports, onSearchSubmit, updateUserSearchQuery, closeAndClearRouter}: SearchRouterListProps,
ref: ForwardedRef<SelectionListHandle>,
) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isSmallScreenWidth} = useResponsiveLayout();
const personalDetails = usePersonalDetails();
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const taxRates = getAllTaxRates();
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const sections: Array<SectionListDataType<OptionData | SearchQueryItem>> = [];
if (currentQuery?.inputQuery) {
sections.push({
data: [
{
text: currentQuery?.inputQuery,
singleIcon: Expensicons.MagnifyingGlass,
query: currentQuery?.inputQuery,
itemStyle: styles.activeComponentBG,
keyForList: 'findItem',
},
],
});
}
if (reportForContextualSearch && !currentQuery?.inputQuery) {
sections.push({
data: [
{
text: `${translate('search.searchIn')} ${reportForContextualSearch.text ?? reportForContextualSearch.alternateText}`,
singleIcon: Expensicons.MagnifyingGlass,
query: SearchUtils.getContextualSuggestionQuery(reportForContextualSearch.reportID),
itemStyle: styles.activeComponentBG,
keyForList: 'contextualSearch',
isContextualSearchItem: true,
},
],
});
}
const recentSearchesData = recentSearches?.map(({query}) => {
const searchQueryJSON = SearchUtils.buildSearchQueryJSON(query);
return {
text: searchQueryJSON ? SearchUtils.getSearchHeaderTitle(searchQueryJSON, personalDetails, cardList, reports, taxRates) : query,
singleIcon: Expensicons.History,
query,
keyForList: query,
};
});
if (!currentQuery?.inputQuery && recentSearchesData && recentSearchesData.length > 0) {
sections.push({title: translate('search.recentSearches'), data: recentSearchesData});
}
const styledRecentReports = recentReports.map((item) => ({...item, pressableStyle: styles.br2, wrapperStyle: [styles.pr3, styles.pl3]}));
sections.push({title: translate('search.recentChats'), data: styledRecentReports});
const onSelectRow = useCallback(
(item: OptionData | SearchQueryItem) => {
if (isSearchQueryItem(item)) {
if (item.isContextualSearchItem) {
// Handle selection of "Contextual search suggestion"
updateUserSearchQuery(`${item?.query} ${currentQuery?.inputQuery ?? ''}`);
return;
}
// Handle selection of "Recent search"
if (!item?.query) {
return;
}
onSearchSubmit(SearchUtils.buildSearchQueryJSON(item?.query));
}
// Handle selection of "Recent chat"
closeAndClearRouter();
if ('reportID' in item && item?.reportID) {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(item?.reportID));
} else if ('login' in item) {
Report.navigateToAndOpenReport(item?.login ? [item.login] : []);
}
},
[closeAndClearRouter, onSearchSubmit, currentQuery, updateUserSearchQuery],
);
return (
<SelectionList<OptionData | SearchQueryItem>
sections={sections}
onSelectRow={onSelectRow}
ListItem={SearchRouterItem}
containerStyle={[styles.mh100]}
sectionListStyle={[isSmallScreenWidth ? styles.ph5 : styles.ph2, styles.pb2]}
listItemWrapperStyle={[styles.pr3, styles.pl3]}
onLayout={setPerformanceTimersEnd}
ref={ref}
showScrollIndicator={!isSmallScreenWidth}
sectionTitleStyles={styles.mhn2}
shouldSingleExecuteRowSelect
/>
);
}
export default forwardRef(SearchRouterList);
export {SearchRouterItem};
export type {ItemWithQuery};