Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React from 'react';
import type {AttachmentViewProps} from '..';

type AttachmentViewImageProps = Pick<AttachmentViewProps, 'attachmentID' | 'isAuthTokenRequired' | 'file' | 'onPress'> & {
url: string;
url: string | number;

loadComplete: boolean;

Expand Down
9 changes: 5 additions & 4 deletions src/components/Attachments/AttachmentView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ function AttachmentView({
// will appear with a source that is a blob
const isSourcePDF = typeof source === 'string' && Str.isPDF(source);
const isFilePDF = file && Str.isPDF(file.name ?? translate('attachmentView.unknownFilename'));
if (!hasPDFFailedToLoad && !isUploading && (isSourcePDF || isFilePDF)) {
if (!hasPDFFailedToLoad && !isUploading && typeof source === 'string' && (isSourcePDF || isFilePDF)) {
// Every mounted PDF viewer is a full PDF.js document parse (its own worker + parsed document), so in a
// carousel the memory cost scales with the number of PDF attachments — enough to OOM the WebContent
// process on iOS Safari and reload the tab when several PDFs are added at once. Only mount the viewer
Expand All @@ -274,7 +274,7 @@ function AttachmentView({
/>
);
}
const encryptedSourceUrl = isAuthTokenRequired ? addEncryptedAuthTokenToURL(source as string, encryptedAuthToken) : (source as string);
const encryptedSourceUrl = isAuthTokenRequired ? addEncryptedAuthTokenToURL(source, encryptedAuthToken) : source;

const onPDFLoadComplete = (path: string) => {
const id = transaction?.transactionID ?? reportActionID;
Expand Down Expand Up @@ -323,7 +323,8 @@ function AttachmentView({

const isImage = isFileImage || (!file?.name && isLocalSourceImage);

if (isImage) {
// ImageView renders URLs and static image assets; other image sources cannot be shown there.
if (isImage && (typeof source === 'string' || typeof source === 'number')) {
if (imageError && (typeof fallbackSource === 'number' || typeof fallbackSource === 'function')) {
return (
<View style={[styles.flexColumn, styles.alignItemsCenter, styles.justifyContentCenter]}>
Expand Down Expand Up @@ -352,7 +353,7 @@ function AttachmentView({
);
}

let imageSource = imageError && fallbackSource ? (fallbackSource as string) : (cachedSource ?? (source as string));
let imageSource = imageError && typeof fallbackSource === 'string' && fallbackSource ? fallbackSource : (cachedSource ?? source);

if (isHighResolution) {
if (!isUploaded) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ImageView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function ImageView({isAuthTokenRequired = false, url, fileName, onError}: ImageV
{/* eslint-disable-next-line react-native-a11y/has-valid-accessibility-ignores-invert-colors -- Custom Image wrapper does not support this prop. */}
<Image
key={reloadKey}
source={{uri: url}}
source={typeof url === 'string' ? {uri: url} : url}
isAuthTokenRequired={isAuthTokenRequired}
style={[styles.h100, styles.w100]}
resizeMode={RESIZE_MODES.contain}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ImageView/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type ImageViewProps = Pick<Attachment, 'attachmentID'> & {
/** Whether source url requires authentication */
isAuthTokenRequired?: boolean;

/** URL to full-sized image */
url: string;
/** URL to full-sized image, or a static image asset on native */
url: string | number;

fileName: string;

Expand Down
10 changes: 5 additions & 5 deletions src/components/Lightbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const FALLBACK_OFFSET = 2;

const LOADING_CONTEXT = {context: 'Lightbox'};

const cachedImageDimensions = new Map<string, Dimensions | undefined>();
const cachedImageDimensions = new Map<string | number, Dimensions | undefined>();

function getImagePriority(isActive: boolean, isLightboxVisible: boolean) {
if (isActive) {
Expand All @@ -48,8 +48,8 @@ type LightboxProps = Pick<Attachment, 'attachmentID'> & {
/** Whether source url requires authentication */
isAuthTokenRequired?: boolean;

/** URI to full-sized attachment */
uri: string;
/** URI to full-sized attachment, or a static image asset on native */
uri: string | number;

/** Triggers whenever the zoom scale changes */
onScaleChanged?: OnScaleChangedCallback;
Expand Down Expand Up @@ -263,7 +263,7 @@ function Lightbox({attachmentID, isAuthTokenRequired = false, uri, onScaleChange
{/* eslint-disable-next-line react-native-a11y/has-valid-accessibility-ignores-invert-colors -- Custom Image wrapper does not support this prop. */}
<Image
key={reloadKey}
source={{uri}}
source={typeof uri === 'string' ? {uri} : uri}
style={[contentSize ?? styles.invisibleImage]}
isAuthTokenRequired={isAuthTokenRequired}
priority={imagePriority}
Expand Down Expand Up @@ -296,7 +296,7 @@ function Lightbox({attachmentID, isAuthTokenRequired = false, uri, onScaleChange
<View style={StyleUtils.getFullscreenCenteredContentStyles()}>
{/* eslint-disable-next-line react-native-a11y/has-valid-accessibility-ignores-invert-colors -- Custom Image wrapper does not support this prop. */}
<Image
source={{uri}}
source={typeof uri === 'string' ? {uri} : uri}
resizeMode="contain"
style={[fallbackSize ?? styles.invisibleImage]}
isAuthTokenRequired={isAuthTokenRequired}
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/ImageViewSourceTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {render} from '@testing-library/react-native';

import type {ImageProps} from '@components/Image/types';

import React from 'react';

import type * as WebImageViewModule from '../../src/components/ImageView/index';

const mockImageProps = jest.fn<void, [ImageProps]>();

jest.mock('@components/Image', () => (props: ImageProps) => {
mockImageProps(props);
return null;
});
jest.mock('@libs/DeviceCapabilities', () => ({canUseTouchScreen: () => false}));

// Jest prefers .native files, so load the web implementation by its file name.
const ImageView = jest.requireActual<typeof WebImageViewModule>('../../src/components/ImageView/index.tsx').default;

describe('ImageView source', () => {
beforeEach(() => {
mockImageProps.mockClear();
});

it('passes a static image asset to the image unchanged', () => {
// Given a static image asset, which React Native represents as a number
const staticImage = 42;

// When the image view renders it
render(
<ImageView
url={staticImage}
fileName="receipt.png"
/>,
);

// Then the image gets the asset itself, because wrapping it as {uri: 42} cannot load
expect(mockImageProps).toHaveBeenLastCalledWith(expect.objectContaining({source: staticImage}));
});

it('wraps a URL in a uri source', () => {
// Given a remote image URL
const url = 'https://www.expensify.com/receipt.png';

// When the image view renders it
render(
<ImageView
url={url}
fileName="receipt.png"
/>,
);

// Then the image gets a uri source, as before
expect(mockImageProps).toHaveBeenLastCalledWith(expect.objectContaining({source: {uri: url}}));
});
});
Loading