Skip to content

Workspace filter is displayed when account has no workspace #59943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
1 change: 0 additions & 1 deletion src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ function isCannedSearchQuery(queryJSON: SearchQueryJSON) {
/**
* A copy of `isDefaultExpensesQuery` handling the policy ID, used if you have access to the leftHandBar beta.
* When this beta is no longer needed, this method will be renamed to `isDefaultExpensesQuery` and will replace the old method.
*
*/
function isDefaultExpensesQueryWithPolicyIDCheck(queryJSON: SearchQueryJSON) {
return queryJSON.type === CONST.SEARCH.DATA_TYPES.EXPENSE && queryJSON.status === CONST.SEARCH.STATUS.EXPENSE.ALL && !queryJSON.filters && !queryJSON.groupBy && !queryJSON.policyID;
Expand Down
50 changes: 25 additions & 25 deletions src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import usePermissions from '@hooks/usePermissions';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import useWaitForNavigation from '@hooks/useWaitForNavigation';
import type {WorkspaceListItem} from '@hooks/useWorkspaceList';
import useWorkspaceList from '@hooks/useWorkspaceList';
import {clearAllFilters, saveSearch} from '@libs/actions/Search';
import {createCardFeedKey, getCardFeedKey, getCardFeedNamesWithType, getWorkspaceCardFeedKey} from '@libs/CardFeedUtils';
import {getCardDescription, mergeCardListWithWorkspaceFeeds} from '@libs/CardUtils';
Expand Down Expand Up @@ -160,7 +162,7 @@ const baseFilterConfig = {
*/
const typeFiltersKeys: Record<string, Array<Array<ValueOf<typeof CONST.SEARCH.SYNTAX_FILTER_KEYS>>>> = {
[CONST.SEARCH.DATA_TYPES.EXPENSE]: [
[CONST.SEARCH.SYNTAX_FILTER_KEYS.KEYWORD, CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, CONST.SEARCH.SYNTAX_FILTER_KEYS.TO],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the policy filter should be available for other search types too (expense reports, trips etc) as those are also associated with policies

Copy link
Contributor

Choose a reason for hiding this comment

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

Going to discuss with the team

[CONST.SEARCH.SYNTAX_FILTER_KEYS.KEYWORD, CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, CONST.SEARCH.SYNTAX_FILTER_KEYS.TO, CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID],
[
CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE,
CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT,
Expand Down Expand Up @@ -231,9 +233,8 @@ const typeFiltersKeys: Record<string, Array<Array<ValueOf<typeof CONST.SEARCH.SY
],
};

function getFilterWorkspaceDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, policies: OnyxCollection<Policy>) {
const workspaceFilter = filters[CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID];
return policies?.[`${ONYXKEYS.COLLECTION.POLICY}${workspaceFilter}`]?.name ?? workspaceFilter;
function getFilterWorkspaceDisplayTitle(filters: SearchAdvancedFiltersForm, policies: WorkspaceListItem[]) {
return policies.filter((value) => value.policyID === filters.policyID).at(0)?.text;
}

function getFilterCardDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, cards: CardList, translate: LocaleContextProps['translate']) {
Expand Down Expand Up @@ -433,24 +434,6 @@ function AdvancedSearchFilters() {
const personalDetails = usePersonalDetails();
const {canUseLeftHandBar} = usePermissions();

// If users have access to the leftHandBar beta, then the workspace filter is displyed in the first section of the advanced search filters
const typeFiltersKeysWithOptionalPolicy = useMemo(
() =>
canUseLeftHandBar
? Object.fromEntries(
Object.entries(typeFiltersKeys).map(([key, arrays]) => {
const firstFiltersSection = arrays.at(0);
if (!firstFiltersSection) {
return [key, arrays];
}

const modifiedFirstFiltersSection = [...firstFiltersSection, CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID];
return [key, [modifiedFirstFiltersSection, ...arrays.slice(1)]];
}),
)
: typeFiltersKeys,
[canUseLeftHandBar],
);
const [policies = {}] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
const [allPolicyCategories = {}] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES, {
selector: (policyCategories) =>
Expand All @@ -469,6 +452,16 @@ function AdvancedSearchFilters() {
.map(getTagNamesFromTagsLists)
.flat();

const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email});

const {sections: workspaces} = useWorkspaceList({
policies,
currentUserLogin,
shouldShowPendingDeletePolicy: false,
selectedPolicyID: undefined,
searchTerm: '',
});

// When looking if a user has any categories to display, we want to ignore the policies that are of type PERSONAL
const nonPersonalPolicyCategoryIds = Object.values(policies)
.filter((policy): policy is NonNullable<Policy> => !!(policy && policy.type !== CONST.POLICY.TYPE.PERSONAL))
Expand All @@ -486,9 +479,12 @@ function AdvancedSearchFilters() {
const shouldDisplayTagFilter = shouldDisplayFilter(tagListsUnpacked.length, areTagsEnabled, !!singlePolicyTagLists);
const shouldDisplayCardFilter = shouldDisplayFilter(Object.keys(allCards).length, areCardsEnabled);
const shouldDisplayTaxFilter = shouldDisplayFilter(Object.keys(taxRates).length, areTaxEnabled);
const shouldDisplayWorkspaceFilter = useMemo(() => {
return canUseLeftHandBar && workspaces.some((section) => section.data.length !== 0);
}, [canUseLeftHandBar, workspaces]);

let currentType = searchAdvancedFilters?.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE;
if (!Object.keys(typeFiltersKeysWithOptionalPolicy).includes(currentType)) {
if (!Object.keys(typeFiltersKeys).includes(currentType)) {
currentType = CONST.SEARCH.DATA_TYPES.EXPENSE;
}

Expand Down Expand Up @@ -520,7 +516,7 @@ function AdvancedSearchFilters() {
applyFiltersAndNavigate();
};

const filters = typeFiltersKeysWithOptionalPolicy[currentType]
const filters = typeFiltersKeys[currentType]
.map((section) => {
return section
.map((key) => {
Expand Down Expand Up @@ -578,7 +574,11 @@ function AdvancedSearchFilters() {
} else if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.REIMBURSABLE || key === CONST.SEARCH.SYNTAX_FILTER_KEYS.BILLABLE) {
filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, key, translate);
} else if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID) {
filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, policies);
if (!shouldDisplayWorkspaceFilter) {
return;
}
const workspacesData = workspaces.flatMap((value) => value.data);
filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, workspacesData);
}
return {
key,
Expand Down
Loading