Skip to content

Commit 5b9e374

Browse files
authored
Merge pull request Expensify#68181 from shubham1206agra/refactor-onyx-47
Removed Onyx.connect in WorkspaceSettingsUtils (part 2)
2 parents 4895fe4 + d7d9a01 commit 5b9e374

8 files changed

Lines changed: 212 additions & 128 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand",
4747
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
4848
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
49-
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=217 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=216 --cache --cache-location=node_modules/.cache/eslint",
5050
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
5151
"lint-watch": "npx eslint-watch --watch --changed",
5252
"shellcheck": "./scripts/shellCheck.sh",

src/components/Navigation/DebugTabView.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
1717
import Navigation from '@libs/Navigation/Navigation';
1818
import {getRouteForCurrentStep as getReimbursementAccountRouteForCurrentStep} from '@libs/ReimbursementAccountUtils';
1919
import type {BrickRoad} from '@libs/WorkspacesSettingsUtils';
20-
import {getChatTabBrickRoadReport} from '@libs/WorkspacesSettingsUtils';
20+
import {getChatTabBrickRoadReportID} from '@libs/WorkspacesSettingsUtils';
2121
import CONST from '@src/CONST';
2222
import type {TranslationPaths} from '@src/languages/types';
2323
import ONYXKEYS from '@src/ONYXKEYS';
@@ -104,8 +104,9 @@ function DebugTabView({selectedTab, chatTabBrickRoad}: DebugTabViewProps) {
104104
const styles = useThemeStyles();
105105
const {translate} = useLocalize();
106106
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true});
107+
const [reportAttributes] = useOnyx(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, {selector: (value) => value?.reports, canBeMissing: true});
107108
const {status, indicatorColor, policyIDWithErrors} = useIndicatorStatus();
108-
const {orderedReports} = useSidebarOrderedReports();
109+
const {orderedReportIDs} = useSidebarOrderedReports();
109110

110111
const message = useMemo((): TranslationPaths | undefined => {
111112
if (selectedTab === NAVIGATION_TABS.HOME) {
@@ -139,10 +140,10 @@ function DebugTabView({selectedTab, chatTabBrickRoad}: DebugTabViewProps) {
139140

140141
const navigateTo = useCallback(() => {
141142
if (selectedTab === NAVIGATION_TABS.HOME && !!chatTabBrickRoad) {
142-
const report = getChatTabBrickRoadReport(orderedReports);
143+
const reportID = getChatTabBrickRoadReportID(orderedReportIDs, reportAttributes);
143144

144-
if (report) {
145-
Navigation.navigate(ROUTES.DEBUG_REPORT.getRoute(report.reportID));
145+
if (reportID) {
146+
Navigation.navigate(ROUTES.DEBUG_REPORT.getRoute(reportID));
146147
}
147148
}
148149
if (selectedTab === NAVIGATION_TABS.SETTINGS) {
@@ -152,7 +153,7 @@ function DebugTabView({selectedTab, chatTabBrickRoad}: DebugTabViewProps) {
152153
Navigation.navigate(route);
153154
}
154155
}
155-
}, [selectedTab, chatTabBrickRoad, orderedReports, status, reimbursementAccount, policyIDWithErrors]);
156+
}, [selectedTab, chatTabBrickRoad, orderedReportIDs, reportAttributes, status, reimbursementAccount, policyIDWithErrors]);
156157

157158
if (!([NAVIGATION_TABS.HOME, NAVIGATION_TABS.SETTINGS, NAVIGATION_TABS.WORKSPACES] as string[]).includes(selectedTab ?? '') || !indicator) {
158159
return null;

src/components/Navigation/NavigationTabBar/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function NavigationTabBar({selectedTab, isTooltipAllowed = false, isTopLevelBar
5555
const styles = useThemeStyles();
5656
const {translate, preferredLocale} = useLocalize();
5757
const {indicatorColor: workspacesTabIndicatorColor, status: workspacesTabIndicatorStatus} = useWorkspacesTabIndicatorStatus();
58-
const {orderedReports} = useSidebarOrderedReports();
58+
const {orderedReportIDs} = useSidebarOrderedReports();
5959
const subscriptionPlan = useSubscriptionPlan();
6060
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: false});
6161
const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES, {canBeMissing: true});
@@ -108,10 +108,8 @@ function NavigationTabBar({selectedTab, isTooltipAllowed = false, isTopLevelBar
108108
const shouldRenderDebugTabViewOnWideLayout = !!account?.isDebugModeEnabled && !isTopLevelBar;
109109

110110
useEffect(() => {
111-
setChatTabBrickRoad(getChatTabBrickRoad(orderedReports));
112-
// We need to get a new brick road state when report attributes are updated, otherwise we'll be showing an outdated brick road.
113-
// That's why reportAttributes is added as a dependency here
114-
}, [orderedReports, reportAttributes]);
111+
setChatTabBrickRoad(getChatTabBrickRoad(orderedReportIDs, reportAttributes));
112+
}, [orderedReportIDs, reportAttributes]);
115113

116114
const navigateToChats = useCallback(() => {
117115
if (selectedTab === NAVIGATION_TABS.HOME) {

src/hooks/useSidebarOrderedReports.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type SidebarOrderedReportsContextProviderProps = {
2525

2626
type SidebarOrderedReportsContextValue = {
2727
orderedReports: OnyxTypes.Report[];
28+
orderedReportIDs: string[];
2829
currentReportID: string | undefined;
2930
policyMemberAccountIDs: number[];
3031
};
@@ -33,6 +34,7 @@ type ReportsToDisplayInLHN = Record<string, OnyxTypes.Report & {hasErrorsOtherTh
3334

3435
const SidebarOrderedReportsContext = createContext<SidebarOrderedReportsContextValue>({
3536
orderedReports: [],
37+
orderedReportIDs: [],
3638
currentReportID: '',
3739
policyMemberAccountIDs: [],
3840
});
@@ -221,13 +223,15 @@ function SidebarOrderedReportsContextProvider({
221223
const updatedReports = getOrderedReports(updatedReportIDs);
222224
return {
223225
orderedReports: updatedReports,
226+
orderedReportIDs: updatedReportIDs,
224227
currentReportID: derivedCurrentReportID,
225228
policyMemberAccountIDs,
226229
};
227230
}
228231

229232
return {
230233
orderedReports,
234+
orderedReportIDs,
231235
currentReportID: derivedCurrentReportID,
232236
policyMemberAccountIDs,
233237
};

src/libs/WorkspacesSettingsUtils.ts

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,44 @@
1-
import Onyx from 'react-native-onyx';
21
import type {OnyxEntry} from 'react-native-onyx';
32
import type {ValueOf} from 'type-fest';
43
import type {LocaleContextProps} from '@components/LocaleContextProvider';
54
import CONST from '@src/CONST';
65
import type {TranslationPaths} from '@src/languages/types';
7-
import ONYXKEYS from '@src/ONYXKEYS';
8-
import type {Policy, Report, ReportAttributesDerivedValue} from '@src/types/onyx';
6+
import type {Policy, ReportAttributesDerivedValue} from '@src/types/onyx';
97
import type {Unit} from '@src/types/onyx/Policy';
108
import {convertToDisplayString} from './CurrencyUtils';
119

1210
type BrickRoad = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | undefined;
1311

14-
let reportAttributes: ReportAttributesDerivedValue['reports'];
15-
Onyx.connect({
16-
key: ONYXKEYS.DERIVED.REPORT_ATTRIBUTES,
17-
callback: (value) => {
18-
if (!value) {
19-
return;
20-
}
21-
reportAttributes = value.reports;
22-
},
23-
});
2412
/**
25-
* @param altReportActions Replaces (local) allReportActions used within (local) function getWorkspacesBrickRoads
26-
* @returns BrickRoad for the policy passed as a param and optionally actionsByReport (if passed)
13+
* @returns BrickRoad for the given reportID using reportAttributes
2714
*/
28-
const getBrickRoadForPolicy = (report: Report): BrickRoad => {
29-
return reportAttributes?.[report.reportID]?.brickRoadStatus;
15+
const getBrickRoadForPolicy = (reportID: string, reportAttributes: ReportAttributesDerivedValue['reports'] | undefined): BrickRoad => {
16+
return reportAttributes?.[reportID]?.brickRoadStatus;
3017
};
3118

32-
function getChatTabBrickRoadReport(orderedReports: Array<OnyxEntry<Report>> = []): OnyxEntry<Report> {
33-
if (!orderedReports.length) {
19+
function getChatTabBrickRoadReportID(orderedReportIDs: string[], reportAttributes: ReportAttributesDerivedValue['reports'] | undefined): string | undefined {
20+
if (!orderedReportIDs.length) {
3421
return undefined;
3522
}
3623

37-
let reportWithGBR: OnyxEntry<Report>;
24+
let reportIDWithGBR: string | undefined;
3825

39-
const reportWithRBR = orderedReports.find((report) => {
40-
const brickRoad = report ? getBrickRoadForPolicy(report) : undefined;
41-
if (!reportWithGBR && brickRoad === CONST.BRICK_ROAD_INDICATOR_STATUS.INFO) {
42-
reportWithGBR = report;
43-
return false;
26+
for (const reportID of orderedReportIDs) {
27+
const brickRoad = getBrickRoadForPolicy(reportID, reportAttributes);
28+
if (brickRoad === CONST.BRICK_ROAD_INDICATOR_STATUS.INFO) {
29+
reportIDWithGBR = reportID;
30+
}
31+
if (brickRoad === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR) {
32+
return reportID;
4433
}
45-
return brickRoad === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
46-
});
47-
48-
if (reportWithRBR) {
49-
return reportWithRBR;
50-
}
51-
52-
if (reportWithGBR) {
53-
return reportWithGBR;
5434
}
5535

56-
return undefined;
36+
return reportIDWithGBR;
5737
}
5838

59-
function getChatTabBrickRoad(orderedReports: Array<OnyxEntry<Report>>): BrickRoad | undefined {
60-
const report = getChatTabBrickRoadReport(orderedReports);
61-
return report ? getBrickRoadForPolicy(report) : undefined;
39+
function getChatTabBrickRoad(orderedReportIDs: string[], reportAttributes: ReportAttributesDerivedValue['reports'] | undefined): BrickRoad | undefined {
40+
const reportID = getChatTabBrickRoadReportID(orderedReportIDs, reportAttributes);
41+
return reportID ? getBrickRoadForPolicy(reportID, reportAttributes) : undefined;
6242
}
6343

6444
/**
@@ -145,5 +125,5 @@ function getOwnershipChecksDisplayText(
145125
return {title, text, buttonText};
146126
}
147127

148-
export {getChatTabBrickRoadReport, getBrickRoadForPolicy, getChatTabBrickRoad, getUnitTranslationKey, getOwnershipChecksDisplayText};
128+
export {getChatTabBrickRoadReportID, getBrickRoadForPolicy, getChatTabBrickRoad, getUnitTranslationKey, getOwnershipChecksDisplayText};
149129
export type {BrickRoad};

tests/unit/WorkspaceSettingsUtilsTest.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)