Skip to content
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

Workspace switcher: Expensify is no longer bolded and missing RBR after refreshing the page #40197

Merged
merged 8 commits into from
Apr 18, 2024
26 changes: 14 additions & 12 deletions src/libs/WorkspacesSettingsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, ReimbursementAccount, Report, ReportActions} from '@src/types/onyx';
import type {Unit} from '@src/types/onyx/Policy';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as CurrencyUtils from './CurrencyUtils';
import type {Phrase, PhraseParameters} from './Localize';
import * as OptionsListUtils from './OptionsListUtils';
Expand Down Expand Up @@ -54,11 +55,12 @@ Onyx.connect({
});

/**
* @param report
* @returns BrickRoad for the policy passed as a param
* @param altReportActions Replaces (local) allReportActions used within (local) function getWorkspacesBrickRoads
* @returns BrickRoad for the policy passed as a param and optionally actionsByReport (if passed)
*/
const getBrickRoadForPolicy = (report: Report): BrickRoad => {
const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {};
const getBrickRoadForPolicy = (report: Report, altReportActions?: OnyxCollection<ReportActions>): BrickRoad => {
const finalReportActions = isEmptyObject(altReportActions) ? allReportActions : altReportActions;
ikevin127 marked this conversation as resolved.
Show resolved Hide resolved
const reportActions = finalReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {};
const reportErrors = OptionsListUtils.getAllReportErrors(report, reportActions);
const doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined;
if (doesReportContainErrors) {
Expand Down Expand Up @@ -144,14 +146,14 @@ function checkIfWorkspaceSettingsTabHasRBR(policyID?: string) {
/**
* @returns a map where the keys are policyIDs and the values are BrickRoads for each policy
*/
function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
if (!allReports) {
function getWorkspacesBrickRoads(reports: OnyxCollection<Report>, policies: OnyxCollection<Policy>, reportActions: OnyxCollection<ReportActions>): Record<string, BrickRoad> {
if (!reports) {
return {};
}

// The key in this map is the workspace id
const workspacesBrickRoadsMap: Record<string, BrickRoad> = {};
Object.values(allPolicies ?? {}).forEach((policy) => {
Object.values(policies ?? {}).forEach((policy) => {
// Only policies which user has access to on the list should be checked. Policies that don't have an ID and contain only information about the errors aren't displayed anywhere.
if (!policy?.id) {
return;
Expand All @@ -162,12 +164,12 @@ function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
}
});

Object.values(allReports).forEach((report) => {
Object.values(reports).forEach((report) => {
const policyID = report?.policyID ?? CONST.POLICY.EMPTY;
if (!report || workspacesBrickRoadsMap[policyID] === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR) {
return;
}
const workspaceBrickRoad = getBrickRoadForPolicy(report);
const workspaceBrickRoad = getBrickRoadForPolicy(report, reportActions);

if (!workspaceBrickRoad && !!workspacesBrickRoadsMap[policyID]) {
return;
Expand All @@ -182,14 +184,14 @@ function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
/**
* @returns a map where the keys are policyIDs and the values are truthy booleans if policy has unread content
*/
function getWorkspacesUnreadStatuses(): Record<string, boolean> {
if (!allReports) {
function getWorkspacesUnreadStatuses(reports: OnyxCollection<Report>): Record<string, boolean> {
ikevin127 marked this conversation as resolved.
Show resolved Hide resolved
if (!reports) {
return {};
}

const workspacesUnreadStatuses: Record<string, boolean> = {};

Object.values(allReports).forEach((report) => {
Object.values(reports).forEach((report) => {
const policyID = report?.policyID;
if (!policyID || workspacesUnreadStatuses[policyID]) {
return;
Expand Down
29 changes: 10 additions & 19 deletions src/pages/WorkspaceSwitcherPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, {useCallback, useMemo} from 'react';
import type {OnyxCollection} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import ScreenWrapper from '@components/ScreenWrapper';
Expand All @@ -19,7 +18,6 @@ import type {BrickRoad} from '@libs/WorkspacesSettingsUtils';
import WorkspaceCardCreateAWorkspace from '@pages/workspace/card/WorkspaceCardCreateAWorkspace';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import WorkspacesSectionHeader from './WorkspacesSectionHeader';

Expand All @@ -40,23 +38,20 @@ const sortWorkspacesBySelected = (workspace1: WorkspaceListItem, workspace2: Wor
return workspace1.text?.toLowerCase().localeCompare(workspace2.text?.toLowerCase() ?? '') ?? 0;
};

type WorkspaceSwitcherPageOnyxProps = {
/** The list of this user's policies */
policies: OnyxCollection<Policy>;
};

type WorkspaceSwitcherPageProps = WorkspaceSwitcherPageOnyxProps;

const WorkspaceCardCreateAWorkspaceInstance = <WorkspaceCardCreateAWorkspace />;

function WorkspaceSwitcherPage({policies}: WorkspaceSwitcherPageProps) {
function WorkspaceSwitcherPage() {
const {isOffline} = useNetwork();
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
const {translate} = useLocalize();
const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace();

const brickRoadsForPolicies = useMemo(() => getWorkspacesBrickRoads(), []);
const unreadStatusesForPolicies = useMemo(() => getWorkspacesUnreadStatuses(), []);
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS);
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);

const brickRoadsForPolicies = useMemo(() => getWorkspacesBrickRoads(reports, policies, reportActions), [reports, policies, reportActions]);
const unreadStatusesForPolicies = useMemo(() => getWorkspacesUnreadStatuses(reports), [reports]);

const getIndicatorTypeForPolicy = useCallback(
(policyId?: string) => {
Expand Down Expand Up @@ -125,7 +120,7 @@ function WorkspaceSwitcherPage({policies}: WorkspaceSwitcherPageProps) {
type: CONST.ICON_TYPE_WORKSPACE,
},
],
boldStyle: hasUnreadData(policy?.id),
isBold: hasUnreadData(policy?.id),
keyForList: policy?.id,
isPolicyAdmin: PolicyUtils.isPolicyAdmin(policy),
isSelected: activeWorkspaceID === policy?.id,
Expand Down Expand Up @@ -201,8 +196,4 @@ function WorkspaceSwitcherPage({policies}: WorkspaceSwitcherPageProps) {

WorkspaceSwitcherPage.displayName = 'WorkspaceSwitcherPage';

export default withOnyx<WorkspaceSwitcherPageProps, WorkspaceSwitcherPageOnyxProps>({
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
})(WorkspaceSwitcherPage);
export default WorkspaceSwitcherPage;
Loading