From 1e9a699bd56d4415a84977cd6a34d640f359e322 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Wed, 9 Apr 2025 18:19:24 +0200 Subject: [PATCH 1/6] fix no showing workspace filter if there is no workspace --- src/pages/Search/AdvancedSearchFilters.tsx | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index fe12ab4d9fdd..5e835f24a381 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -11,11 +11,13 @@ import {usePersonalDetails} from '@components/OnyxProvider'; import ScrollView from '@components/ScrollView'; import type {SearchDateFilterKeys, SearchFilterKey} from '@components/Search/types'; import SpacerView from '@components/SpacerView'; +import useDebouncedState from '@hooks/useDebouncedState'; import useLocalize from '@hooks/useLocalize'; import usePermissions from '@hooks/usePermissions'; import useSingleExecution from '@hooks/useSingleExecution'; import useThemeStyles from '@hooks/useThemeStyles'; import useWaitForNavigation from '@hooks/useWaitForNavigation'; +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'; @@ -451,6 +453,18 @@ function AdvancedSearchFilters() { .map(getTagNamesFromTagsLists) .flat(); + const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); + const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email}); + const [debouncedSearchTerm] = useDebouncedState(''); + + const {sections: workspaces} = useWorkspaceList({ + policies, + currentUserLogin, + shouldShowPendingDeletePolicy: false, + selectedPolicyID: searchAdvancedFiltersForm?.policyID, + searchTerm: debouncedSearchTerm, + }); + // 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.type !== CONST.POLICY.TYPE.PERSONAL)) @@ -468,6 +482,10 @@ 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 !(workspaces.length === 0 || workspaces.every((section) => section.data.length === 0)); + }, [workspaces]); + debugger; let currentType = searchAdvancedFilters?.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE; if (!Object.keys(typeFiltersKeysWithOptionalPolicy).includes(currentType)) { @@ -558,6 +576,9 @@ function AdvancedSearchFilters() { } else if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) { filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, translate, reports); } else if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID) { + if (!shouldDisplayWorkspaceFilter) { + return; + } filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, policies); } return { From 1c7983e2b016c558d189da7b673d6ce76d04d445 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Wed, 9 Apr 2025 18:39:22 +0200 Subject: [PATCH 2/6] fix case of removed workspace --- src/pages/Search/AdvancedSearchFilters.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index 5e835f24a381..1d753825a83f 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -465,6 +465,8 @@ function AdvancedSearchFilters() { searchTerm: debouncedSearchTerm, }); + const isWorkspaceNameStillPresent = (filterTitle: string | undefined) => workspaces.some((section) => section.data?.some((item) => item.text === filterTitle)); + // 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.type !== CONST.POLICY.TYPE.PERSONAL)) @@ -485,7 +487,6 @@ function AdvancedSearchFilters() { const shouldDisplayWorkspaceFilter = useMemo(() => { return !(workspaces.length === 0 || workspaces.every((section) => section.data.length === 0)); }, [workspaces]); - debugger; let currentType = searchAdvancedFilters?.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE; if (!Object.keys(typeFiltersKeysWithOptionalPolicy).includes(currentType)) { @@ -579,7 +580,10 @@ function AdvancedSearchFilters() { if (!shouldDisplayWorkspaceFilter) { return; } - filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, policies); + const selectedWorkspaceName = baseFilterConfig[key].getTitle(searchAdvancedFilters, policies); + if (isWorkspaceNameStillPresent(selectedWorkspaceName)) { + filterTitle = selectedWorkspaceName; + } } return { key, From a00c72284a5e76f35319c558faea92c723a1d200 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Thu, 10 Apr 2025 12:04:05 +0200 Subject: [PATCH 3/6] cleanup code --- src/libs/SearchQueryUtils.ts | 1 - src/pages/Search/AdvancedSearchFilters.tsx | 37 ++++++---------------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/libs/SearchQueryUtils.ts b/src/libs/SearchQueryUtils.ts index 9ca5176bc877..8ced91b22ea7 100644 --- a/src/libs/SearchQueryUtils.ts +++ b/src/libs/SearchQueryUtils.ts @@ -777,7 +777,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; diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index 1d753825a83f..d025cad44735 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -152,7 +152,7 @@ const baseFilterConfig = { */ const typeFiltersKeys: Record>>> = { [CONST.SEARCH.DATA_TYPES.EXPENSE]: [ - [CONST.SEARCH.SYNTAX_FILTER_KEYS.KEYWORD, CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, CONST.SEARCH.SYNTAX_FILTER_KEYS.TO], + [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, @@ -417,24 +417,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) => @@ -453,18 +435,17 @@ function AdvancedSearchFilters() { .map(getTagNamesFromTagsLists) .flat(); - const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email}); - const [debouncedSearchTerm] = useDebouncedState(''); const {sections: workspaces} = useWorkspaceList({ policies, currentUserLogin, shouldShowPendingDeletePolicy: false, - selectedPolicyID: searchAdvancedFiltersForm?.policyID, - searchTerm: debouncedSearchTerm, + selectedPolicyID: undefined, + searchTerm: '', }); - + // We check if the previously selected workspace (which might have been deleted) + // still exists — only then can we set it as the active one again. const isWorkspaceNameStillPresent = (filterTitle: string | undefined) => workspaces.some((section) => section.data?.some((item) => item.text === filterTitle)); // When looking if a user has any categories to display, we want to ignore the policies that are of type PERSONAL @@ -485,11 +466,11 @@ function AdvancedSearchFilters() { const shouldDisplayCardFilter = shouldDisplayFilter(Object.keys(allCards).length, areCardsEnabled); const shouldDisplayTaxFilter = shouldDisplayFilter(Object.keys(taxRates).length, areTaxEnabled); const shouldDisplayWorkspaceFilter = useMemo(() => { - return !(workspaces.length === 0 || workspaces.every((section) => section.data.length === 0)); - }, [workspaces]); + return !(workspaces.length === 0 || (workspaces.every((section) => section.data.length === 0) && canUseLeftHandBar)); + }, [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; } @@ -521,7 +502,7 @@ function AdvancedSearchFilters() { applyFiltersAndNavigate(); }; - const filters = typeFiltersKeysWithOptionalPolicy[currentType] + const filters = typeFiltersKeys[currentType] .map((section) => { return section .map((key) => { From 0d421e2a1d2003cd4715ba3665a7aaf64e06b624 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Thu, 10 Apr 2025 12:14:05 +0200 Subject: [PATCH 4/6] cleanup code --- src/pages/Search/AdvancedSearchFilters.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index d025cad44735..49e8f356d18e 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -11,7 +11,6 @@ import {usePersonalDetails} from '@components/OnyxProvider'; import ScrollView from '@components/ScrollView'; import type {SearchDateFilterKeys, SearchFilterKey} from '@components/Search/types'; import SpacerView from '@components/SpacerView'; -import useDebouncedState from '@hooks/useDebouncedState'; import useLocalize from '@hooks/useLocalize'; import usePermissions from '@hooks/usePermissions'; import useSingleExecution from '@hooks/useSingleExecution'; From 67b6794c513a5200550cdab804d7d7dd04b7ae28 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Mon, 14 Apr 2025 12:08:15 +0200 Subject: [PATCH 5/6] change workspace filter tittle getter function --- src/pages/Search/AdvancedSearchFilters.tsx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index 49e8f356d18e..5b1feb5f6c6c 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -16,6 +16,7 @@ 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'; @@ -220,9 +221,8 @@ const typeFiltersKeys: Record, policies: OnyxCollection) { - 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, cards: CardList, translate: LocaleContextProps['translate']) { @@ -443,9 +443,6 @@ function AdvancedSearchFilters() { selectedPolicyID: undefined, searchTerm: '', }); - // We check if the previously selected workspace (which might have been deleted) - // still exists — only then can we set it as the active one again. - const isWorkspaceNameStillPresent = (filterTitle: string | undefined) => workspaces.some((section) => section.data?.some((item) => item.text === filterTitle)); // 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) @@ -560,10 +557,8 @@ function AdvancedSearchFilters() { if (!shouldDisplayWorkspaceFilter) { return; } - const selectedWorkspaceName = baseFilterConfig[key].getTitle(searchAdvancedFilters, policies); - if (isWorkspaceNameStillPresent(selectedWorkspaceName)) { - filterTitle = selectedWorkspaceName; - } + const workspacesData = workspaces.flatMap((value) => value.data); + filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, workspacesData); } return { key, From 5c3d774e5ab7d6b8e4f308c1ca4865351fbac4a1 Mon Sep 17 00:00:00 2001 From: sumo_slonik Date: Tue, 15 Apr 2025 13:57:50 +0200 Subject: [PATCH 6/6] new condition in shouldDisplayWorkspaceFilter --- src/pages/Search/AdvancedSearchFilters.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Search/AdvancedSearchFilters.tsx b/src/pages/Search/AdvancedSearchFilters.tsx index 5b1feb5f6c6c..a0828bf39d95 100644 --- a/src/pages/Search/AdvancedSearchFilters.tsx +++ b/src/pages/Search/AdvancedSearchFilters.tsx @@ -462,7 +462,7 @@ function AdvancedSearchFilters() { const shouldDisplayCardFilter = shouldDisplayFilter(Object.keys(allCards).length, areCardsEnabled); const shouldDisplayTaxFilter = shouldDisplayFilter(Object.keys(taxRates).length, areTaxEnabled); const shouldDisplayWorkspaceFilter = useMemo(() => { - return !(workspaces.length === 0 || (workspaces.every((section) => section.data.length === 0) && canUseLeftHandBar)); + return canUseLeftHandBar && workspaces.some((section) => section.data.length !== 0); }, [canUseLeftHandBar, workspaces]); let currentType = searchAdvancedFilters?.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE;