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
4 changes: 4 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3034,6 +3034,10 @@ const CONST = {
DEFAULT: 5,
CAROUSEL: 3,
},
BRICK_ROAD: {
GBR: 'GBR',
RBR: 'RBR',
},
} as const;

export default CONST;
71 changes: 71 additions & 0 deletions src/libs/BrickRoadsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Onyx, {OnyxCollection} from 'react-native-onyx';
import {ValueOf} from 'type-fest';
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 = ValueOf<typeof CONST.BRICK_ROAD> | undefined;

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

/**
* @param policyReport
* @returns BrickRoad for the policy passed as a param
*/
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 doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined;
if (doesReportContainErrors) {
return CONST.BRICK_ROAD.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 ? CONST.BRICK_ROAD.GBR : undefined;
};

/**
* @returns a map where the keys are policyIDs and the values are BrickRoads for each policy
*/
function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
if (!allReports) {
return {};
}

// The key in this map is the workspace id
const workspacesBrickRoadsMap: Record<string, BrickRoad> = {};

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

if (!policyBrickRoad && !!workspacesBrickRoadsMap[policyID]) {
mountiny marked this conversation as resolved.
Show resolved Hide resolved
return;
}

workspacesBrickRoadsMap[policyID] = policyBrickRoad;
mountiny marked this conversation as resolved.
Show resolved Hide resolved
});

return workspacesBrickRoadsMap;
}

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