Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/MoneyReportHeaderActions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
import type {ButtonWithDropdownMenuRef} from '@components/ButtonWithDropdownMenu/types';
import MoneyReportHeaderPrimaryAction from '@components/MoneyReportHeaderPrimaryAction';
import {useMoneyReportTransactionThread} from '@components/MoneyReportTransactionThreadContext';
import {useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext';
import useExportAgainModal from '@hooks/useExportAgainModal';
import useOnyx from '@hooks/useOnyx';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useResponsiveLayoutOnWideRHP from '@hooks/useResponsiveLayoutOnWideRHP';
import useThemeStyles from '@hooks/useThemeStyles';
import useTransactionThreadReport from '@hooks/useTransactionThreadReport';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -40,7 +40,7 @@ function MoneyReportHeaderActions({reportID, primaryAction, isReportInSearch, ba
const [moneyRequestReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(moneyRequestReport?.chatReportID)}`);

const {transactionThreadReportID} = useTransactionThreadReport(reportID);
const {transactionThreadReportID} = useMoneyReportTransactionThread();

const {triggerExportOrConfirm} = useExportAgainModal(moneyRequestReport?.reportID, moneyRequestReport?.policyID);

Expand Down
26 changes: 2 additions & 24 deletions src/components/MoneyReportHeaderEducationalModals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import type {ValueOf} from 'type-fest';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
import useTransactionsAndViolationsForReport from '@hooks/useTransactionsAndViolationsForReport';
import {setNameValuePair} from '@libs/actions/User';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getAllNonDeletedTransactions} from '@libs/MoneyRequestReportUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getFilteredReportActionsForReportView, getOneTransactionThreadReportID, getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils';
import {changeMoneyRequestHoldStatus, rejectMoneyRequestReason} from '@libs/ReportUtils';
import {dismissRejectUseExplanation} from '@userActions/IOU/RejectMoneyRequest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
import HoldOrRejectEducationalModal from './HoldOrRejectEducationalModal';
import HoldSubmitterEducationalModal from './HoldSubmitterEducationalModal';
import {useMoneyReportTransactionThread} from './MoneyReportTransactionThreadContext';

type RejectModalAction = ValueOf<
typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.HOLD | typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.REJECT | typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.REJECT_BULK
Expand All @@ -42,27 +38,9 @@ function MoneyReportHeaderEducationalModals({reportID, ref}: MoneyReportHeaderEd
const {isOffline} = useNetwork();
const [shouldFailAllRequests] = useOnyx(ONYXKEYS.NETWORK, {selector: shouldFailAllRequestsSelector});

// Fetch report data needed for educational modals
const [moneyRequestReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReport?.chatReportID}`);
const {reportActions: unfilteredReportActions} = usePaginatedReportActions(moneyRequestReport?.reportID);
const reportActions = getFilteredReportActionsForReportView(unfilteredReportActions);
const {transactions: reportTransactions} = useTransactionsAndViolationsForReport(moneyRequestReport?.reportID);
const {login: currentUserLogin, accountID: currentUserAccountID} = useCurrentUserPersonalDetails();

// Derive transaction thread and parent action
const nonDeletedTransactions = getAllNonDeletedTransactions(reportTransactions, reportActions, isOffline, true);
const visibleTransactionsForThreadID = nonDeletedTransactions?.filter((t) => isOffline || t.pendingAction !== 'delete');
const reportTransactionIDs = visibleTransactionsForThreadID?.map((t) => t.transactionID);
const transactionThreadReportID = getOneTransactionThreadReportID(moneyRequestReport, chatReport, reportActions ?? [], isOffline, reportTransactionIDs);
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`);

const requestParentReportAction =
reportActions && transactionThreadReport?.parentReportActionID
? reportActions.find((action): action is OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => action.reportActionID === transactionThreadReport.parentReportActionID)
: null;

const iouTransactionID = isMoneyRequestAction(requestParentReportAction) ? getOriginalMessage(requestParentReportAction)?.IOUTransactionID : undefined;
const {iouTransactionID, requestParentReportAction} = useMoneyReportTransactionThread();
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(iouTransactionID)}`);

useImperativeHandle(ref, () => ({
Expand Down
27 changes: 15 additions & 12 deletions src/components/MoneyReportHeaderModals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import MoneyReportHeaderEducationalModals from './MoneyReportHeaderEducationalMo
import type {MoneyReportHeaderEducationalModalsHandle, RejectModalAction} from './MoneyReportHeaderEducationalModals';
import MoneyReportHeaderModalsContext from './MoneyReportHeaderModalsContext';
import type {HoldMenuParams} from './MoneyReportHeaderModalsContext';
import {MoneyReportTransactionThreadProvider} from './MoneyReportTransactionThreadContext';
import ReportPDFDownloadModal from './ReportPDFDownloadModal';

type MoneyReportHeaderModalsProps = {
Expand Down Expand Up @@ -109,18 +110,20 @@ function MoneyReportHeaderModals({reportID, children}: MoneyReportHeaderModalsPr

return (
<MoneyReportHeaderModalsContext.Provider value={contextValue}>
{children}

<MoneyReportHeaderEducationalModals
ref={educationalModalsRef}
reportID={moneyRequestReport?.reportID}
/>

<ReportPDFDownloadModal
reportID={moneyRequestReport?.reportID}
isVisible={isPDFModalVisible}
onClose={() => setIsPDFModalVisible(false)}
/>
<MoneyReportTransactionThreadProvider reportID={moneyRequestReport?.reportID}>
{children}

<MoneyReportHeaderEducationalModals
ref={educationalModalsRef}
reportID={moneyRequestReport?.reportID}
/>

<ReportPDFDownloadModal
reportID={moneyRequestReport?.reportID}
isVisible={isPDFModalVisible}
onClose={() => setIsPDFModalVisible(false)}
/>
</MoneyReportTransactionThreadProvider>
</MoneyReportHeaderModalsContext.Provider>
);
}
Expand Down
24 changes: 2 additions & 22 deletions src/components/MoneyReportHeaderMoreContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import useMoneyReportHeaderStatusBar from '@hooks/useMoneyReportHeaderStatusBar';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
import useReportTransactionsCollection from '@hooks/useReportTransactionsCollection';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useResponsiveLayoutOnWideRHP from '@hooks/useResponsiveLayoutOnWideRHP';
import useThemeStyles from '@hooks/useThemeStyles';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getAllNonDeletedTransactions} from '@libs/MoneyRequestReportUtils';
import type {PlatformStackRouteProp} from '@libs/Navigation/PlatformStackNavigation/types';
import type {ReportsSplitNavigatorParamList, RightModalNavigatorParamList} from '@libs/Navigation/types';
import {getFilteredReportActionsForReportView, getOneTransactionThreadReportID, getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils';
import {isInvoiceReport as isInvoiceReportUtil} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import MoneyReportHeaderNextStep from './MoneyReportHeaderNextStep';
import MoneyReportHeaderStatusBarSection from './MoneyReportHeaderStatusBarSection';
import {useMoneyReportTransactionThread} from './MoneyReportTransactionThreadContext';
import MoneyRequestReportNavigation from './MoneyRequestReportView/MoneyRequestReportNavigation';

type MoneyReportHeaderMoreContentProps = {
Expand Down Expand Up @@ -76,29 +72,13 @@ type MoneyReportHeaderMoreContentBodyProps = {

function MoneyReportHeaderMoreContentBody({moneyRequestReport, statusBarType, isReportInSearch, shouldShowNextStep}: MoneyReportHeaderMoreContentBodyProps) {
const styles = useThemeStyles();
const {isOffline} = useNetwork();
const {shouldUseNarrowLayout, isMediumScreenWidth} = useResponsiveLayout();
const shouldDisplayNarrowVersion = shouldUseNarrowLayout || isMediumScreenWidth;
const {isWideRHPDisplayedOnWideLayout, isSuperWideRHPDisplayedOnWideLayout} = useResponsiveLayoutOnWideRHP();
const shouldDisplayNarrowMoreButton = !shouldDisplayNarrowVersion || isWideRHPDisplayedOnWideLayout || isSuperWideRHPDisplayedOnWideLayout;

const reportID = moneyRequestReport?.reportID;
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReport?.chatReportID}`);

const {reportActions: unfilteredReportActions} = usePaginatedReportActions(reportID);
const reportActions = getFilteredReportActionsForReportView(unfilteredReportActions);

const allReportTransactions = useReportTransactionsCollection(reportID);
const nonDeletedTransactions = getAllNonDeletedTransactions(allReportTransactions, reportActions, isOffline, true);
const visibleTransactionsForThreadID = nonDeletedTransactions?.filter((t) => isOffline || t.pendingAction !== 'delete');
const reportTransactionIDs = visibleTransactionsForThreadID?.map((t) => t.transactionID);
const transactionThreadReportID = getOneTransactionThreadReportID(moneyRequestReport, chatReport, reportActions ?? [], isOffline, reportTransactionIDs);
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`);

const requestParentReportAction =
reportActions && transactionThreadReport?.parentReportActionID ? reportActions.find((action) => action.reportActionID === transactionThreadReport.parentReportActionID) : null;

const iouTransactionID = isMoneyRequestAction(requestParentReportAction) ? getOriginalMessage(requestParentReportAction)?.IOUTransactionID : undefined;
const {iouTransactionID} = useMoneyReportTransactionThread();

return (
<View style={[styles.flexRow, styles.gap2, styles.justifyContentStart, styles.flexNoWrap, styles.ph5, styles.pb3]}>
Expand Down
64 changes: 64 additions & 0 deletions src/components/MoneyReportTransactionThreadContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, {createContext, useContext} from 'react';
import type {ReactNode} from 'react';
import useOnyx from '@hooks/useOnyx';
import useTransactionThreadReport from '@hooks/useTransactionThreadReport';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils';
import type CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';

type MoneyReportTransactionThreadContextValue = {
/** The transaction ID from the parent IOU report action */
iouTransactionID: string | undefined;
/** The parent IOU report action for the transaction thread */
requestParentReportAction: OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> | null;
/** The transaction thread report ID */
transactionThreadReportID: string | undefined;
/** Filtered report actions for the transaction thread */
reportActions: OnyxTypes.ReportAction[];
Comment thread
TMisiukiewicz marked this conversation as resolved.
Resolved
};

const defaultValue: MoneyReportTransactionThreadContextValue = {
iouTransactionID: undefined,
requestParentReportAction: null,
transactionThreadReportID: undefined,
reportActions: [],
};

const MoneyReportTransactionThreadContext = createContext<MoneyReportTransactionThreadContextValue>(defaultValue);

type MoneyReportTransactionThreadProviderProps = {
/** The money request report ID */
reportID: string | undefined;
/** The children */
children: ReactNode;
Comment thread
TMisiukiewicz marked this conversation as resolved.
Resolved
};

function MoneyReportTransactionThreadProvider({reportID, children}: MoneyReportTransactionThreadProviderProps) {
Comment thread
TMisiukiewicz marked this conversation as resolved.
Resolved
const {transactionThreadReportID, transactionThreadReport, reportActions} = useTransactionThreadReport(reportID);
const [reportActionsForParent] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(reportID)}`);

// Parent lookup uses the raw Onyx collection so Hold/Unhold still work when the parent IOU is
// filtered from the report view (e.g. deleted money request) or outside the paginated window.
const parentReportAction = transactionThreadReport?.parentReportActionID ? reportActionsForParent?.[transactionThreadReport.parentReportActionID] : undefined;
const requestParentReportAction = isMoneyRequestAction(parentReportAction) ? parentReportAction : null;

const iouTransactionID = isMoneyRequestAction(requestParentReportAction) ? getOriginalMessage(requestParentReportAction)?.IOUTransactionID : undefined;

const value: MoneyReportTransactionThreadContextValue = {
iouTransactionID,
requestParentReportAction: requestParentReportAction ?? null,
transactionThreadReportID,
reportActions: reportActions ?? [],
};

return <MoneyReportTransactionThreadContext.Provider value={value}>{children}</MoneyReportTransactionThreadContext.Provider>;
}

function useMoneyReportTransactionThread() {
return useContext(MoneyReportTransactionThreadContext);
}

export default MoneyReportTransactionThreadContext;
export {MoneyReportTransactionThreadProvider, useMoneyReportTransactionThread};
11 changes: 3 additions & 8 deletions src/hooks/useExpenseActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {ValueOf} from 'type-fest';
import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types';
import {ModalActions} from '@components/Modal/Global/ModalContext';
import type {SecondaryActionEntry} from '@components/MoneyReportHeaderActions/types';
import {useMoneyReportTransactionThread} from '@components/MoneyReportTransactionThreadContext';
import {useSearchQueryContext, useSearchSelectionActions} from '@components/Search/SearchContext';
import {duplicateReport as duplicateReportAction, duplicateExpenseTransaction as duplicateTransactionAction} from '@libs/actions/IOU/Duplicate';
import {setupMergeTransactionDataAndNavigate} from '@libs/actions/MergeTransaction';
Expand All @@ -19,7 +20,7 @@ import Log from '@libs/Log';
import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute';
import Navigation from '@libs/Navigation/Navigation';
import {isPolicyAccessible} from '@libs/PolicyUtils';
import {getIOUActionForTransactionID, getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {
canEditFieldOfMoneyRequest,
canUserPerformWriteAction as canUserPerformWriteActionReportUtils,
Expand Down Expand Up @@ -62,7 +63,6 @@ import useReportIsArchived from './useReportIsArchived';
import useTheme from './useTheme';
import useThrottledButtonState from './useThrottledButtonState';
import useTransactionsAndViolationsForReport from './useTransactionsAndViolationsForReport';
import useTransactionThreadReport from './useTransactionThreadReport';
import useTransactionViolations from './useTransactionViolations';

type UseExpenseActionsParams = {
Expand Down Expand Up @@ -96,7 +96,7 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(moneyRequestReport?.policyID)}`);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(moneyRequestReport?.chatReportID)}`);

const {transactionThreadReportID, transactionThreadReport, reportActions} = useTransactionThreadReport(reportID);
const {iouTransactionID, requestParentReportAction, transactionThreadReportID, reportActions} = useMoneyReportTransactionThread();

const {transactions: reportTransactions} = useTransactionsAndViolationsForReport(moneyRequestReport?.reportID);

Expand All @@ -110,11 +110,6 @@ function useExpenseActions({reportID, isReportInSearch = false, backTo, onDuplic
}

const currentTransaction = transactions.at(0);
const requestParentReportAction =
reportActions?.find((action): action is OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => action.reportActionID === transactionThreadReport?.parentReportActionID) ??
null;

const iouTransactionID = isMoneyRequestAction(requestParentReportAction) ? getOriginalMessage(requestParentReportAction)?.IOUTransactionID : undefined;
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(iouTransactionID)}`);
const [originalTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transaction?.comment?.originalTransactionID)}`);
const {iouReport, chatReport: chatIOUReport, isChatIOUReportArchived} = useGetIOUReportFromReportAction(requestParentReportAction);
Expand Down
Loading
Loading