Skip to content

Commit

Permalink
Merge pull request #8928 from allroundexperts/fix-8590
Browse files Browse the repository at this point in the history
Fix resizes on image upload
  • Loading branch information
chiragsalian authored Jun 3, 2022
2 parents fd29904 + 421f084 commit 50da22f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const ImageRenderer = (props) => {
Config.EXPENSIFY.URL_API_ROOT,
);

const imageWidth = htmlAttribs['data-expensify-width'] ? parseInt(htmlAttribs['data-expensify-width'], 10) : undefined;
const imageHeight = htmlAttribs['data-expensify-height'] ? parseInt(htmlAttribs['data-expensify-height'], 10) : undefined;

return (
<AttachmentModal
allowDownload
Expand All @@ -60,6 +63,8 @@ const ImageRenderer = (props) => {
previewSourceURL={previewSource}
style={styles.webViewStyles.tagStyles.img}
isAuthTokenRequired={isAttachment}
imageWidth={imageWidth}
imageHeight={imageHeight}
/>
</PressableWithoutFocus>
)}
Expand Down
38 changes: 33 additions & 5 deletions src/components/ThumbnailImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,45 @@ const propTypes = {
/** Do the urls require an authToken? */
isAuthTokenRequired: PropTypes.bool.isRequired,

/** Width of the thumbnail image */
imageWidth: PropTypes.number,

/** Height of the thumbnail image */
imageHeight: PropTypes.number,

...windowDimensionsPropTypes,
};

const defaultProps = {
style: {},
imageWidth: 200,
imageHeight: 200,
};

class ThumbnailImage extends PureComponent {
constructor(props) {
super(props);

this.updateImageSize = this.updateImageSize.bind(this);

const {thumbnailWidth, thumbnailHeight} = this.calculateThumbnailImageSize(props.imageWidth, props.imageHeight);
this.state = {
thumbnailWidth: 200,
thumbnailHeight: 200,
thumbnailWidth,
thumbnailHeight,
};
}

updateImageSize({width, height}) {
/**
* Compute the thumbnails width and height given original image dimensions.
*
* @param {Number} width - Width of the original image.
* @param {Number} height - Height of the original image.
* @returns {Object} - Object containing thumbnails width and height.
*/
calculateThumbnailImageSize(width, height) {
if (!width || !height) {
return {};
}

// Width of the thumbnail works better as a constant than it does
// a percentage of the screen width since it is relative to each screen
// Note: Clamp minimum width 40px to support touch device
Expand All @@ -54,8 +73,17 @@ class ThumbnailImage extends PureComponent {
} else {
thumbnailScreenHeight = Math.round(thumbnailScreenWidth * aspectRatio);
}
return {thumbnailWidth: thumbnailScreenWidth, thumbnailHeight: Math.max(40, thumbnailScreenHeight)};
}

this.setState({thumbnailWidth: thumbnailScreenWidth, thumbnailHeight: Math.max(40, thumbnailScreenHeight)});
/**
* Update the state with the computed thumbnail sizes.
*
* @param {{ width: number, height: number }} Params - width and height of the original image.
*/
updateImageSize({width, height}) {
const {thumbnailWidth, thumbnailHeight} = this.calculateThumbnailImageSize(width, height);
this.setState({thumbnailWidth, thumbnailHeight});
}

render() {
Expand Down
26 changes: 8 additions & 18 deletions src/pages/home/report/ReportActionItemFragment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {memo} from 'react';
import {ActivityIndicator, ImageBackground, View} from 'react-native';
import {ActivityIndicator, View} from 'react-native';
import PropTypes from 'prop-types';
import Str from 'expensify-common/lib/str';
import reportActionFragmentPropTypes from './reportActionFragmentPropTypes';
Expand Down Expand Up @@ -72,28 +72,18 @@ const ReportActionItemFragment = (props) => {
// If this is an attachment placeholder, return the placeholder component
if (props.isAttachment && props.loading) {
return (
<View style={[styles.chatItemAttachmentPlaceholder]}>
{Str.isImage(props.attachmentInfo.name)
? (
<ImageBackground
source={{uri: props.attachmentInfo.source}}
resizeMode="cover"
imageStyle={[styles.borderBottomRounded, styles.borderTopRounded]}
style={[styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter]}
>
<ActivityIndicator
size="large"
color={themeColors.uploadPreviewActivityIndicator}
/>
</ImageBackground>
) : (
Str.isImage(props.attachmentInfo.name)
? (
<RenderHTML html={`<comment><img src="${props.attachmentInfo.source}"/></comment>`} />
) : (
<View style={[styles.chatItemAttachmentPlaceholder]}>
<ActivityIndicator
size="large"
color={themeColors.textSupporting}
style={[styles.flex1]}
/>
)}
</View>
</View>
)
);
}

Expand Down

0 comments on commit 50da22f

Please sign in to comment.