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
23 changes: 9 additions & 14 deletions src/hooks/useAdvancedSearchFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,17 @@ function advancedSearchPoliciesSelector(policies: OnyxCollection<Policy>): OnyxC
return result;
}

/**
* Checks whether a single policy's tag lists contain at least one tag.
* Short-circuits on the first tag found.
*/
const policyTagListHasTags = (policyTagList: PolicyTagLists | undefined) => Object.values(policyTagList ?? {}).some((tagList) => Object.keys(tagList.tags ?? {}).length > 0);

/**
* Selector that checks if any tags exist across all policy tag lists.
* Returns a boolean with early exit on first tag found.
*/
const hasTagsSelector = (allPolicyTagLists: OnyxCollection<PolicyTagLists>) => {
for (const policyTagList of Object.values(allPolicyTagLists ?? {})) {
if (!policyTagList) {
continue;
}
for (const tagList of Object.values(policyTagList)) {
if (Object.keys(tagList.tags ?? {}).length > 0) {
return true;
}
}
}
return false;
};
const hasTagsSelector = (allPolicyTagLists: OnyxCollection<PolicyTagLists>) => Object.values(allPolicyTagLists ?? {}).some(policyTagListHasTags);

function useAdvancedSearchFiltersWorkspaces(policies: OnyxCollection<Policy>, searchTerm?: string) {
const {localeCompare} = useLocalize();
Expand Down Expand Up @@ -338,7 +332,8 @@ function useAdvancedSearchFilters(type: SearchDataTypes | undefined, policyID: s
});

const shouldDisplayCategoryFilter = shouldDisplayFilter(hasNonPersonalPolicyCategories ? 1 : 0, policyDerived?.areCategoriesEnabled ?? false, selectedPolicyCategories?.length > 0);
const shouldDisplayTagFilter = shouldDisplayFilter(hasTags ? 1 : 0, policyDerived?.areTagsEnabled ?? false, !!selectedPolicyTagLists);
const hasSelectedPolicyTags = selectedPolicyTagLists.some(policyTagListHasTags);
const shouldDisplayTagFilter = shouldDisplayFilter(hasTags ? 1 : 0, policyDerived?.areTagsEnabled ?? false, hasSelectedPolicyTags);
const shouldDisplayTaxFilter = shouldDisplayFilter(policyDerived?.hasAnyTaxRates ? 1 : 0, policyDerived?.areTaxEnabled ?? false);
const shouldDisplayWorkspaceFilter = workspaces.some((section) => section.data.length > 1);

Expand Down
39 changes: 37 additions & 2 deletions tests/unit/hooks/useAdvancedSearchFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('useAdvancedSearchFilters', () => {
});
});

it('shows tag filter when tags are enabled even if no tags exist (singlePolicyCondition is always truthy)', async () => {
it('hides tag filter when tags are enabled but no tags exist', async () => {
const policy = buildPolicy(1, {areTagsEnabled: true});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, policy);

Expand All @@ -188,7 +188,42 @@ describe('useAdvancedSearchFilters', () => {

const {result} = renderHook(() => useAdvancedSearchFilters(undefined, undefined), {wrapper});

// Tag filter is visible because singlePolicyCondition (!!selectedPolicyTagLists) is always true
// Tag filter is hidden because the tag list contains no actual tags
await waitFor(() => {
const allKeys = result.current.flat();
expect(allKeys).not.toContain(CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG);
});
});

it('hides tag filter when a selected policy has tags enabled but no tags exist', async () => {
const policy = buildPolicy(1, {areTagsEnabled: true});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, policy);

const emptyTagList: PolicyTagLists = {
Department: {
name: 'Department',
orderWeight: 0,
required: false,
tags: {},
},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_TAGS}1`, emptyTagList);

const {result} = renderHook(() => useAdvancedSearchFilters(undefined, ['1']), {wrapper});

await waitFor(() => {
const allKeys = result.current.flat();
expect(allKeys).not.toContain(CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG);
});
});

it('shows tag filter when a selected policy has actual tags', async () => {
const policy = buildPolicy(1, {areTagsEnabled: true});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}1`, policy);
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_TAGS}1`, buildTagList('Engineering'));

const {result} = renderHook(() => useAdvancedSearchFilters(undefined, ['1']), {wrapper});

await waitFor(() => {
const allKeys = result.current.flat();
expect(allKeys).toContain(CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG);
Expand Down
Loading