Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS Migration] Migrate EReceiptThumbnail.js component to TypeScript #32913

Merged
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;
JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
};

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't these consts (and many others in this file) have type declarations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript is automatically inferring it from the the return type of getEReceiptColorStyles function:

image

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
Loading