diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 5dc9cd19cf70..89ddbdc06883 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -21,9 +21,6 @@ const ONYXKEYS = { * which tab is the leader, and which ones are the followers */ ACTIVE_CLIENTS: 'activeClients', - /** Holds active policyID (if any is active) */ - ACTIVE_WORKSPACE_ID: 'activeWorkspaceID', - /** A unique ID for the device */ DEVICE_ID: 'deviceID', @@ -366,7 +363,6 @@ type OnyxValues = { [ONYXKEYS.ACCOUNT_MANAGER_REPORT_ID]: string; [ONYXKEYS.NVP_IS_FIRST_TIME_NEW_EXPENSIFY_USER]: boolean; [ONYXKEYS.ACTIVE_CLIENTS]: string[]; - [ONYXKEYS.ACTIVE_WORKSPACE_ID]: string | undefined; [ONYXKEYS.DEVICE_ID]: string; [ONYXKEYS.IS_SIDEBAR_LOADED]: boolean; [ONYXKEYS.PERSISTED_REQUESTS]: OnyxTypes.Request[]; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 19bac9c2971d..a91a11362474 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -25,6 +25,7 @@ import type DeepValueOf from '@src/types/utils/DeepValueOf'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; import {isEmptyObject, isNotEmptyObject} from '@src/types/utils/EmptyObject'; import type IconAsset from '@src/types/utils/IconAsset'; +import Log from "./Log" import * as CurrencyUtils from './CurrencyUtils'; import DateUtils from './DateUtils'; import isReportMessageAttachment from './isReportMessageAttachment'; @@ -3437,11 +3438,18 @@ function shouldHideReport(report: OnyxEntry, currentReportId: string): b * This logic is very specific and the order of the logic is very important. It should fail quickly in most cases and also * filter out the majority of reports before filtering out very specific minority of reports. */ -function shouldReportBeInOptionList(report: OnyxEntry, currentReportId: string, isInGSDMode: boolean, betas: Beta[], policies: OnyxCollection, excludeEmptyChats = false) { +function shouldReportBeInOptionList(report: OnyxEntry, currentReportId: string, isInGSDMode: boolean, betas: Beta[], policies: OnyxCollection, excludeEmptyChats = false, activeWorkspaceID: string | undefined = undefined) { const isInDefaultMode = !isInGSDMode; // Exclude reports that have no data because there wouldn't be anything to show in the option item. // This can happen if data is currently loading from the server or a report is in various stages of being created. // This can also happen for anyone accessing a public room or archived room for which they don't have access to the underlying policy. + // Optionally exclude reports that do not belong to currently active workspace + + Log.info(`active workspace id ${activeWorkspaceID}`) + if (activeWorkspaceID && report?.policyID !== activeWorkspaceID && !isConciergeChatReport(report)) { + return false; + } + if ( !report?.reportID || !report?.type || diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 6e46ec320066..b47c66e1a4d8 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -120,6 +120,7 @@ function getOrderedReportIDs( policies: Record, priorityMode: ValueOf, allReportActions: OnyxCollection, + activeWorkspaceID: string | undefined = undefined, ): string[] { // Generate a unique cache key based on the function arguments const cachedReportsKey = JSON.stringify( @@ -151,7 +152,7 @@ function getOrderedReportIDs( const isInDefaultMode = !isInGSDMode; const allReportsDictValues = Object.values(allReports); // Filter out all the reports that shouldn't be displayed - const reportsToDisplay = allReportsDictValues.filter((report) => ReportUtils.shouldReportBeInOptionList(report, currentReportId ?? '', isInGSDMode, betas, policies, true)); + const reportsToDisplay = allReportsDictValues.filter((report) => ReportUtils.shouldReportBeInOptionList(report, currentReportId ?? '', isInGSDMode, betas, policies, true, activeWorkspaceID)); if (reportsToDisplay.length === 0) { // Display Concierge chat report when there is no report to be displayed diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index ab4bd2c1947b..3ff0b99b59fb 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -1523,14 +1523,6 @@ function buildOptimisticPolicyRecentlyUsedTags(policyID, tag) { }; } -/** - * This function saves the selected workspace to onyx to filter data depending on this ID - * @param {String | undefined} policyID - */ -function selectWorkspace(policyID) { - Onyx.set(ONYXKEYS.ACTIVE_WORKSPACE_ID, policyID); -} - /** * This flow is used for bottom up flow converting IOU report to an expense report. When user takes this action, * we create a Collect type workspace when the person taking the action becomes an owner and an admin, while we diff --git a/src/pages/WorkspaceSwitcherPage.js b/src/pages/WorkspaceSwitcherPage.js index 2b37f4c10559..f5405ad08152 100644 --- a/src/pages/WorkspaceSwitcherPage.js +++ b/src/pages/WorkspaceSwitcherPage.js @@ -15,6 +15,7 @@ import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useTheme from '@hooks/useTheme'; +import useActiveWorkspace from '@hooks/useActiveWorkspace'; import useThemeStyles from '@hooks/useThemeStyles'; import Navigation from '@libs/Navigation/Navigation'; import * as PolicyUtils from '@libs/PolicyUtils'; @@ -48,18 +49,16 @@ const propTypes = { pendingAction: PropTypes.oneOf(_.values(CONST.RED_BRICK_ROAD_PENDING_ACTION)), }), ), - activeWorkspaceID: PropTypes.string, }; const defaultProps = { policies: {}, - activeWorkspaceID: undefined, }; const MINIMUM_WORKSPACES_TO_SHOW_SEARCH = 8; const EXPENSIFY_TITLE = 'Expensify'; -function WorkspaceSwitcherPage({policies, activeWorkspaceID}) { +function WorkspaceSwitcherPage({policies}) { const theme = useTheme(); const styles = useThemeStyles(); const {isOffline} = useNetwork(); @@ -67,6 +66,7 @@ function WorkspaceSwitcherPage({policies, activeWorkspaceID}) { const [searchTerm, setSearchTerm] = useState(''); const {inputCallbackRef} = useAutoFocusInput(); const {translate} = useLocalize(); + const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace(); const brickRoadsForPolicies = useMemo(() => getWorkspacesBrickRoads(), []); const unreadStatusesForPolicies = useMemo(() => getWorkspacesUnreadStatuses(), []); @@ -112,6 +112,9 @@ function WorkspaceSwitcherPage({policies, activeWorkspaceID}) { } else { setSelectedOption(undefined); } + // Temporary: This will be handled in custom navigation function that also puts policyID in BottomTabNavigator state + setActiveWorkspaceID(policyID); + Navigation.goBack(); Navigation.navigate(ROUTES.HOME); }, []); @@ -313,7 +316,4 @@ export default withOnyx({ policies: { key: ONYXKEYS.COLLECTION.POLICY, }, - activeWorkspaceID: { - key: ONYXKEYS.ACTIVE_WORKSPACE_ID, - }, })(WorkspaceSwitcherPage); diff --git a/src/pages/home/sidebar/SidebarLinks.js b/src/pages/home/sidebar/SidebarLinks.js index 17abb3bdb43b..127c8b72f336 100644 --- a/src/pages/home/sidebar/SidebarLinks.js +++ b/src/pages/home/sidebar/SidebarLinks.js @@ -20,6 +20,7 @@ import * as App from '@userActions/App'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; +import useActiveWorkspace from '@hooks/useActiveWorkspace'; const basePropTypes = { /** Toggles the navigation menu open and closed */ @@ -48,6 +49,7 @@ function SidebarLinks({onLinkClick, insets, optionListItems, isLoading, priority const modal = useRef({}); const {translate, updateLocale} = useLocalize(); const {isSmallScreenWidth} = useWindowDimensions(); + const {activeWorkspaceID} = useActiveWorkspace(); useEffect(() => { if (!isSmallScreenWidth) { diff --git a/src/pages/home/sidebar/SidebarLinksData.js b/src/pages/home/sidebar/SidebarLinksData.js index 580cc7909fd1..195a0eeccb04 100644 --- a/src/pages/home/sidebar/SidebarLinksData.js +++ b/src/pages/home/sidebar/SidebarLinksData.js @@ -16,6 +16,7 @@ import SidebarUtils from '@libs/SidebarUtils'; import reportPropTypes from '@pages/reportPropTypes'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import useActiveWorkspace from '@hooks/useActiveWorkspace'; import SidebarLinks, {basePropTypes} from './SidebarLinks'; const propTypes = { @@ -68,12 +69,13 @@ const defaultProps = { function SidebarLinksData({isFocused, allReportActions, betas, chatReports, currentReportID, insets, isLoadingApp, onLinkClick, policies, priorityMode, network}) { const styles = useThemeStyles(); + const {activeWorkspaceID} = useActiveWorkspace(); const {translate} = useLocalize(); const reportIDsRef = useRef(null); const isLoading = isLoadingApp; const optionListItems = useMemo(() => { - const reportIDs = SidebarUtils.getOrderedReportIDs(null, chatReports, betas, policies, priorityMode, allReportActions); + const reportIDs = SidebarUtils.getOrderedReportIDs(null, chatReports, betas, policies, priorityMode, allReportActions, activeWorkspaceID); if (deepEqual(reportIDsRef.current, reportIDs)) { return reportIDsRef.current; @@ -85,7 +87,7 @@ function SidebarLinksData({isFocused, allReportActions, betas, chatReports, curr reportIDsRef.current = reportIDs; } return reportIDsRef.current || []; - }, [allReportActions, betas, chatReports, policies, priorityMode, isLoading, network.isOffline]); + }, [chatReports, betas, policies, priorityMode, allReportActions, activeWorkspaceID, isLoading, network.isOffline]); // We need to make sure the current report is in the list of reports, but we do not want // to have to re-generate the list every time the currentReportID changes. To do that @@ -94,10 +96,10 @@ function SidebarLinksData({isFocused, allReportActions, betas, chatReports, curr // case we re-generate the list a 2nd time with the current report included. const optionListItemsWithCurrentReport = useMemo(() => { if (currentReportID && !_.contains(optionListItems, currentReportID)) { - return SidebarUtils.getOrderedReportIDs(currentReportID, chatReports, betas, policies, priorityMode, allReportActions); + return SidebarUtils.getOrderedReportIDs(currentReportID, chatReports, betas, policies, priorityMode, allReportActions, activeWorkspaceID); } return optionListItems; - }, [currentReportID, optionListItems, chatReports, betas, policies, priorityMode, allReportActions]); + }, [currentReportID, optionListItems, chatReports, betas, policies, priorityMode, allReportActions, activeWorkspaceID]); const currentReportIDRef = useRef(currentReportID); currentReportIDRef.current = currentReportID;