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

fix: mWeb - Chat - Uploading an image while offline briefly shows an offline message in the preview. #43249

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/components/ImageView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as FileUtils from '@libs/fileDownload/FileUtils';
import CONST from '@src/CONST';
import viewRef from '@src/types/utils/viewRef';
import type ImageViewProps from './types';
Expand Down Expand Up @@ -195,6 +196,8 @@ function ImageView({isAuthTokenRequired = false, url, fileName, onError}: ImageV
};
}, [canUseTouchScreen, trackMovement, trackPointerPosition]);

const isLocalFile = FileUtils.isLocalFile(url);

if (canUseTouchScreen) {
return (
<View
Expand All @@ -213,8 +216,8 @@ function ImageView({isAuthTokenRequired = false, url, fileName, onError}: ImageV
onLoad={imageLoad}
onError={onError}
/>
{((isLoading && !isOffline) || (!isLoading && zoomScale === 0)) && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && <AttachmentOfflineIndicator />}
{((isLoading && (!isOffline || isLocalFile)) || (!isLoading && zoomScale === 0)) && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && !isLocalFile && <AttachmentOfflineIndicator />}
</View>
);
}
Expand Down Expand Up @@ -247,8 +250,8 @@ function ImageView({isAuthTokenRequired = false, url, fileName, onError}: ImageV
/>
</PressableWithoutFeedback>

{isLoading && !isOffline && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && <AttachmentOfflineIndicator />}
{isLoading && (!isOffline || isLocalFile) && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && !isLocalFile && <AttachmentOfflineIndicator />}
</View>
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/Lightbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {getCanvasFitScale} from '@components/MultiGestureCanvas/utils';
import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as FileUtils from '@libs/fileDownload/FileUtils';
import NUMBER_OF_CONCURRENT_LIGHTBOXES from './numberOfConcurrentLightboxes';

const cachedImageDimensions = new Map<string, ContentSize | undefined>();
Expand Down Expand Up @@ -197,6 +198,8 @@ function Lightbox({isAuthTokenRequired = false, uri, onScaleChanged: onScaleChan
[onScaleChangedContext, onScaleChangedProp],
);

const isLocalFile = FileUtils.isLocalFile(uri);

return (
<View
style={[StyleSheet.absoluteFill, style]}
Expand Down Expand Up @@ -248,13 +251,13 @@ function Lightbox({isAuthTokenRequired = false, uri, onScaleChanged: onScaleChan
)}

{/* Show activity indicator while the lightbox is still loading the image. */}
{isLoading && !isOffline && (
{isLoading && (!isOffline || isLocalFile) && (
<ActivityIndicator
size="large"
style={StyleSheet.absoluteFill}
/>
)}
{isLoading && <AttachmentOfflineIndicator />}
{isLoading && !isLocalFile && <AttachmentOfflineIndicator />}
</>
)}
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ReceiptUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function getThumbnailAndImageURIs(transaction: OnyxEntry<Transaction>, receiptPa
return {thumbnail: `${path.substring(0, path.length - 4)}.jpg.1024.jpg`, image: path, filename};
}

const isLocalFile = typeof path === 'number' || path.startsWith('blob:') || path.startsWith('file:') || path.startsWith('/');
const isLocalFile = FileUtils.isLocalFile(path);
const {fileExtension} = FileUtils.splitExtensionFromFileName(filename);
return {isThumbnail: true, fileExtension: Object.values(CONST.IOU.FILE_TYPES).find((type) => type === fileExtension), image: path, isLocalFile, filename};
}
Expand Down
8 changes: 8 additions & 0 deletions src/libs/fileDownload/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ function validateImageForCorruption(file: FileObject): Promise<void> {
});
}

function isLocalFile(receiptUri?: string | number): boolean {
if (!receiptUri) {
return false;
}
return typeof receiptUri === 'number' || receiptUri?.startsWith('blob:') || receiptUri?.startsWith('file:') || receiptUri?.startsWith('/');
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure with the type check here @Krishna2323 @arosiclair ?

What are the cases where a URI would be a number ?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@allgandalf, we can leave that as it is. This isn't added in our PR, and it is an optional check, so it won't affect our use case.

Copy link
Contributor

Choose a reason for hiding this comment

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

umm, if @arosiclair approves this then i would have no issue haha..., rest code LGTM!

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC it's a number on some platforms (iOS/Android I think). It should be fine to keep that here.

}

export {
showGeneralErrorAlert,
showSuccessAlert,
Expand All @@ -264,5 +271,6 @@ export {
appendTimeToFileName,
readFileAsync,
base64ToFile,
isLocalFile,
validateImageForCorruption,
};
3 changes: 2 additions & 1 deletion src/pages/iou/request/step/IOURequestStepConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as FileUtils from '@libs/fileDownload/FileUtils';
import getCurrentPosition from '@libs/getCurrentPosition';
import * as IOUUtils from '@libs/IOUUtils';
import Log from '@libs/Log';
Expand Down Expand Up @@ -204,7 +205,7 @@ function IOURequestStepConfirmation({
// skip this in case user is moving the transaction as the receipt path will be valid in that case
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const isLocalFile = typeof receiptPath === 'number' || receiptPath?.startsWith('blob:') || receiptPath?.startsWith('file:') || receiptPath?.startsWith('/');
const isLocalFile = FileUtils.isLocalFile(receiptPath);

if (!isLocalFile) {
setReceiptFile(transaction?.receipt);
Expand Down
Loading