Skip to content

Commit

Permalink
Add filtering of reports based on active workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuuszzzzz committed Jan 11, 2024
1 parent 7c7f8fa commit 4a5eb63
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
4 changes: 0 additions & 4 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down Expand Up @@ -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[];
Expand Down
10 changes: 9 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -3437,11 +3438,18 @@ function shouldHideReport(report: OnyxEntry<Report>, 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<Report>, currentReportId: string, isInGSDMode: boolean, betas: Beta[], policies: OnyxCollection<Policy>, excludeEmptyChats = false) {
function shouldReportBeInOptionList(report: OnyxEntry<Report>, currentReportId: string, isInGSDMode: boolean, betas: Beta[], policies: OnyxCollection<Policy>, 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 ||
Expand Down
3 changes: 2 additions & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function getOrderedReportIDs(
policies: Record<string, Policy>,
priorityMode: ValueOf<typeof CONST.PRIORITY_MODE>,
allReportActions: OnyxCollection<ReportAction[]>,
activeWorkspaceID: string | undefined = undefined,
): string[] {
// Generate a unique cache key based on the function arguments
const cachedReportsKey = JSON.stringify(
Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/pages/WorkspaceSwitcherPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -48,25 +49,24 @@ 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();
const [selectedOption, setSelectedOption] = useState();
const [searchTerm, setSearchTerm] = useState('');
const {inputCallbackRef} = useAutoFocusInput();
const {translate} = useLocalize();
const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace();

const brickRoadsForPolicies = useMemo(() => getWorkspacesBrickRoads(), []);
const unreadStatusesForPolicies = useMemo(() => getWorkspacesUnreadStatuses(), []);
Expand Down Expand Up @@ -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);
}, []);

Expand Down Expand Up @@ -313,7 +316,4 @@ export default withOnyx({
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
activeWorkspaceID: {
key: ONYXKEYS.ACTIVE_WORKSPACE_ID,
},
})(WorkspaceSwitcherPage);
2 changes: 2 additions & 0 deletions src/pages/home/sidebar/SidebarLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -48,6 +49,7 @@ function SidebarLinks({onLinkClick, insets, optionListItems, isLoading, priority
const modal = useRef({});
const {translate, updateLocale} = useLocalize();
const {isSmallScreenWidth} = useWindowDimensions();
const {activeWorkspaceID} = useActiveWorkspace();

Check failure on line 52 in src/pages/home/sidebar/SidebarLinks.js

View workflow job for this annotation

GitHub Actions / lint

'activeWorkspaceID' is assigned a value but never used

useEffect(() => {
if (!isSmallScreenWidth) {
Expand Down
10 changes: 6 additions & 4 deletions src/pages/home/sidebar/SidebarLinksData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 4a5eb63

Please sign in to comment.