Skip to content

Commit 38fa9c2

Browse files
authored
Merge pull request Expensify#67753 from shubham1206agra/refactor-onyx-35
Removed Onyx.connect from DebugUtils (part 1)
2 parents 690d07e + 3778a30 commit 38fa9c2

4 files changed

Lines changed: 57 additions & 55 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=280 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=278 --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/libs/DebugUtils.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,6 @@ const TRANSACTION_REQUIRED_PROPERTIES: Array<keyof Transaction> = ['transactionI
8787

8888
const TRANSACTION_VIOLATION_REQUIRED_PROPERTIES: Array<keyof TransactionViolation> = ['type', 'name'] satisfies Array<keyof TransactionViolation>;
8989

90-
let isInFocusMode: OnyxEntry<boolean>;
91-
Onyx.connect({
92-
key: ONYXKEYS.NVP_PRIORITY_MODE,
93-
callback: (priorityMode) => {
94-
isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD;
95-
},
96-
});
97-
9890
let transactionViolations: OnyxCollection<TransactionViolation[]>;
9991
Onyx.connect({
10092
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
@@ -104,14 +96,6 @@ Onyx.connect({
10496
},
10597
});
10698

107-
let betas: OnyxEntry<Beta[]>;
108-
Onyx.connect({
109-
key: ONYXKEYS.BETAS,
110-
callback: (value) => {
111-
betas = value;
112-
},
113-
});
114-
11599
function stringifyJSON(data: Record<string, unknown>) {
116100
return JSON.stringify(data, null, 6);
117101
}
@@ -1333,7 +1317,21 @@ function validateTransactionViolationJSON(json: string) {
13331317
/**
13341318
* Gets the reason for showing LHN row
13351319
*/
1336-
function getReasonForShowingRowInLHN(report: OnyxEntry<Report>, chatReport: OnyxEntry<Report>, hasRBR = false, isReportArchived = false): TranslationPaths | null {
1320+
function getReasonForShowingRowInLHN({
1321+
report,
1322+
chatReport,
1323+
hasRBR = false,
1324+
isReportArchived = false,
1325+
isInFocusMode = false,
1326+
betas = undefined,
1327+
}: {
1328+
report: OnyxEntry<Report>;
1329+
chatReport: OnyxEntry<Report>;
1330+
hasRBR?: boolean;
1331+
isReportArchived?: boolean;
1332+
isInFocusMode?: boolean;
1333+
betas?: OnyxEntry<Beta[]>;
1334+
}): TranslationPaths | null {
13371335
if (!report) {
13381336
return null;
13391337
}
@@ -1345,7 +1343,7 @@ function getReasonForShowingRowInLHN(report: OnyxEntry<Report>, chatReport: Onyx
13451343
chatReport,
13461344
// We can't pass report.reportID because it will cause reason to always be isFocused
13471345
currentReportId: '-1',
1348-
isInFocusMode: !!isInFocusMode,
1346+
isInFocusMode,
13491347
betas,
13501348
excludeEmptyChats: true,
13511349
doesReportHaveViolations,

src/pages/Debug/Report/DebugReportPage.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ function DebugReportPage({
6060
selector: (attributes) => attributes?.reports?.[reportID],
6161
canBeMissing: true,
6262
});
63+
const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {canBeMissing: true});
64+
const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: true});
6365
const transactionID = DebugUtils.getTransactionID(report, reportActions);
6466
const isReportArchived = useReportIsArchived(reportID);
6567

@@ -76,7 +78,7 @@ function DebugReportPage({
7678
DebugUtils.getReasonAndReportActionForRBRInLHNRow(report, chatReport, reportActions, transactions, hasViolations, reportAttributes?.reportErrors ?? {}, isReportArchived) ?? {};
7779
const hasRBR = !!reasonRBR;
7880
const hasGBR = !hasRBR && !!reasonGBR;
79-
const reasonLHN = DebugUtils.getReasonForShowingRowInLHN(report, chatReport, hasRBR, isReportArchived);
81+
const reasonLHN = DebugUtils.getReasonForShowingRowInLHN({report, chatReport, betas, hasRBR, isReportArchived, isInFocusMode: priorityMode === CONST.PRIORITY_MODE.GSD});
8082

8183
return [
8284
{
@@ -121,7 +123,7 @@ function DebugReportPage({
121123
: undefined,
122124
},
123125
];
124-
}, [chatReport, report, transactionViolations, reportID, reportActions, transactions, reportAttributes?.reportErrors, isReportArchived, translate]);
126+
}, [report, transactionViolations, reportID, isReportArchived, chatReport, reportActions, transactions, reportAttributes?.reportErrors, betas, priorityMode, translate]);
125127

126128
const DebugDetailsTab = useCallback(
127129
() => (

tests/unit/DebugUtilsTest.ts

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -735,56 +735,55 @@ describe('DebugUtils', () => {
735735
Onyx.clear();
736736
});
737737
it('returns null when report is not defined', () => {
738-
const reason = DebugUtils.getReasonForShowingRowInLHN(undefined, chatReportR14932);
738+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: undefined, chatReport: chatReportR14932});
739739
expect(reason).toBeNull();
740740
});
741741
it('returns correct reason when report has a valid draft comment', async () => {
742742
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}1`, 'Hello world!');
743-
const reason = DebugUtils.getReasonForShowingRowInLHN(baseReport, chatReportR14932);
743+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: baseReport, chatReport: chatReportR14932});
744744
expect(reason).toBe('debug.reasonVisibleInLHN.hasDraftComment');
745745
});
746746
it('returns correct reason when report has GBR', () => {
747-
const reason = DebugUtils.getReasonForShowingRowInLHN(
748-
{
747+
const reason = DebugUtils.getReasonForShowingRowInLHN({
748+
report: {
749749
...baseReport,
750750
lastMentionedTime: '2024-08-10 18:70:44.171',
751751
lastReadTime: '2024-08-08 18:70:44.171',
752752
},
753-
chatReportR14932,
754-
);
753+
chatReport: chatReportR14932,
754+
});
755755
expect(reason).toBe('debug.reasonVisibleInLHN.hasGBR');
756756
});
757757
it('returns correct reason when report is pinned', () => {
758-
const reason = DebugUtils.getReasonForShowingRowInLHN(
759-
{
758+
const reason = DebugUtils.getReasonForShowingRowInLHN({
759+
report: {
760760
...baseReport,
761761
isPinned: true,
762762
},
763-
chatReportR14932,
764-
);
763+
chatReport: chatReportR14932,
764+
});
765765
expect(reason).toBe('debug.reasonVisibleInLHN.pinnedByUser');
766766
});
767767
it('returns correct reason when report has add workspace room errors', () => {
768-
const reason = DebugUtils.getReasonForShowingRowInLHN(
769-
{
768+
const reason = DebugUtils.getReasonForShowingRowInLHN({
769+
report: {
770770
...baseReport,
771771
errorFields: {
772772
addWorkspaceRoom: {
773773
error: 'Something happened',
774774
},
775775
},
776776
},
777-
chatReportR14932,
778-
);
777+
chatReport: chatReportR14932,
778+
});
779779
expect(reason).toBe('debug.reasonVisibleInLHN.hasAddWorkspaceRoomErrors');
780780
});
781781
it('returns correct reason when report is unread', async () => {
782-
await Onyx.set(ONYXKEYS.NVP_PRIORITY_MODE, CONST.PRIORITY_MODE.GSD);
783782
await Onyx.set(ONYXKEYS.SESSION, {
784783
accountID: 1234,
785784
});
786-
const reason = DebugUtils.getReasonForShowingRowInLHN(
787-
{
785+
const reason = DebugUtils.getReasonForShowingRowInLHN({
786+
report: {
788787
...baseReport,
789788
participants: {
790789
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -796,39 +795,42 @@ describe('DebugUtils', () => {
796795
lastReadTime: '2024-08-08 18:70:44.171',
797796
lastMessageText: 'Hello world!',
798797
},
799-
chatReportR14932,
800-
);
798+
chatReport: chatReportR14932,
799+
isInFocusMode: true,
800+
});
801801
expect(reason).toBe('debug.reasonVisibleInLHN.isUnread');
802802
});
803803
it('returns correct reason when report is archived', async () => {
804804
const reportNameValuePairs = {
805805
private_isArchived: DateUtils.getDBTime(),
806806
};
807-
await Onyx.set(ONYXKEYS.NVP_PRIORITY_MODE, CONST.PRIORITY_MODE.DEFAULT);
808807
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${baseReport.reportID}`, reportNameValuePairs);
809808
const {result: isReportArchived} = renderHook(() => useReportIsArchived(baseReport?.reportID));
810-
const reason = DebugUtils.getReasonForShowingRowInLHN(
811-
{
809+
const reason = DebugUtils.getReasonForShowingRowInLHN({
810+
report: {
812811
...baseReport,
813812
},
814-
chatReportR14932,
815-
false,
816-
isReportArchived.current,
817-
);
813+
chatReport: chatReportR14932,
814+
hasRBR: false,
815+
isReportArchived: isReportArchived.current,
816+
});
818817
expect(reason).toBe('debug.reasonVisibleInLHN.isArchived');
819818
});
820819
it('returns correct reason when report is self DM', () => {
821-
const reason = DebugUtils.getReasonForShowingRowInLHN(
822-
{
820+
const reason = DebugUtils.getReasonForShowingRowInLHN({
821+
report: {
823822
...baseReport,
824823
chatType: CONST.REPORT.CHAT_TYPE.SELF_DM,
825824
},
826-
chatReportR14932,
827-
);
825+
chatReport: chatReportR14932,
826+
});
828827
expect(reason).toBe('debug.reasonVisibleInLHN.isSelfDM');
829828
});
830829
it('returns correct reason when report is temporarily focused', () => {
831-
const reason = DebugUtils.getReasonForShowingRowInLHN(baseReport, chatReportR14932);
830+
const reason = DebugUtils.getReasonForShowingRowInLHN({
831+
report: baseReport,
832+
chatReport: chatReportR14932,
833+
});
832834
expect(reason).toBe('debug.reasonVisibleInLHN.isFocused');
833835
});
834836
it('returns correct reason when report has one transaction thread with violations', async () => {
@@ -886,7 +888,7 @@ describe('DebugUtils', () => {
886888
},
887889
],
888890
});
889-
const reason = DebugUtils.getReasonForShowingRowInLHN(MOCK_TRANSACTION_REPORT, chatReportR14932, true);
891+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: MOCK_TRANSACTION_REPORT, chatReport: chatReportR14932, hasRBR: true});
890892
expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR');
891893
});
892894
it('returns correct reason when report has violations', async () => {
@@ -944,11 +946,11 @@ describe('DebugUtils', () => {
944946
},
945947
],
946948
});
947-
const reason = DebugUtils.getReasonForShowingRowInLHN(MOCK_EXPENSE_REPORT, chatReportR14932, true);
949+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: MOCK_EXPENSE_REPORT, chatReport: chatReportR14932, hasRBR: true});
948950
expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR');
949951
});
950952
it('returns correct reason when report has errors', () => {
951-
const reason = DebugUtils.getReasonForShowingRowInLHN(baseReport, chatReportR14932, true);
953+
const reason = DebugUtils.getReasonForShowingRowInLHN({report: baseReport, chatReport: chatReportR14932, hasRBR: true});
952954
expect(reason).toBe('debug.reasonVisibleInLHN.hasRBR');
953955
});
954956
});

0 commit comments

Comments
 (0)