-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathuseSearchTypeMenuSections.ts
More file actions
194 lines (171 loc) · 7.47 KB
/
Copy pathuseSearchTypeMenuSections.ts
File metadata and controls
194 lines (171 loc) · 7.47 KB
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
import {createTypeMenuSections, doesSearchItemMatchSort} from '@libs/SearchUIUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {isTrackIntentUserSelector} from '@src/selectors/Onboarding';
import type {Policy, Session} from '@src/types/onyx';
import type {OnyxEntry} from 'react-native-onyx';
import {defaultExpensifyCardSelector} from '@selectors/Card';
import {validTransactionDraftIDsSelector} from '@selectors/TransactionDraft';
import {useCallback, useEffect, useMemo, useState} from 'react';
import useCardFeedsForDisplay from './useCardFeedsForDisplay';
import useCreateEmptyReportConfirmation from './useCreateEmptyReportConfirmation';
import useMappedPolicies from './useMappedPolicies';
import useNetwork from './useNetwork';
import useOnyx from './useOnyx';
const policyMapper = (policy: OnyxEntry<Policy>): OnyxEntry<Policy> =>
policy && {
id: policy.id,
name: policy.name,
type: policy.type,
role: policy.role,
owner: policy.owner,
connections: policy.connections,
outputCurrency: policy.outputCurrency,
isPolicyExpenseChatEnabled: policy.isPolicyExpenseChatEnabled,
isJoinRequestPending: policy.isJoinRequestPending,
pendingAction: policy.pendingAction,
errors: policy.errors,
reimburser: policy.reimburser,
exporter: policy.exporter,
approver: policy.approver,
approvalMode: policy.approvalMode,
employeeList: policy.employeeList,
reimbursementChoice: policy.reimbursementChoice,
areCompanyCardsEnabled: policy.areCompanyCardsEnabled,
areExpensifyCardsEnabled: policy.areExpensifyCardsEnabled,
achAccount: policy.achAccount,
areCategoriesEnabled: policy.areCategoriesEnabled,
areWorkflowsEnabled: policy.areWorkflowsEnabled,
};
const currentUserLoginAndAccountIDSelector = (session: OnyxEntry<Session>) => ({
email: session?.email,
accountID: session?.accountID,
});
type UseSearchTypeMenuSectionsParams = {
hash?: number;
similarSearchHash?: number;
sortBy?: string;
sortOrder?: string;
type?: string;
};
/**
* Get a list of all search groupings, along with their search items. Also returns the
* currently focused search, based on the hash
*/
const useSearchTypeMenuSections = (queryParams?: UseSearchTypeMenuSectionsParams) => {
const {hash, similarSearchHash, sortBy, sortOrder, type} = queryParams ?? {};
const [defaultExpensifyCard] = useOnyx(ONYXKEYS.DERIVED.NON_PERSONAL_AND_WORKSPACE_CARD_LIST, {selector: defaultExpensifyCardSelector});
const {defaultCardFeed, cardFeedsByPolicy} = useCardFeedsForDisplay();
const {isOffline} = useNetwork();
const [allPolicies] = useMappedPolicies(policyMapper);
const [currentUserLoginAndAccountID] = useOnyx(ONYXKEYS.SESSION, {selector: currentUserLoginAndAccountIDSelector});
const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES);
const [draftTransactionIDs] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {selector: validTransactionDraftIDsSelector});
const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector});
const [pendingReportCreation, setPendingReportCreation] = useState<{policyID: string; policyName?: string; onConfirm: (shouldDismissEmptyReportsConfirmation: boolean) => void} | null>(
null,
);
const handlePendingConfirm = useCallback(
(shouldDismissEmptyReportsConfirmation: boolean) => {
pendingReportCreation?.onConfirm(shouldDismissEmptyReportsConfirmation);
setPendingReportCreation(null);
},
[pendingReportCreation, setPendingReportCreation],
);
const handlePendingCancel = useCallback(() => {
setPendingReportCreation(null);
}, [setPendingReportCreation]);
const {openCreateReportConfirmation} = useCreateEmptyReportConfirmation({
policyID: pendingReportCreation?.policyID,
policyName: pendingReportCreation?.policyName ?? '',
onConfirm: handlePendingConfirm,
onCancel: handlePendingCancel,
});
useEffect(() => {
if (!pendingReportCreation) {
return;
}
openCreateReportConfirmation();
}, [pendingReportCreation, openCreateReportConfirmation]);
const typeMenuSections = useMemo(
() =>
createTypeMenuSections({
currentUserEmail: currentUserLoginAndAccountID?.email,
currentUserAccountID: currentUserLoginAndAccountID?.accountID,
cardFeedsByPolicy,
defaultCardFeed: defaultCardFeed ?? defaultExpensifyCard,
policies: allPolicies,
savedSearches,
isOffline,
defaultExpensifyCard,
draftTransactionIDs,
isTrackIntentUser: isTrackIntentUser ?? false,
}),
[
currentUserLoginAndAccountID?.email,
currentUserLoginAndAccountID?.accountID,
cardFeedsByPolicy,
defaultCardFeed,
defaultExpensifyCard,
allPolicies,
savedSearches,
isOffline,
draftTransactionIDs,
isTrackIntentUser,
],
);
const activeItemIndex = useMemo(() => {
const isSavedSearchActive =
hash !== undefined &&
!!savedSearches &&
Object.entries(savedSearches).some(([key, item]) => {
if (Number(key) !== hash) {
return false;
}
if (item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !isOffline) {
return false;
}
return true;
});
if (isSavedSearchActive) {
return -1;
}
let index = 0;
for (const section of typeMenuSections) {
const found = section.menuItems.findIndex((item) => {
if (item.similarSearchHash !== similarSearchHash) {
return false;
}
return doesSearchItemMatchSort(item.key, item.searchQueryJSON?.sortBy, item.searchQueryJSON?.sortOrder, sortBy, sortOrder);
});
if (found !== -1) {
return index + found;
}
index += section.menuItems.length;
}
// Fallback: if no exact match found, select the generic search key matching the type
const typeToGenericKey: Record<string, string> = {
[CONST.SEARCH.DATA_TYPES.EXPENSE]: CONST.SEARCH.SEARCH_KEYS.EXPENSES,
[CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT]: CONST.SEARCH.SEARCH_KEYS.REPORTS,
};
const fallbackKey = type ? typeToGenericKey[type] : undefined;
if (fallbackKey) {
let fallbackIndex = 0;
for (const section of typeMenuSections) {
const found = section.menuItems.findIndex((item) => item.key === fallbackKey);
if (found !== -1) {
return fallbackIndex + found;
}
fallbackIndex += section.menuItems.length;
}
}
return -1;
}, [typeMenuSections, savedSearches, hash, similarSearchHash, sortBy, sortOrder, type, isOffline]);
const activeKey = activeItemIndex < 0 ? undefined : typeMenuSections.flatMap((section) => section.menuItems).at(activeItemIndex)?.key;
return {
typeMenuSections,
activeItemIndex,
activeKey,
};
};
export default useSearchTypeMenuSections;