|
| 1 | +import React, {useRef} from 'react'; |
| 2 | +import {ScrollView, View} from 'react-native'; |
| 3 | +import type {ImageSourcePropType} from 'react-native'; |
| 4 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 5 | +import expensifyLogo from '@assets/images/expensify-logo-round-transparent.png'; |
| 6 | +import ContextMenuItem from '@components/ContextMenuItem'; |
| 7 | +import HeaderWithBackButton from '@components/HeaderWithBackButton'; |
| 8 | +import * as Expensicons from '@components/Icon/Expensicons'; |
| 9 | +import MenuItem from '@components/MenuItem'; |
| 10 | +import QRShareWithDownload from '@components/QRShare/QRShareWithDownload'; |
| 11 | +import type QRShareWithDownloadHandle from '@components/QRShare/QRShareWithDownload/types'; |
| 12 | +import ScreenWrapper from '@components/ScreenWrapper'; |
| 13 | +import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails'; |
| 14 | +import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails'; |
| 15 | +import useEnvironment from '@hooks/useEnvironment'; |
| 16 | +import useLocalize from '@hooks/useLocalize'; |
| 17 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 18 | +import useWindowDimensions from '@hooks/useWindowDimensions'; |
| 19 | +import Clipboard from '@libs/Clipboard'; |
| 20 | +import getPlatform from '@libs/getPlatform'; |
| 21 | +import Navigation from '@libs/Navigation/Navigation'; |
| 22 | +import * as ReportUtils from '@libs/ReportUtils'; |
| 23 | +import * as Url from '@libs/Url'; |
| 24 | +import * as UserUtils from '@libs/UserUtils'; |
| 25 | +import CONST from '@src/CONST'; |
| 26 | +import ROUTES from '@src/ROUTES'; |
| 27 | +import type {Report, Session} from '@src/types/onyx'; |
| 28 | + |
| 29 | +type ShareCodePageOnyxProps = WithCurrentUserPersonalDetailsProps & { |
| 30 | + /** Session info for the currently logged in user. */ |
| 31 | + session: OnyxEntry<Session>; |
| 32 | + |
| 33 | + /** The report currently being looked at */ |
| 34 | + report?: OnyxEntry<Report>; |
| 35 | +}; |
| 36 | + |
| 37 | +type ShareCodePageProps = ShareCodePageOnyxProps; |
| 38 | + |
| 39 | +function ShareCodePage({report, session, currentUserPersonalDetails}: ShareCodePageProps) { |
| 40 | + const themeStyles = useThemeStyles(); |
| 41 | + const {translate} = useLocalize(); |
| 42 | + const {environmentURL} = useEnvironment(); |
| 43 | + const qrCodeRef = useRef<QRShareWithDownloadHandle>(null); |
| 44 | + const {isSmallScreenWidth} = useWindowDimensions(); |
| 45 | + |
| 46 | + const isReport = !!report?.reportID; |
| 47 | + |
| 48 | + const getSubtitle = () => { |
| 49 | + if (isReport) { |
| 50 | + if (ReportUtils.isExpenseReport(report)) { |
| 51 | + return ReportUtils.getPolicyName(report); |
| 52 | + } |
| 53 | + if (ReportUtils.isMoneyRequestReport(report)) { |
| 54 | + // generate subtitle from participants |
| 55 | + return ReportUtils.getVisibleMemberIDs(report) |
| 56 | + .map((accountID) => ReportUtils.getDisplayNameForParticipant(accountID)) |
| 57 | + .join(' & '); |
| 58 | + } |
| 59 | + |
| 60 | + return ReportUtils.getParentNavigationSubtitle(report).workspaceName ?? ReportUtils.getChatRoomSubtitle(report); |
| 61 | + } |
| 62 | + |
| 63 | + return session?.email; |
| 64 | + }; |
| 65 | + |
| 66 | + const title = isReport ? ReportUtils.getReportName(report) : currentUserPersonalDetails.displayName ?? ''; |
| 67 | + const subtitle = getSubtitle(); |
| 68 | + const urlWithTrailingSlash = Url.addTrailingForwardSlash(environmentURL); |
| 69 | + const url = isReport ? `${urlWithTrailingSlash}${ROUTES.REPORT_WITH_ID.getRoute(report.reportID)}` : `${urlWithTrailingSlash}${ROUTES.PROFILE.getRoute(session?.accountID ?? '')}`; |
| 70 | + const platform = getPlatform(); |
| 71 | + const isNative = platform === CONST.PLATFORM.IOS || platform === CONST.PLATFORM.ANDROID; |
| 72 | + |
| 73 | + return ( |
| 74 | + <ScreenWrapper |
| 75 | + testID={ShareCodePage.displayName} |
| 76 | + shouldShowOfflineIndicatorInWideScreen={!isReport} |
| 77 | + > |
| 78 | + <HeaderWithBackButton |
| 79 | + title={translate('common.shareCode')} |
| 80 | + onBackButtonPress={() => Navigation.goBack(isReport ? ROUTES.REPORT_WITH_ID_DETAILS.getRoute(report.reportID) : ROUTES.SETTINGS)} |
| 81 | + shouldShowBackButton={isReport || isSmallScreenWidth} |
| 82 | + /> |
| 83 | + <ScrollView style={[themeStyles.flex1, themeStyles.mt3]}> |
| 84 | + <View style={[isSmallScreenWidth ? themeStyles.workspaceSectionMobile : themeStyles.workspaceSection, themeStyles.ph4]}> |
| 85 | + <QRShareWithDownload |
| 86 | + ref={qrCodeRef} |
| 87 | + url={url} |
| 88 | + title={title} |
| 89 | + subtitle={subtitle} |
| 90 | + logo={isReport ? expensifyLogo : (UserUtils.getAvatarUrl(currentUserPersonalDetails?.avatar, currentUserPersonalDetails?.accountID) as ImageSourcePropType)} |
| 91 | + logoRatio={isReport ? CONST.QR.EXPENSIFY_LOGO_SIZE_RATIO : CONST.QR.DEFAULT_LOGO_SIZE_RATIO} |
| 92 | + logoMarginRatio={isReport ? CONST.QR.EXPENSIFY_LOGO_MARGIN_RATIO : CONST.QR.DEFAULT_LOGO_MARGIN_RATIO} |
| 93 | + /> |
| 94 | + </View> |
| 95 | + |
| 96 | + <View style={{marginTop: 36}}> |
| 97 | + <ContextMenuItem |
| 98 | + isAnonymousAction |
| 99 | + text={translate('qrCodes.copy')} |
| 100 | + icon={Expensicons.Copy} |
| 101 | + successIcon={Expensicons.Checkmark} |
| 102 | + successText={translate('qrCodes.copied')} |
| 103 | + onPress={() => Clipboard.setString(url)} |
| 104 | + shouldLimitWidth={false} |
| 105 | + /> |
| 106 | + |
| 107 | + {isNative && ( |
| 108 | + <MenuItem |
| 109 | + isAnonymousAction |
| 110 | + title={translate('common.download')} |
| 111 | + icon={Expensicons.Download} |
| 112 | + onPress={qrCodeRef.current?.download} |
| 113 | + /> |
| 114 | + )} |
| 115 | + |
| 116 | + <MenuItem |
| 117 | + title={translate(`referralProgram.${CONST.REFERRAL_PROGRAM.CONTENT_TYPES.SHARE_CODE}.buttonText1`)} |
| 118 | + icon={Expensicons.Cash} |
| 119 | + onPress={() => Navigation.navigate(ROUTES.REFERRAL_DETAILS_MODAL.getRoute(CONST.REFERRAL_PROGRAM.CONTENT_TYPES.SHARE_CODE))} |
| 120 | + /> |
| 121 | + </View> |
| 122 | + </ScrollView> |
| 123 | + </ScreenWrapper> |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +ShareCodePage.displayName = 'ShareCodePage'; |
| 128 | + |
| 129 | +export default withCurrentUserPersonalDetails(ShareCodePage); |
0 commit comments