Skip to content

Commit

Permalink
Merge pull request Expensify#54113 from callstack-internal/VickyStash…
Browse files Browse the repository at this point in the history
…/feature/53141-remove-getReport-export

Remove ReportUtils.getReport method export
  • Loading branch information
mountiny authored Dec 18, 2024
2 parents 471962e + 5b97ab6 commit 9cc0012
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import tryResolveUrlFromApiRoot from '@libs/tryResolveUrlFromApiRoot';
import CONST from '@src/CONST';
import type {ReportAction, ReportActions} from '@src/types/onyx';
import type {Report, ReportAction, ReportActions} from '@src/types/onyx';
import type {Note} from '@src/types/onyx/Report';

/**
Expand All @@ -20,12 +20,11 @@ function extractAttachments(
accountID,
parentReportAction,
reportActions,
reportID,
}: {privateNotes?: Record<number, Note>; accountID?: number; parentReportAction?: OnyxEntry<ReportAction>; reportActions?: OnyxEntry<ReportActions>; reportID: string},
report,
}: {privateNotes?: Record<number, Note>; accountID?: number; parentReportAction?: OnyxEntry<ReportAction>; reportActions?: OnyxEntry<ReportActions>; report: OnyxEntry<Report>},
) {
const targetNote = privateNotes?.[Number(accountID)]?.note ?? '';
const attachments: Attachment[] = [];
const report = ReportUtils.getReport(reportID);
const canUserPerformWriteAction = ReportUtils.canUserPerformWriteAction(report);

// We handle duplicate image sources by considering the first instance as original. Selecting any duplicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ function AttachmentCarousel({report, source, onNavigate, setDownloadButtonVisibi
const parentReportAction = report.parentReportActionID && parentReportActions ? parentReportActions[report.parentReportActionID] : undefined;
let newAttachments: Attachment[] = [];
if (type === CONST.ATTACHMENT_TYPE.NOTE && accountID) {
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.NOTE, {privateNotes: report.privateNotes, accountID, reportID: report.reportID});
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.NOTE, {privateNotes: report.privateNotes, accountID, report});
} else {
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.REPORT, {parentReportAction, reportActions, reportID: report.reportID});
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.REPORT, {parentReportAction, reportActions, report});
}

let newIndex = newAttachments.findIndex(compareImage);
Expand Down Expand Up @@ -68,7 +68,7 @@ function AttachmentCarousel({report, source, onNavigate, setDownloadButtonVisibi
}
}
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [reportActions, compareImage]);
}, [reportActions, compareImage, report]);

/** Updates the page state when the user navigates between attachments */
const updatePage = useCallback(
Expand Down
18 changes: 3 additions & 15 deletions src/components/Attachments/AttachmentCarousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ function AttachmentCarousel({report, source, onNavigate, setDownloadButtonVisibi
const parentReportAction = report.parentReportActionID && parentReportActions ? parentReportActions[report.parentReportActionID] : undefined;
let newAttachments: Attachment[] = [];
if (type === CONST.ATTACHMENT_TYPE.NOTE && accountID) {
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.NOTE, {privateNotes: report.privateNotes, accountID, reportID: report.reportID});
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.NOTE, {privateNotes: report.privateNotes, accountID, report});
} else {
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.REPORT, {parentReportAction, reportActions: reportActions ?? undefined, reportID: report.reportID});
newAttachments = extractAttachments(CONST.ATTACHMENT_TYPE.REPORT, {parentReportAction, reportActions: reportActions ?? undefined, report});
}

if (isEqual(attachments, newAttachments)) {
Expand Down Expand Up @@ -130,19 +130,7 @@ function AttachmentCarousel({report, source, onNavigate, setDownloadButtonVisibi
onNavigate(attachment);
}
}
}, [
report.privateNotes,
reportActions,
parentReportActions,
compareImage,
report.parentReportActionID,
attachments,
setDownloadButtonVisibility,
onNavigate,
accountID,
type,
report.reportID,
]);
}, [reportActions, parentReportActions, compareImage, attachments, setDownloadButtonVisibility, onNavigate, accountID, type, report]);

// Scroll position is affected when window width is resized, so we readjust it on width changes
useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
});
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`);
const targetPolicyID = updatedTransaction?.reportID ? ReportUtils.getReport(updatedTransaction?.reportID)?.policyID : policyID;
const [transactionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${updatedTransaction?.reportID}`);
const targetPolicyID = updatedTransaction?.reportID ? transactionReport?.policyID : policyID;
const [policyTagList] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${targetPolicyID}`);
const [parentReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, {
canEvict: false,
Expand Down
11 changes: 10 additions & 1 deletion src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ Onyx.connect({
callback: (val) => (allPolicies = val),
});

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

const lastReportActions: ReportActions = {};
const allSortedReportActions: Record<string, ReportAction[]> = {};
let allReportActions: OnyxCollection<ReportActions>;
Expand Down Expand Up @@ -254,7 +263,7 @@ Onyx.connect({
lastReportActions[reportID] = firstReportAction;
}

const report = ReportUtils.getReport(reportID);
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const canUserPerformWriteAction = ReportUtils.canUserPerformWriteAction(report);

// The report is only visible if it is the last action not deleted that
Expand Down
1 change: 0 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8813,7 +8813,6 @@ export {
getReportLastMessage,
getMostRecentlyVisitedReport,
getSourceIDFromReportAction,
getReport,
getReportNameValuePairs,
hasReportViolations,
isPayAtEndExpenseReport,
Expand Down
16 changes: 13 additions & 3 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ Onyx.connect({
allPersonalDetails = value ?? {};
},
});

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

Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!actions || !key) {
return;
}
const reportID = CollectionUtils.extractCollectionItemID(key);
const report = ReportUtils.getReport(reportID);
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const canUserPerformWriteAction = ReportUtils.canUserPerformWriteAction(report);
const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(actions));

Expand Down Expand Up @@ -91,7 +101,7 @@ function ensureSingleSpacing(text: string) {
*/
function getOrderedReportIDs(
currentReportId: string | null,
allReports: OnyxCollection<Report>,
reports: OnyxCollection<Report>,
betas: OnyxEntry<Beta[]>,
policies: OnyxCollection<PolicySelector>,
priorityMode: OnyxEntry<PriorityMode>,
Expand All @@ -102,7 +112,7 @@ function getOrderedReportIDs(
Performance.markStart(CONST.TIMING.GET_ORDERED_REPORT_IDS);
const isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD;
const isInDefaultMode = !isInFocusMode;
const allReportsDictValues = Object.values(allReports ?? {});
const allReportsDictValues = Object.values(reports ?? {});

// Filter out all the reports that shouldn't be displayed
let reportsToDisplay: Array<Report & {hasErrorsOtherThanFailedReceipt?: boolean}> = [];
Expand Down
11 changes: 10 additions & 1 deletion src/libs/WorkspacesSettingsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ Onyx.connect({
},
});

let reportsCollection: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
reportsCollection = value;
},
});

let allTransactionViolations: NonNullable<OnyxCollection<TransactionViolations>> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
Expand Down Expand Up @@ -74,7 +83,7 @@ const getBrickRoadForPolicy = (report: Report, altReportActions?: OnyxCollection
}

if (oneTransactionThreadReportID && !doesReportContainErrors) {
const oneTransactionThreadReport = ReportUtils.getReport(oneTransactionThreadReportID);
const oneTransactionThreadReport = reportsCollection?.[`${ONYXKEYS.COLLECTION.REPORT}${oneTransactionThreadReportID}`];

if (ReportUtils.shouldDisplayViolationsRBRInLHN(oneTransactionThreadReport, allTransactionViolations)) {
doesReportContainErrors = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
Expand Down
8 changes: 5 additions & 3 deletions src/pages/ErrorPage/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import {useOnyx} from 'react-native-onyx';
import type {FullPageNotFoundViewProps} from '@components/BlockingViews/FullPageNotFoundView';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import ScreenWrapper from '@components/ScreenWrapper';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';

type NotFoundPageProps = {
onBackButtonPress?: () => void;
Expand All @@ -16,6 +17,8 @@ function NotFoundPage({onBackButtonPress = () => Navigation.goBack(), isReportRe
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to go back to the not found page on large screens and to the home page on small screen
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const topmostReportId = Navigation.getTopmostReportId();
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${topmostReportId}`);

return (
<ScreenWrapper testID={NotFoundPage.displayName}>
Expand All @@ -26,8 +29,7 @@ function NotFoundPage({onBackButtonPress = () => Navigation.goBack(), isReportRe
onBackButtonPress();
return;
}
const topmostReportId = Navigation.getTopmostReportId();
const report = ReportUtils.getReport(topmostReportId ?? '');

// detect the report is invalid
if (topmostReportId && (!report || report.errorFields?.notFound)) {
Navigation.dismissModal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ function BaseReportActionContextMenu({
const transactionID = ReportActionsUtils.getLinkedTransactionID(reportActionID, reportID);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
const [user] = useOnyx(ONYXKEYS.USER);
const policyID = ReportUtils.getReport(reportID)?.policyID;
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const policyID = report?.policyID;
const workspaceAccountID = PolicyUtils.getWorkspaceAccountID(policyID ?? '-1');
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`);
Expand All @@ -151,7 +152,7 @@ function BaseReportActionContextMenu({

const [download] = useOnyx(`${ONYXKEYS.COLLECTION.DOWNLOAD}${sourceID}`);

const childReport = ReportUtils.getReport(reportAction?.childReportID ?? '-1');
const [childReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportAction?.childReportID}`);
const parentReportAction = ReportActionsUtils.getReportAction(childReport?.parentReportID ?? '', childReport?.parentReportActionID ?? '');
const {reportActions: paginatedReportActions} = usePaginatedReportActions(childReport?.reportID ?? '-1');

Expand All @@ -178,7 +179,7 @@ function BaseReportActionContextMenu({
const moneyRequestAction = transactionThreadReportID ? requestParentReportAction : parentReportAction;

const [parentReportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${childReport?.parentReportID ?? '-1'}`);
const parentReport = ReportUtils.getReport(childReport?.parentReportID ?? '-1');
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${childReport?.parentReportID}`);

const isMoneyRequest = useMemo(() => ReportUtils.isMoneyRequest(childReport), [childReport]);
const isTrackExpenseReport = ReportUtils.isTrackExpenseReport(childReport);
Expand Down Expand Up @@ -323,6 +324,7 @@ function BaseReportActionContextMenu({
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
reportAction: (reportAction ?? null) as ReportAction,
reportID,
report,
draftMessage,
selection,
close: () => setShouldKeepOpen(false),
Expand Down
9 changes: 4 additions & 5 deletions src/pages/home/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ROUTES from '@src/ROUTES';
import type {Beta, Download as DownloadOnyx, OnyxInputOrEntry, ReportAction, ReportActionReactions, Transaction, User} from '@src/types/onyx';
import type {Beta, Download as DownloadOnyx, OnyxInputOrEntry, ReportAction, ReportActionReactions, Report as ReportType, Transaction, User} from '@src/types/onyx';
import type IconAsset from '@src/types/utils/IconAsset';
import type {ContextMenuAnchor} from './ReportActionContextMenu';
import {hideContextMenu, showDeleteModal} from './ReportActionContextMenu';
Expand Down Expand Up @@ -75,6 +75,7 @@ type ContextMenuActionPayload = {
reportAction: ReportAction;
transaction?: OnyxEntry<Transaction>;
reportID: string;
report: OnyxEntry<ReportType>;
draftMessage: string;
selection: string;
close: () => void;
Expand Down Expand Up @@ -374,7 +375,7 @@ const ContextMenuActions: ContextMenuAction[] = [
// If return value is true, we switch the `text` and `icon` on
// `ContextMenuItem` with `successText` and `successIcon` which will fall back to
// the `text` and `icon`
onPress: (closePopover, {reportAction, transaction, selection, reportID, hasCard}) => {
onPress: (closePopover, {reportAction, transaction, selection, report, reportID, hasCard}) => {
const isReportPreviewAction = ReportActionsUtils.isReportPreviewAction(reportAction);
const messageHtml = getActionHtml(reportAction);
const messageText = ReportActionsUtils.getReportActionMessageText(reportAction);
Expand Down Expand Up @@ -476,7 +477,6 @@ const ContextMenuActions: ContextMenuAction[] = [
const {label, errorMessage} = ReportActionsUtils.getOriginalMessage(reportAction) ?? {label: '', errorMessage: ''};
setClipboardMessage(Localize.translateLocal('report.actions.type.integrationSyncFailed', {label, errorMessage}));
} else if (ReportActionsUtils.isCardIssuedAction(reportAction)) {
const report = ReportUtils.getReport(reportID);
setClipboardMessage(ReportActionsUtils.getCardIssuedMessage(reportAction, true, report?.policyID, hasCard));
} else if (ReportActionsUtils.isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.DELETE_INTEGRATION)) {
setClipboardMessage(ReportActionsUtils.getRemovedConnectionMessage(reportAction));
Expand Down Expand Up @@ -603,8 +603,7 @@ const ContextMenuActions: ContextMenuAction[] = [
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: ({type, isProduction}) => type === CONST.CONTEXT_MENU_TYPES.REPORT && !isProduction,
onPress: (closePopover, {reportID}) => {
const report = ReportUtils.getReport(reportID);
onPress: (closePopover, {report}) => {
Clipboard.setString(JSON.stringify(report, null, 4));
hideContextMenu(true, ReportActionComposeFocusManager.focus);
},
Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/report/ReportActionItemMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ReportActionItemMessageProps = {
function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHidden = false}: ReportActionItemMessageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${ReportActionsUtils.getLinkedTransactionID(action) ?? -1}`);

const fragments = ReportActionsUtils.getReportActionMessageFragments(action);
Expand Down Expand Up @@ -122,7 +123,7 @@ function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHid
};

const openWorkspaceInvoicesPage = () => {
const policyID = ReportUtils.getReport(reportID)?.policyID;
const policyID = report?.policyID;

if (!policyID) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/pages/home/report/ReportActionItemParentAction.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import TripDetailsView from '@components/ReportActionItem/TripDetailsView';
import useNetwork from '@hooks/useNetwork';
Expand Down Expand Up @@ -63,6 +64,7 @@ function ReportActionItemParentAction({
shouldUseThreadDividerLine = false,
}: ReportActionItemParentActionProps) {
const styles = useThemeStyles();
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const ancestorIDs = useRef(ReportUtils.getAllAncestorReportActionIDs(report));
const ancestorReports = useRef<Record<string, OnyxEntry<OnyxTypes.Report>>>({});
const [allAncestors, setAllAncestors] = useState<ReportUtils.Ancestor[]>([]);
Expand Down Expand Up @@ -107,7 +109,7 @@ function ReportActionItemParentAction({
<AnimatedEmptyStateBackground />
{/* eslint-disable-next-line react-compiler/react-compiler */}
{allAncestors.map((ancestor) => {
const ancestorReport = ReportUtils.getReport(ancestor.report.reportID);
const ancestorReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${ancestor.report.reportID}`];
const canUserPerformWriteAction = ReportUtils.canUserPerformWriteAction(ancestorReport);
return (
<OfflineWithFeedback
Expand Down
Loading

0 comments on commit 9cc0012

Please sign in to comment.