-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SearchTypeMenu.tsx
239 lines (220 loc) · 9.87 KB
/
SearchTypeMenu.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, {useCallback} from 'react';
import {View} from 'react-native';
import type {TextStyle, ViewStyle} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import type {MenuItemBaseProps} from '@components/MenuItem';
import MenuItem from '@components/MenuItem';
import MenuItemList from '@components/MenuItemList';
import type {MenuItemWithLink} from '@components/MenuItemList';
import {usePersonalDetails} from '@components/OnyxProvider';
import ScrollView from '@components/ScrollView';
import type {SearchQueryJSON} from '@components/Search/types';
import Text from '@components/Text';
import ThreeDotsMenu from '@components/ThreeDotsMenu';
import useDeleteSavedSearch from '@hooks/useDeleteSavedSearch';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import * as SearchActions from '@libs/actions/Search';
import Navigation from '@libs/Navigation/Navigation';
import {getAllTaxRates} from '@libs/PolicyUtils';
import * as SearchUtils from '@libs/SearchUtils';
import variables from '@styles/variables';
import * as Expensicons from '@src/components/Icon/Expensicons';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import type {SaveSearchItem} from '@src/types/onyx/SaveSearch';
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
import type IconAsset from '@src/types/utils/IconAsset';
import SearchTypeMenuNarrow from './SearchTypeMenuNarrow';
type SavedSearchMenuItem = MenuItemBaseProps & {
key: string;
hash: string;
query: string;
styles: Array<ViewStyle | TextStyle>;
};
type SearchTypeMenuProps = {
queryJSON: SearchQueryJSON;
};
type SearchTypeMenuItem = {
title: string;
type: SearchDataTypes;
icon: IconAsset;
route?: Route;
};
function SearchTypeMenu({queryJSON}: SearchTypeMenuProps) {
const {type, hash} = queryJSON;
const styles = useThemeStyles();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {singleExecution} = useSingleExecution();
const {translate} = useLocalize();
const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES);
const [shouldHideSavedSearchRenameTooltip] = useOnyx(ONYXKEYS.NVP_SHOULD_HIDE_SAVED_SEARCH_RENAME_TOOLTIP, {initialValue: true});
const {showDeleteModal, DeleteConfirmModal} = useDeleteSavedSearch();
const personalDetails = usePersonalDetails();
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const taxRates = getAllTaxRates();
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const typeMenuItems: SearchTypeMenuItem[] = [
{
title: translate('common.expenses'),
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
icon: Expensicons.Receipt,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery()}),
},
{
title: translate('common.chats'),
type: CONST.SEARCH.DATA_TYPES.CHAT,
icon: Expensicons.ChatBubbles,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.TRIP.ALL)}),
},
{
title: translate('workspace.common.invoices'),
type: CONST.SEARCH.DATA_TYPES.INVOICE,
icon: Expensicons.InvoiceGeneric,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.STATUS.INVOICE.ALL)}),
},
{
title: translate('travel.trips'),
type: CONST.SEARCH.DATA_TYPES.TRIP,
icon: Expensicons.Suitcase,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.STATUS.TRIP.ALL)}),
},
];
const getOverflowMenu = useCallback(
(itemName: string, itemHash: number, itemQuery: string) => SearchUtils.getOverflowMenu(itemName, itemHash, itemQuery, showDeleteModal),
[showDeleteModal],
);
const createSavedSearchMenuItem = useCallback(
(item: SaveSearchItem, key: string, isNarrow: boolean) => {
const baseMenuItem: SavedSearchMenuItem = {
key,
title: item.name,
hash: key,
query: item.query,
shouldShowRightComponent: true,
focused: Number(key) === hash,
onPress: () => {
SearchActions.clearAllFilters();
Navigation.navigate(ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: item?.query ?? ''}));
},
rightComponent: (
<ThreeDotsMenu
menuItems={getOverflowMenu(item.name, Number(key), item.query)}
anchorPosition={{horizontal: 0, vertical: 380}}
anchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP,
}}
/>
),
styles: [styles.alignItemsCenter],
};
if (!isNarrow) {
return {
...baseMenuItem,
shouldRenderTooltip: !shouldHideSavedSearchRenameTooltip,
tooltipAnchorAlignment: {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
},
tooltipShiftHorizontal: -32,
tooltipShiftVertical: 15,
tooltipWrapperStyle: [styles.bgPaleGreen, styles.mh4, styles.pv2],
renderTooltipContent: () => {
SearchActions.dismissSavedSearchRenameTooltip();
return (
<View style={[styles.flexRow, styles.alignItemsCenter]}>
<Expensicons.Lightbulb
width={16}
height={16}
fill={styles.colorGreenSuccess.color}
/>
<Text style={[styles.ml1, styles.textLabel]}>{translate('search.saveSearchTooltipText')}</Text>
</View>
);
},
};
}
return baseMenuItem;
},
[hash, styles, getOverflowMenu, translate, shouldHideSavedSearchRenameTooltip],
);
const savedSearchesMenuItems = () => {
if (!savedSearches) {
return [];
}
return Object.entries(savedSearches).map(([key, item]) => createSavedSearchMenuItem(item as SaveSearchItem, key, shouldUseNarrowLayout));
};
const renderSavedSearchesSection = useCallback(
(menuItems: MenuItemWithLink[]) => (
<View style={[styles.pb4, styles.mh3, styles.mt3]}>
<Text style={[styles.sectionTitle, styles.pb1]}>{translate('search.savedSearchesMenuItemTitle')}</Text>
<MenuItemList
menuItems={menuItems}
wrapperStyle={styles.sectionMenuItem}
icon={Expensicons.Bookmark}
iconWidth={variables.iconSizeNormal}
iconHeight={variables.iconSizeNormal}
shouldUseSingleExecution
isPaneMenu
/>
</View>
),
[styles, translate],
);
const isCannedQuery = SearchUtils.isCannedSearchQuery(queryJSON);
const activeItemIndex = isCannedQuery ? typeMenuItems.findIndex((item) => item.type === type) : -1;
if (shouldUseNarrowLayout) {
const title = isCannedQuery ? undefined : SearchUtils.getSearchHeaderTitle(queryJSON, personalDetails, cardList, reports, taxRates);
return (
<SearchTypeMenuNarrow
typeMenuItems={typeMenuItems}
activeItemIndex={activeItemIndex}
queryJSON={queryJSON}
title={title}
savedSearchesMenuItems={savedSearchesMenuItems()}
/>
);
}
return (
<>
<View style={[styles.pb4, styles.mh3, styles.mt3]}>
{typeMenuItems.map((item, index) => {
const onPress = singleExecution(() => {
SearchActions.clearAllFilters();
Navigation.navigate(item.route);
});
return (
<MenuItem
key={item.title}
disabled={false}
interactive
title={item.title}
icon={item.icon}
iconWidth={variables.iconSizeNormal}
iconHeight={variables.iconSizeNormal}
wrapperStyle={styles.sectionMenuItem}
focused={index === activeItemIndex}
hoverAndPressStyle={styles.hoveredComponentBG}
onPress={onPress}
isPaneMenu
/>
);
})}
</View>
{savedSearches && Object.keys(savedSearches).length > 0 && (
<>
<ScrollView>{renderSavedSearchesSection(savedSearchesMenuItems())}</ScrollView>
<DeleteConfirmModal />
</>
)}
</>
);
}
SearchTypeMenu.displayName = 'SearchTypeMenu';
export default SearchTypeMenu;
export type {SearchTypeMenuItem};