Skip to content

Commit e6a4563

Browse files
committed
use selection on session
1 parent ee60688 commit e6a4563

3 files changed

Lines changed: 21 additions & 19 deletions

File tree

src/hooks/useSearchTypeMenuSections.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import useOnyx from './useOnyx';
99
* currently focused search, based on the hash
1010
*/
1111
const useSearchTypeMenuSections = (hash = 0) => {
12-
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
12+
const [session] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => ({currentUserLogin: session?.email, currentUserAccountID: session?.accountID}), canBeMissing: false});
1313
const [allFeeds] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER, {canBeMissing: true});
1414
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
1515

16-
const typeMenuSections = useMemo(() => createTypeMenuSections(session, allFeeds, allPolicies), [allPolicies, allFeeds, session]);
16+
const typeMenuSections = useMemo(
17+
() => createTypeMenuSections(session?.currentUserLogin, session?.currentUserAccountID, allFeeds, allPolicies),
18+
[session?.currentUserLogin, session?.currentUserAccountID, allPolicies, allFeeds],
19+
);
1720

1821
const currentSearch = useMemo(() => {
1922
const flatMenuItems = typeMenuSections.map((section) => section.menuItems).flat();

src/libs/SearchUIUtils.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,12 +1300,11 @@ function isCorrectSearchUserName(displayName?: string) {
13001300
}
13011301

13021302
function createTypeMenuSections(
1303-
session: OnyxTypes.Session | undefined,
1303+
currentUserLogin: string | undefined,
1304+
currentUserAccountID: number | undefined,
13041305
feeds: OnyxCollection<OnyxTypes.CardFeeds>,
13051306
policies: OnyxCollection<OnyxTypes.Policy> = {},
13061307
): SearchTypeMenuSection[] {
1307-
const email = session?.email;
1308-
13091308
// Start building the sections by requiring the following sections to always be present
13101309
const typeMenuSections: SearchTypeMenuSection[] = [
13111310
{
@@ -1354,7 +1353,7 @@ function createTypeMenuSections(
13541353
}
13551354

13561355
const reimburser = policy.reimburser;
1357-
const isReimburser = reimburser === email;
1356+
const isReimburser = reimburser === currentUserLogin;
13581357
const isAdmin = policy.role === CONST.POLICY.ROLE.ADMIN;
13591358

13601359
if (policy.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
@@ -1369,34 +1368,34 @@ function createTypeMenuSections(
13691368
});
13701369

13711370
const showApproveSuggestion = Object.values(policies).some((policy): policy is OnyxTypes.Policy => {
1372-
if (!policy || !email || !isPaidGroupPolicy(policy)) {
1371+
if (!policy || !currentUserLogin || !isPaidGroupPolicy(policy)) {
13731372
return false;
13741373
}
13751374

13761375
if (policy.approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL) {
13771376
return false;
13781377
}
13791378

1380-
const isPolicyApprover = policy.approver === email;
1379+
const isPolicyApprover = policy.approver === currentUserLogin;
13811380
const isSubmittedTo = Object.values(policy.employeeList ?? {}).some((employee) => {
1382-
return employee.submitsTo === email || employee.forwardsTo === email;
1381+
return employee.submitsTo === currentUserLogin || employee.forwardsTo === currentUserLogin;
13831382
});
13841383

13851384
return isPolicyApprover || isSubmittedTo;
13861385
});
13871386

13881387
const showExportSuggestion = Object.values(policies).some((policy): policy is OnyxTypes.Policy => {
1389-
if (!policy || !email) {
1388+
if (!policy || !currentUserLogin) {
13901389
return false;
13911390
}
13921391

1393-
return policy.exporter === email;
1392+
return policy.exporter === currentUserLogin;
13941393
});
13951394
// We suggest specific filters for users based on their access in specific policies. Show the todo section
13961395
// only if any of these items are available
13971396
const showTodoSection = showSubmitSuggestion || showApproveSuggestion || showPaySuggestion || showExportSuggestion;
13981397

1399-
if (showTodoSection && session) {
1398+
if (showTodoSection && currentUserAccountID) {
14001399
const section: SearchTypeMenuSection = {
14011400
translationPath: 'common.todo',
14021401
menuItems: [],
@@ -1422,7 +1421,7 @@ function createTypeMenuSections(
14221421
buttonAction: () => {
14231422
interceptAnonymousUser(() => {
14241423
const activePolicy = getActivePolicy();
1425-
const personalDetails = getPersonalDetailsForAccountID(session.accountID) as OnyxTypes.PersonalDetails;
1424+
const personalDetails = getPersonalDetailsForAccountID(currentUserAccountID) as OnyxTypes.PersonalDetails;
14261425

14271426
let workspaceIDForReportCreation: string | undefined;
14281427

@@ -1454,7 +1453,7 @@ function createTypeMenuSections(
14541453
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
14551454
groupBy: CONST.SEARCH.GROUP_BY.REPORTS,
14561455
status: CONST.SEARCH.STATUS.EXPENSE.DRAFTS,
1457-
from: [`${session.accountID}`],
1456+
from: [`${currentUserAccountID}`],
14581457
});
14591458
return queryString;
14601459
},
@@ -1477,7 +1476,7 @@ function createTypeMenuSections(
14771476
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
14781477
groupBy: CONST.SEARCH.GROUP_BY.REPORTS,
14791478
action: CONST.SEARCH.ACTION_FILTERS.APPROVE,
1480-
to: [`${session.accountID}`],
1479+
to: [`${currentUserAccountID}`],
14811480
});
14821481
return queryString;
14831482
},
@@ -1501,7 +1500,7 @@ function createTypeMenuSections(
15011500
groupBy: CONST.SEARCH.GROUP_BY.REPORTS,
15021501
action: CONST.SEARCH.ACTION_FILTERS.PAY,
15031502
reimbursable: CONST.SEARCH.BOOLEAN.YES,
1504-
payer: session.accountID?.toString(),
1503+
payer: currentUserAccountID?.toString(),
15051504
});
15061505
return queryString;
15071506
},
@@ -1524,7 +1523,7 @@ function createTypeMenuSections(
15241523
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
15251524
groupBy: CONST.SEARCH.GROUP_BY.REPORTS,
15261525
action: CONST.SEARCH.ACTION_FILTERS.EXPORT,
1527-
exporter: [`${session.accountID}`],
1526+
exporter: [`${currentUserAccountID}`],
15281527
exportedOn: CONST.SEARCH.DATE_PRESETS.NEVER,
15291528
});
15301529
return queryString;

tests/unit/Search/SearchUIUtilsTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ describe('SearchUIUtils', () => {
13211321

13221322
describe('Test createTypeMenuItems', () => {
13231323
it('should return the default menu items', () => {
1324-
const menuItems = SearchUIUtils.createTypeMenuSections(undefined, {}, {})
1324+
const menuItems = SearchUIUtils.createTypeMenuSections(undefined, undefined, {}, {})
13251325
.map((section) => section.menuItems)
13261326
.flat();
13271327

@@ -1348,7 +1348,7 @@ describe('SearchUIUtils', () => {
13481348
});
13491349

13501350
it('should generate correct routes', () => {
1351-
const menuItems = SearchUIUtils.createTypeMenuSections(undefined, {}, {})
1351+
const menuItems = SearchUIUtils.createTypeMenuSections(undefined, undefined, {}, {})
13521352
.map((section) => section.menuItems)
13531353
.flat();
13541354

0 commit comments

Comments
 (0)