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

[NoQA] Add BrickRoadsUtils #33239

Merged
merged 10 commits into from
Jan 10, 2024
62 changes: 62 additions & 0 deletions src/libs/BrickRoadsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Onyx, {OnyxCollection} from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {Report} from '@src/types/onyx';
import * as OptionsListUtils from './OptionsListUtils';
import * as ReportActionsUtils from './ReportActionsUtils';
import * as ReportUtils from './ReportUtils';

let allReports: OnyxCollection<Report>;

type BrickRoad = 'GBR' | 'RBR' | undefined;

Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => (allReports = value),
});

const getBrickRoadForPolicy = (policyReport: Report): BrickRoad => {
mountiny marked this conversation as resolved.
Show resolved Hide resolved
const policyReportAction = ReportActionsUtils.getAllReportActions(policyReport.reportID);
mountiny marked this conversation as resolved.
Show resolved Hide resolved
const reportErrors = OptionsListUtils.getAllReportErrors(policyReport, policyReportAction);
const redBrickRoadIndicator = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

should we name this doesReportContainErrors?

if (redBrickRoadIndicator) {
return 'RBR';
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a constant for RBR?

}
let itemParentReportAction = {};
mountiny marked this conversation as resolved.
Show resolved Hide resolved
if (policyReport.parentReportID) {
const itemParentReportActions = ReportActionsUtils.getAllReportActions(policyReport.parentReportID);
itemParentReportAction = policyReport.parentReportActionID ? itemParentReportActions[policyReport.parentReportActionID] : {};
}
const optionFromPolicyReport = {...policyReport, isUnread: ReportUtils.isUnread(policyReport), isUnreadWithMention: ReportUtils.isUnreadWithMention(policyReport)};
mountiny marked this conversation as resolved.
Show resolved Hide resolved
const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(optionFromPolicyReport, itemParentReportAction);
return shouldShowGreenDotIndicator ? 'GBR' : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a constant for GBR?

};

function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
if (!allReports) {
return {};
}

const brickRoadsMap: Record<string, BrickRoad> = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment here that the key of this object should be a report ID?


Object.keys(allReports).forEach((report) => {
const policyID = allReports?.[report]?.policyID;
const policyReport = allReports ? allReports[report] : null;
if (!policyID || !policyReport || brickRoadsMap[policyID] === 'RBR') {
return;
}
const policyBrickRoad = getBrickRoadForPolicy(policyReport);
mountiny marked this conversation as resolved.
Show resolved Hide resolved

if (!policyBrickRoad && !!brickRoadsMap[policyID]) {
return;
}

brickRoadsMap[policyID] = policyBrickRoad;
});

return brickRoadsMap;
}

export {getBrickRoadForPolicy, getWorkspacesBrickRoads};
export type {BrickRoad};
Loading