Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/hooks/useSearchTypeMenuSections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {SearchKey} from '@libs/SearchKeyUtils';
import {createTypeMenuSections, SPEND_INSIGHT_KEYS} from '@libs/SearchUIUtils';
import {createTypeMenuSections, omitInsightsPageMenuItems} from '@libs/SearchUIUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -144,8 +143,6 @@ const useSearchTypeMenuSections = (isScreenFocused = true) => {
return typeMenuSections;
};

const spendInsightKeys = new Set<SearchKey>(SPEND_INSIGHT_KEYS);

const useSearchTypeMenuSectionsForNavigation = (isScreenFocused = true) => {
const typeMenuSections = useSearchTypeMenuSections(isScreenFocused);
const {isBetaEnabled} = usePermissions();
Expand All @@ -154,13 +151,7 @@ const useSearchTypeMenuSectionsForNavigation = (isScreenFocused = true) => {
return typeMenuSections;
}

return typeMenuSections.flatMap((section) => {
const menuItems = section.menuItems.filter((item) => !spendInsightKeys.has(item.key));
if (menuItems.length === section.menuItems.length) {
return section;
}
return menuItems.length > 0 ? {...section, menuItems} : [];
});
return omitInsightsPageMenuItems(typeMenuSections);
};

export default useSearchTypeMenuSections;
Expand Down
13 changes: 13 additions & 0 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4678,6 +4678,8 @@ const SPEND_INSIGHT_KEYS = [
CONST.SEARCH.SEARCH_KEYS.TOP_MERCHANTS,
] as const satisfies SearchKey[];

const insightsPageMenuKeys = new Set<SearchKey>([...SPEND_INSIGHT_KEYS, CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Neil's AI agent)

NAB: insightsPageMenuKeys and insightsSearchKeys in createTypeMenuSections hold the same list. Can the Insights section loop iterate insightsPageMenuKeys instead (a Set keeps insertion order), so a future insight can't be added to one and missed in the other?


type TypeMenuSectionsParams = {
currentUserEmail: string | undefined;
currentUserAccountID: number | undefined;
Expand Down Expand Up @@ -4890,6 +4892,16 @@ function createTypeMenuSections(params: TypeMenuSectionsParams): SearchTypeMenuS
return typeMenuSections;
}

function omitInsightsPageMenuItems(sections: SearchTypeMenuSection[]): SearchTypeMenuSection[] {
return sections.flatMap((section) => {
const menuItems = section.menuItems.filter((item) => !insightsPageMenuKeys.has(item.key));
if (menuItems.length === section.menuItems.length) {
return section;
}
return menuItems.length > 0 ? {...section, menuItems} : [];
});
}

/**
* Icons used for each saved-search data type. Each asset already has the bookmark subscript baked in,
* so it can be rendered directly with the standard Expensicons component.
Expand Down Expand Up @@ -7295,6 +7307,7 @@ export {
getActions,
getPrimaryAction,
createTypeMenuSections,
omitInsightsPageMenuItems,
SPEND_INSIGHT_KEYS,
formatBadgeText,
getSectionBadgeText,
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/Search/SearchUIUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9345,6 +9345,52 @@ describe('SearchUIUtils', () => {
expect(allMenuItemKeys).not.toContain(CONST.SEARCH.SEARCH_KEYS.STATEMENTS);
});

it('should hide Violations by submitter from Spend menus once the Insights page replaces those entries', () => {
// Given a Control admin who still gets Violations by submitter from the Spend Insights section
const mockPolicies = {
policy1: {
id: policyID,
name: 'Control Workspace',
owner: adminEmail,
outputCurrency: 'USD',
role: CONST.POLICY.ROLE.ADMIN,
type: CONST.POLICY.TYPE.CORPORATE,
areRulesEnabled: true,
areCategoriesEnabled: true,
employeeList: {
'employee1@policy.com': {email: 'employee1@policy.com'},
'employee2@policy.com': {email: 'employee2@policy.com'},
},
},
};

const sections = SearchUIUtils.createTypeMenuSections({
currentUserEmail: adminEmail,
currentUserAccountID: adminAccountID,
cardFeedsByPolicy: {},
defaultCardFeed: undefined,
policies: mockPolicies,
savedSearches: {},
isOffline: false,
defaultExpensifyCard: undefined,
draftTransactionIDs: [],
isTrackIntentUser: false,
});

const menuKeysBeforeInsightsPage = sections.flatMap((section) => section.menuItems.map((item) => item.key));
expect(menuKeysBeforeInsightsPage).toContain(CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER);

// When Spend menus hide the entries the Insights page takes over
const menuSections = SearchUIUtils.omitInsightsPageMenuItems(sections);
const menuKeys = menuSections.flatMap((section) => section.menuItems.map((item) => item.key));

// Then Violations by submitter leaves the Spend menu, while the suggested search definition remains
expect(menuKeys).not.toContain(CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER);
expect(menuKeys).toContain(CONST.SEARCH.SEARCH_KEYS.EXPENSES);
expect(menuSections.find((section) => section.translationPath === 'search.tabs.insights')).toBeUndefined();
expect(SearchUIUtils.getSuggestedSearches(adminAccountID)[CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER].key).toBe(CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Neil's AI agent)

NAB: this only checks that the entry's key equals the constant it's defined with in getSuggestedSearches, so it doesn't test any behavior. The new useSearchKeyParam test already proves the search still resolves, so I think this line can be removed.

});

it('should not show Needs approval for a Submit workspace member who is not an approver', () => {
const mockPolicies = {
policy1: {
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/components/Search/hooks/useSearchKeyParam.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ describe('useSearchKeyParam', () => {
expect(result.current.currentSearchKey).toBe(CONST.SEARCH.SEARCH_KEYS.RECONCILIATION);
});

it('resolves an existing Violations by submitter query to its search key', () => {
// Given Violations by submitter is no longer a Spend menu entry, but the suggested search is still defined
const violationsQuery = getSuggestedSearches()[CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER].searchQuery;

// When that query is the current search
const {result} = renderSearchKeyParam(violationsQuery);

// Then it keeps the Violations by submitter key instead of falling back to Expenses
expect(result.current.currentSearchKey).toBe(CONST.SEARCH.SEARCH_KEYS.VIOLATIONS_BY_SUBMITTER);
});

it('matches a suggested search by its last (SEARCH_FILTERS) query', () => {
mockOnyx({[ONYXKEYS.SEARCH_FILTERS]: {[CONST.SEARCH.SEARCH_KEYS.SUBMIT]: mockSearchFilter(`type:${CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT} merchant:Zulu`)}});

Expand Down
Loading