|
| 1 | +import {personalDetailByAccountIDSelector} from '@selectors/PersonalDetails'; |
| 2 | +import React from 'react'; |
| 3 | +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; |
| 4 | +import OfflineWithFeedback from '@components/OfflineWithFeedback'; |
| 5 | +import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; |
| 6 | +import useNetwork from '@hooks/useNetwork'; |
| 7 | +import useOnyx from '@hooks/useOnyx'; |
| 8 | +import useResponsiveLayout from '@hooks/useResponsiveLayout'; |
| 9 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 10 | +import {isTripPreview} from '@libs/ReportActionsUtils'; |
| 11 | +import {canCurrentUserOpenReport, canUserPerformWriteAction as canUserPerformWriteActionReportUtils, isArchivedReport, navigateToLinkedReportAction} from '@libs/ReportUtils'; |
| 12 | +import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report'; |
| 13 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 14 | +import type {Beta, IntroSelected, PersonalDetails, Report, ReportAction, ReportNameValuePairs} from '@src/types/onyx'; |
| 15 | +import type {Errors} from '@src/types/onyx/OnyxCommon'; |
| 16 | +import ReportActionItem from './ReportActionItem'; |
| 17 | +import ThreadDivider from './ThreadDivider'; |
| 18 | + |
| 19 | +type AncestorReportActionItemProps = { |
| 20 | + /** Report for this action */ |
| 21 | + report: Report; |
| 22 | + |
| 23 | + /** All the data of the action item */ |
| 24 | + reportAction: ReportAction; |
| 25 | + |
| 26 | + /** Should we display the new marker on top of the comment? */ |
| 27 | + shouldDisplayNewMarker: boolean; |
| 28 | + |
| 29 | + /** Report name value pairs for the ancestor reports */ |
| 30 | + reportNameValuePairs: OnyxCollection<ReportNameValuePairs>; |
| 31 | + |
| 32 | + /** Beta features list */ |
| 33 | + allBetas: OnyxEntry<Beta[]>; |
| 34 | + |
| 35 | + /** Concierge personal details */ |
| 36 | + conciergePersonalDetail: OnyxEntry<PersonalDetails>; |
| 37 | + |
| 38 | + /** The user's Concierge reportID */ |
| 39 | + conciergeReportID: string | undefined; |
| 40 | + |
| 41 | + /** Account ID of the current user */ |
| 42 | + currentUserAccountID: number; |
| 43 | + |
| 44 | + /** Model of onboarding */ |
| 45 | + introSelected: OnyxEntry<IntroSelected>; |
| 46 | + |
| 47 | + /** If this is the first visible report action */ |
| 48 | + isFirstVisibleReportAction: boolean; |
| 49 | + |
| 50 | + /** Whether the current report is archived */ |
| 51 | + isReportArchived: boolean; |
| 52 | + |
| 53 | + /** Whether the user has viewed the self-guided tour */ |
| 54 | + isSelfTourViewed: boolean | undefined; |
| 55 | + |
| 56 | + /** Linked transaction route error */ |
| 57 | + linkedTransactionRouteError: Errors | undefined; |
| 58 | + |
| 59 | + /** Report action belonging to the report's parent */ |
| 60 | + parentReportAction: OnyxEntry<ReportAction>; |
| 61 | + |
| 62 | + /** If the thread divider line will be used */ |
| 63 | + shouldUseThreadDividerLine: boolean; |
| 64 | + |
| 65 | + /** The transaction thread report associated with the current report, if any */ |
| 66 | + transactionThreadReport: OnyxEntry<Report>; |
| 67 | +}; |
| 68 | + |
| 69 | +function AncestorReportActionItem({ |
| 70 | + report, |
| 71 | + reportAction, |
| 72 | + shouldDisplayNewMarker, |
| 73 | + reportNameValuePairs, |
| 74 | + allBetas, |
| 75 | + conciergePersonalDetail, |
| 76 | + conciergeReportID, |
| 77 | + currentUserAccountID, |
| 78 | + introSelected, |
| 79 | + isFirstVisibleReportAction, |
| 80 | + isReportArchived, |
| 81 | + isSelfTourViewed, |
| 82 | + linkedTransactionRouteError, |
| 83 | + parentReportAction, |
| 84 | + shouldUseThreadDividerLine, |
| 85 | + transactionThreadReport, |
| 86 | +}: AncestorReportActionItemProps) { |
| 87 | + const styles = useThemeStyles(); |
| 88 | + const currentUserPersonalDetail = useCurrentUserPersonalDetails(); |
| 89 | + const [reportOwnerPersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailByAccountIDSelector(report?.ownerAccountID)}); |
| 90 | + |
| 91 | + const shouldDisplayThreadDivider = !isTripPreview(reportAction); |
| 92 | + const isAncestorReportArchived = isArchivedReport(reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`]); |
| 93 | + const canOpenAncestorReport = canCurrentUserOpenReport(report, allBetas, isAncestorReportArchived); |
| 94 | + |
| 95 | + const {isOffline} = useNetwork(); |
| 96 | + const {isInNarrowPaneModal} = useResponsiveLayout(); |
| 97 | + |
| 98 | + const openLinkedAncestorReport = (isArchived: boolean) => { |
| 99 | + navigateToLinkedReportAction( |
| 100 | + { |
| 101 | + report, |
| 102 | + reportAction, |
| 103 | + shouldDisplayNewMarker, |
| 104 | + }, |
| 105 | + isInNarrowPaneModal, |
| 106 | + canUserPerformWriteActionReportUtils(report, isArchived), |
| 107 | + isOffline, |
| 108 | + ); |
| 109 | + }; |
| 110 | + |
| 111 | + const openAncestorReport = () => { |
| 112 | + openLinkedAncestorReport(isReportArchived); |
| 113 | + }; |
| 114 | + |
| 115 | + const openAncestorReportFromThreadDivider = () => { |
| 116 | + openLinkedAncestorReport(isAncestorReportArchived); |
| 117 | + }; |
| 118 | + |
| 119 | + const deleteAncestorReportAndNavigateToConcierge = () => { |
| 120 | + navigateToConciergeChatAndDeleteReport( |
| 121 | + report?.reportID, |
| 122 | + conciergeReportID, |
| 123 | + currentUserAccountID, |
| 124 | + introSelected, |
| 125 | + isSelfTourViewed, |
| 126 | + allBetas, |
| 127 | + reportOwnerPersonalDetail, |
| 128 | + currentUserPersonalDetail, |
| 129 | + conciergePersonalDetail, |
| 130 | + ); |
| 131 | + }; |
| 132 | + |
| 133 | + return ( |
| 134 | + <OfflineWithFeedback |
| 135 | + shouldDisableOpacity={!!reportAction?.pendingAction} |
| 136 | + pendingAction={report?.pendingFields?.addWorkspaceRoom ?? report?.pendingFields?.createChat} |
| 137 | + errors={report?.errorFields?.addWorkspaceRoom ?? report?.errorFields?.createChat} |
| 138 | + errorRowStyles={[styles.ml10, styles.mr2]} |
| 139 | + onClose={deleteAncestorReportAndNavigateToConcierge} |
| 140 | + > |
| 141 | + {shouldDisplayThreadDivider && ( |
| 142 | + <ThreadDivider |
| 143 | + shouldDisplayNewMarker={shouldDisplayNewMarker} |
| 144 | + onPress={canOpenAncestorReport ? openAncestorReportFromThreadDivider : undefined} |
| 145 | + /> |
| 146 | + )} |
| 147 | + <ReportActionItem |
| 148 | + report={report} |
| 149 | + action={reportAction} |
| 150 | + onPress={canOpenAncestorReport ? openAncestorReport : undefined} |
| 151 | + parentReportAction={parentReportAction} |
| 152 | + transactionThreadReport={transactionThreadReport} |
| 153 | + displayAsGroup={false} |
| 154 | + shouldDisplayNewMarker={shouldDisplayNewMarker} |
| 155 | + isFirstVisibleReportAction={isFirstVisibleReportAction} |
| 156 | + shouldUseThreadDividerLine={shouldUseThreadDividerLine} |
| 157 | + isThreadReportParentAction |
| 158 | + linkedTransactionRouteError={linkedTransactionRouteError} |
| 159 | + /> |
| 160 | + </OfflineWithFeedback> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +export default AncestorReportActionItem; |
0 commit comments