Skip to content

Commit e3d991e

Browse files
authored
Merge pull request Expensify#67298 from truph01/fix/67097
fix: Remove call to getReportNameValuePairs() in method canSeeDefaultRoom
2 parents aa807db + 44fdf80 commit e3d991e

5 files changed

Lines changed: 69 additions & 15 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7904,11 +7904,9 @@ function isIOUOwnedByCurrentUser(report: OnyxEntry<Report>, allReportsDict?: Ony
79047904
* Assuming the passed in report is a default room, lets us know whether we can see it or not, based on permissions and
79057905
* the various subsets of users we've allowed to use default rooms.
79067906
*/
7907-
function canSeeDefaultRoom(report: OnyxEntry<Report>, betas: OnyxEntry<Beta[]>): boolean {
7907+
function canSeeDefaultRoom(report: OnyxEntry<Report>, betas: OnyxEntry<Beta[]>, isReportArchived = false): boolean {
79087908
// Include archived rooms
7909-
// This will get removed as part of https://github.com/Expensify/App/issues/59961
7910-
// eslint-disable-next-line deprecation/deprecation
7911-
if (isArchivedNonExpenseReport(report, !!getReportNameValuePairs(report?.reportID)?.private_isArchived)) {
7909+
if (isArchivedNonExpenseReport(report, isReportArchived)) {
79127910
return true;
79137911
}
79147912

@@ -7926,9 +7924,9 @@ function canSeeDefaultRoom(report: OnyxEntry<Report>, betas: OnyxEntry<Beta[]>):
79267924
return Permissions.isBetaEnabled(CONST.BETAS.DEFAULT_ROOMS, betas ?? []);
79277925
}
79287926

7929-
function canAccessReport(report: OnyxEntry<Report>, betas: OnyxEntry<Beta[]>): boolean {
7927+
function canAccessReport(report: OnyxEntry<Report>, betas: OnyxEntry<Beta[]>, isReportArchived = false): boolean {
79307928
// We hide default rooms (it's basically just domain rooms now) from people who aren't on the defaultRooms beta.
7931-
if (isDefaultRoom(report) && !canSeeDefaultRoom(report, betas)) {
7929+
if (isDefaultRoom(report) && !canSeeDefaultRoom(report, betas, isReportArchived)) {
79327930
return false;
79337931
}
79347932

@@ -8239,7 +8237,7 @@ function reasonForReportToBeInOptionList({
82398237
return null;
82408238
}
82418239

8242-
if (!canAccessReport(report, betas)) {
8240+
if (!canAccessReport(report, betas, isReportArchived)) {
82438241
return null;
82448242
}
82458243

@@ -9331,8 +9329,8 @@ function isReportParticipant(accountID: number | undefined, report: OnyxEntry<Re
93319329
/**
93329330
* Check to see if the current user has access to view the report.
93339331
*/
9334-
function canCurrentUserOpenReport(report: OnyxEntry<Report>): boolean {
9335-
return (isReportParticipant(currentUserAccountID, report) || isPublicRoom(report)) && canAccessReport(report, allBetas);
9332+
function canCurrentUserOpenReport(report: OnyxEntry<Report>, isReportArchived = false): boolean {
9333+
return (isReportParticipant(currentUserAccountID, report) || isPublicRoom(report)) && canAccessReport(report, allBetas, isReportArchived);
93369334
}
93379335

93389336
function shouldUseFullTitleToDisplay(report: OnyxEntry<Report>): boolean {

src/pages/home/report/ReportActionItemParentAction.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {View} from 'react-native';
33
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
44
import OfflineWithFeedback from '@components/OfflineWithFeedback';
55
import useNetwork from '@hooks/useNetwork';
6+
import useOnyx from '@hooks/useOnyx';
67
import useResponsiveLayout from '@hooks/useResponsiveLayout';
78
import useThemeStyles from '@hooks/useThemeStyles';
89
import onyxSubscribe from '@libs/onyxSubscribe';
@@ -13,6 +14,7 @@ import {
1314
canUserPerformWriteAction as canUserPerformWriteActionReportUtils,
1415
getAllAncestorReportActionIDs,
1516
getAllAncestorReportActions,
17+
isArchivedReport,
1618
navigateToLinkedReportAction,
1719
} from '@libs/ReportUtils';
1820
import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report';
@@ -81,6 +83,19 @@ function ReportActionItemParentAction({
8183
const [allAncestors, setAllAncestors] = useState<Ancestor[]>([]);
8284
const {isOffline} = useNetwork();
8385
const {isInNarrowPaneModal} = useResponsiveLayout();
86+
const [ancestorReportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {
87+
canBeMissing: true,
88+
selector: (allPairs) => {
89+
const ancestorIDsToSelect = new Set(ancestorIDs.current.reportIDs);
90+
91+
return Object.fromEntries(
92+
Object.entries(allPairs ?? {}).filter(([key]) => {
93+
const id = key.split('_').at(1);
94+
return id && ancestorIDsToSelect.has(id);
95+
}),
96+
);
97+
},
98+
});
8499

85100
useEffect(() => {
86101
const unsubscribeReports: Array<() => void> = [];
@@ -124,7 +139,9 @@ function ReportActionItemParentAction({
124139
const ancestorReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${ancestor.report.reportID}`];
125140
const canUserPerformWriteAction = canUserPerformWriteActionReportUtils(ancestorReport);
126141
const shouldDisplayThreadDivider = !isTripPreview(ancestor.reportAction);
127-
142+
const reportNameValuePair =
143+
ancestorReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${ancestorReports.current?.[ancestor?.report?.reportID]?.reportID}`];
144+
const isAncestorReportArchived = isArchivedReport(reportNameValuePair);
128145
return (
129146
<OfflineWithFeedback
130147
key={ancestor.reportAction.reportActionID}
@@ -137,14 +154,14 @@ function ReportActionItemParentAction({
137154
{shouldDisplayThreadDivider && (
138155
<ThreadDivider
139156
ancestor={ancestor}
140-
isLinkDisabled={!canCurrentUserOpenReport(ancestorReports.current?.[ancestor?.report?.reportID])}
157+
isLinkDisabled={!canCurrentUserOpenReport(ancestorReports.current?.[ancestor?.report?.reportID], isAncestorReportArchived)}
141158
/>
142159
)}
143160
<ReportActionItem
144161
allReports={allReports}
145162
policies={policies}
146163
onPress={
147-
canCurrentUserOpenReport(ancestorReports.current?.[ancestor?.report?.reportID])
164+
canCurrentUserOpenReport(ancestorReports.current?.[ancestor?.report?.reportID], isAncestorReportArchived)
148165
? () => navigateToLinkedReportAction(ancestor, isInNarrowPaneModal, canUserPerformWriteAction, isOffline)
149166
: undefined
150167
}

src/pages/home/report/withReportAndReportActionOrNotFound.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {useEffect, useMemo} from 'react';
44
import type {OnyxEntry} from 'react-native-onyx';
55
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
66
import useOnyx from '@hooks/useOnyx';
7+
import useReportIsArchived from '@hooks/useReportIsArchived';
78
import useResponsiveLayout from '@hooks/useResponsiveLayout';
89
import {openReport} from '@libs/actions/Report';
910
import getComponentDisplayName from '@libs/getComponentDisplayName';
@@ -83,7 +84,8 @@ export default function <TProps extends WithReportAndReportActionOrNotFoundProps
8384
// Perform all the loading checks
8485
const isLoadingReport = isLoadingReportData && !report?.reportID;
8586
const isLoadingReportAction = isEmptyObject(reportActions) || (reportMetadata?.isLoadingInitialReportActions && isEmptyObject(linkedReportAction));
86-
const shouldHideReport = !isLoadingReport && (!report?.reportID || !canAccessReport(report, betas));
87+
const isReportArchived = useReportIsArchived(report?.reportID);
88+
const shouldHideReport = !isLoadingReport && (!report?.reportID || !canAccessReport(report, betas, isReportArchived));
8789

8890
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
8991
if ((isLoadingReport || isLoadingReportAction) && !shouldHideReport) {

src/pages/home/report/withReportOrNotFound.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, {useEffect} from 'react';
55
import type {OnyxEntry} from 'react-native-onyx';
66
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
77
import useOnyx from '@hooks/useOnyx';
8+
import useReportIsArchived from '@hooks/useReportIsArchived';
89
import {openReport} from '@libs/actions/Report';
910
import getComponentDisplayName from '@libs/getComponentDisplayName';
1011
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
@@ -72,7 +73,7 @@ export default function (
7273
const contentShown = React.useRef(false);
7374
const isReportIdInRoute = !!props.route.params.reportID?.length;
7475
const isReportLoaded = !isEmptyObject(report) && !!report?.reportID;
75-
76+
const isReportArchived = useReportIsArchived(report?.reportID);
7677
// The `isLoadingInitialReportActions` value will become `false` only after the first OpenReport API call is finished (either succeeded or failed)
7778
const shouldFetchReport = isReportIdInRoute && reportMetadata?.isLoadingInitialReportActions !== false;
7879

@@ -90,7 +91,7 @@ export default function (
9091

9192
if (shouldRequireReportID || isReportIdInRoute) {
9293
const shouldShowFullScreenLoadingIndicator = !isReportLoaded && (isLoadingReportData !== false || shouldFetchReport);
93-
const shouldShowNotFoundPage = !isReportLoaded || !canAccessReport(report, betas);
94+
const shouldShowNotFoundPage = !isReportLoaded || !canAccessReport(report, betas, isReportArchived);
9495

9596
// If the content was shown, but it's not anymore, that means the report was deleted, and we are probably navigating out of this screen.
9697
// Return null for this case to avoid rendering FullScreenLoadingIndicator or NotFoundPage when animating transition.

tests/unit/ReportUtilsTest.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
canHoldUnholdReportAction,
3232
canJoinChat,
3333
canLeaveChat,
34+
canSeeDefaultRoom,
3435
canUserPerformWriteAction,
3536
findLastAccessedReport,
3637
getAllAncestorReportActions,
@@ -228,6 +229,10 @@ const personalDetails: PersonalDetailsList = {
228229
accountID: 7,
229230
login: 'owner@test.com',
230231
},
232+
'8': {
233+
accountID: 8,
234+
login: CONST.EMAIL.GUIDES_DOMAIN,
235+
},
231236
};
232237

233238
const rules = {
@@ -4978,4 +4983,35 @@ describe('ReportUtils', () => {
49784983
expect(getReportStatusTranslation(undefined, CONST.REPORT.STATUS_NUM.OPEN)).toBe('');
49794984
});
49804985
});
4986+
describe('canSeeDefaultRoom', () => {
4987+
it('should return true if report is archived room ', () => {
4988+
const betas = [CONST.BETAS.DEFAULT_ROOMS];
4989+
const report: Report = {
4990+
...createRandomReport(40002),
4991+
type: CONST.REPORT.TYPE.CHAT,
4992+
participants: buildParticipantsFromAccountIDs([currentUserAccountID, 1]),
4993+
};
4994+
expect(canSeeDefaultRoom(report, betas, true)).toBe(true);
4995+
});
4996+
it('should return true if the room has an assigned guide', () => {
4997+
const betas = [CONST.BETAS.DEFAULT_ROOMS];
4998+
const report: Report = {
4999+
...createRandomReport(40002),
5000+
participants: buildParticipantsFromAccountIDs([currentUserAccountID, 8]),
5001+
};
5002+
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails).then(() => {
5003+
expect(canSeeDefaultRoom(report, betas, false)).toBe(true);
5004+
});
5005+
});
5006+
it('should return true if the report is admin room', () => {
5007+
const betas = [CONST.BETAS.DEFAULT_ROOMS];
5008+
const report: Report = {
5009+
...createRandomReport(40002),
5010+
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
5011+
};
5012+
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails).then(() => {
5013+
expect(canSeeDefaultRoom(report, betas, false)).toBe(true);
5014+
});
5015+
});
5016+
});
49815017
});

0 commit comments

Comments
 (0)