Skip to content

Commit

Permalink
Merge pull request #32913 from JKobrynski/migrateEReceiptThumbnailToT…
Browse files Browse the repository at this point in the history
…ypeScript

[TS Migration] Migrate EReceiptThumbnail.js component to TypeScript
  • Loading branch information
tgolen authored Jan 9, 2024
2 parents 8925a89 + 3f67d65 commit 108b5e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import PropTypes from 'prop-types';
import React, {useMemo, useState} from 'react';
import type {LayoutChangeEvent} from 'react-native';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ReportUtils from '@libs/ReportUtils';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Transaction} from '@src/types/onyx';
import Icon from './Icon';
import * as eReceiptBGs from './Icon/EReceiptBGs';
import * as Expensicons from './Icon/Expensicons';
import * as MCCIcons from './Icon/MCCIcons';
import Image from './Image';
import transactionPropTypes from './transactionPropTypes';

const propTypes = {
/* TransactionID of the transaction this EReceipt corresponds to */
// eslint-disable-next-line react/no-unused-prop-types
transactionID: PropTypes.string.isRequired,

/* Onyx Props */
transaction: transactionPropTypes,
type EReceiptThumbnailOnyxProps = {
transaction: OnyxEntry<Transaction>;
};

const defaultProps = {
transaction: {},
type EReceiptThumbnailProps = EReceiptThumbnailOnyxProps & {
/** TransactionID of the transaction this EReceipt corresponds to. It's used by withOnyx HOC */
// eslint-disable-next-line react/no-unused-prop-types
transactionID: string;
};

const backgroundImages = {
Expand All @@ -37,30 +35,35 @@ const backgroundImages = {
[CONST.ERECEIPT_COLORS.PINK]: eReceiptBGs.EReceiptBG_Pink,
};

function EReceiptThumbnail({transaction}) {
function EReceiptThumbnail({transaction}: EReceiptThumbnailProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
// Get receipt colorway, or default to Yellow.
const {backgroundColor: primaryColor, color: secondaryColor} = StyleUtils.getEReceiptColorStyles(StyleUtils.getEReceiptColorCode(transaction));

const [containerWidth, setContainerWidth] = useState(0);
const [containerHeight, setContainerHeight] = useState(0);

const onContainerLayout = (event) => {
const backgroundImage = useMemo(() => backgroundImages[StyleUtils.getEReceiptColorCode(transaction)], [StyleUtils, transaction]);

const colorStyles = StyleUtils.getEReceiptColorStyles(StyleUtils.getEReceiptColorCode(transaction));
const primaryColor = colorStyles?.backgroundColor;
const secondaryColor = colorStyles?.color;

const onContainerLayout = (event: LayoutChangeEvent) => {
const {width, height} = event.nativeEvent.layout;
setContainerWidth(width);
setContainerHeight(height);
};

const {mccGroup: transactionMCCGroup} = ReportUtils.getTransactionDetails(transaction);
const MCCIcon = MCCIcons[`${transactionMCCGroup}`];
const transactionDetails = ReportUtils.getTransactionDetails(transaction);
const transactionMCCGroup = transactionDetails?.mccGroup;
const MCCIcon = transactionMCCGroup ? MCCIcons[`${transactionMCCGroup}`] : undefined;

const isSmall = containerWidth && containerWidth < variables.eReceiptThumbnailSmallBreakpoint;
const isMedium = containerWidth && containerWidth < variables.eReceiptThumbnailMediumBreakpoint;

let receiptIconWidth = variables.eReceiptIconWidth;
let receiptIconHeight = variables.eReceiptIconHeight;
let receiptMCCSize = variables.eReceiptMCCHeightWidth;
let receiptIconWidth: number = variables.eReceiptIconWidth;
let receiptIconHeight: number = variables.eReceiptIconHeight;
let receiptMCCSize: number = variables.eReceiptMCCHeightWidth;

if (isSmall) {
receiptIconWidth = variables.eReceiptIconWidthSmall;
Expand All @@ -72,13 +75,11 @@ function EReceiptThumbnail({transaction}) {
receiptMCCSize = variables.eReceiptMCCHeightWidthMedium;
}

const backgroundImage = useMemo(() => backgroundImages[StyleUtils.getEReceiptColorCode(transaction)], [StyleUtils, transaction]);

return (
<View
style={[
styles.flex1,
StyleUtils.getBackgroundColorStyle(primaryColor),
primaryColor ? StyleUtils.getBackgroundColorStyle(primaryColor) : {},
styles.overflowHidden,
styles.alignItemsCenter,
containerHeight && containerHeight < variables.eReceiptThumnailCenterReceiptBreakpoint ? styles.justifyContentCenter : {},
Expand Down Expand Up @@ -114,10 +115,8 @@ function EReceiptThumbnail({transaction}) {
}

EReceiptThumbnail.displayName = 'EReceiptThumbnail';
EReceiptThumbnail.propTypes = propTypes;
EReceiptThumbnail.defaultProps = defaultProps;

export default withOnyx({
export default withOnyx<EReceiptThumbnailProps, EReceiptThumbnailOnyxProps>({
transaction: {
key: ({transactionID}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
},
Expand Down
7 changes: 4 additions & 3 deletions src/styles/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {Animated, DimensionValue, ImageStyle, PressableStateCallbackType, StyleProp, TextStyle, ViewStyle} from 'react-native';
import {StyleSheet} from 'react-native';
import type {Animated, DimensionValue, ImageStyle, PressableStateCallbackType, StyleProp, TextStyle, ViewStyle} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import type {EdgeInsets} from 'react-native-safe-area-context';
import type {ValueOf} from 'type-fest';
import * as Browser from '@libs/Browser';
Expand Down Expand Up @@ -275,9 +276,9 @@ function getDefaultWorkspaceAvatarColor(workspaceName: string): ViewStyle {
/**
* Helper method to return eReceipt color code
*/
function getEReceiptColorCode(transaction: Transaction): EReceiptColorName {
function getEReceiptColorCode(transaction: OnyxEntry<Transaction>): EReceiptColorName {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const transactionID = transaction.parentTransactionID || transaction.transactionID || '';
const transactionID = transaction?.parentTransactionID || transaction?.transactionID || '';

const colorHash = UserUtils.hashText(transactionID.trim(), eReceiptColors.length);

Expand Down

0 comments on commit 108b5e0

Please sign in to comment.