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

Image Web/Desktop: Add support for http headers #34505

Merged

Conversation

kidroca
Copy link
Contributor

@kidroca kidroca commented Jan 15, 2024

Details

This PR refactors the Image component, aligning the web/desktop and native implementations for better consistency and maintainability.

Changes:

  1. Introduced BaseImage Component:

    • File Location: src/components/Image/BaseImage.tsx
    • This new component acts as a bridge between the native and web implementations.
    • For the onLoad event, it ensures that both web and native platforms have the same signature.
  2. Native Implementation Update:

    • File Location: src/components/Image/BaseImage.native.tsx
    • The native side uses expo-image, with a slight adjustment for the onLoad prop, to make it's interface uniform.
  3. Unified Image Component for Web/Desktop and Native:

    • File Location: src/components/Image/index.js
    • The main Image component has been refactored to use the new BaseImage.
    • This change streamlines the authToken inclusion process in the image source, ensuring it works seamlessly across both platforms.
  4. Removed Redundant Native Specific Implementation:

    • File Location: src/components/Image/index.native.js
    • With the new unified approach, the native-specific implementation has become redundant and has been removed.
  5. Patch for react-native-web

    • File Location: patches/react-native-web+0.19.9+005+image-header-support.patch
    • Implemented a custom patch for the official react-native-web version 0.19.9, incorporating changes from PR #2442. This patch adds support for Image with HTTP headers, which was previously missing in our current version.

Fixed Issues

$ #12603
PROPOSAL: N/A

Tests

Verify the Image component works on all platforms

  1. Launch the application and navigate to a report containing multiple attachments.
  2. Scroll through the report, ensuring that all attachments are loading successfully without any errors or delay.
  3. Click on an attachment to open its preview.
    • Confirm that the preview displays the image correctly.
    • Ensure the download button is present and functional; downloading the image should work as expected.
  4. Open your browser's developer tools or the equivalent network monitoring tool for the platform you are testing on.
    • Monitor the network requests for image attachments.
    • Verify that the requests contain the X-Chat-Attachment-Token header.
  5. Test the image caching functionality:
    • Keep the application open for more than 2 hours.
    • Observe that the images remain loaded and are not re-fetched.
    • Logout from the application and then log back in.
    • Navigate to the same report with attachments.
    • Images that were previously loaded should display instantly, indicating they are loaded from the cache.
    • Confirm that the behavior remains consistent even if the X-Chat-Attachment-Token value changes.
  6. Verify Attachment Opening for Scanned Receipts in Payment Requests
    • Create a payment request using "Request money"
    • Scan or upload a receipt during the request process.
    • Once the receipt is uploaded and the request is sent, attempt to open the receipt by tapping on the receipt image.
    • Confirm that the receipt opens correctly in the attachment view.

Offline tests

  1. Make sure you have viewed some reports with attachments while online.
  2. Disconnect from the internet or turn on airplane mode to simulate an offline state.
  3. Navigate to a report with attachments that were previously loaded.
    • Verify that these images still render correctly, even without an internet connection.
  4. Navigate to a report with attachments that were NOT previously loaded.
    • The attachments should appear in an infinite loading state, indicating they cannot be fetched without an internet connection.

QA Steps

  1. Open the staging environment of the application.
  2. Navigate to various reports that contain different types of attachments.
  3. Ensure that all attachments load successfully without any visual artifacts or errors.
  4. Click on any attachment to initiate the attachment preview.
    • Confirm that the preview opens, displaying the attachment correctly.
    • Locate the download button and click on it. Verify that the image is downloaded to your device or computer as expected.
  5. Verify Attachment Opening for Scanned Receipts in Payment Requests
    • Create a payment request using "Request money"
    • Scan or upload a receipt during the request process.
    • Once the receipt is uploaded and the request is sent, attempt to open the receipt by tapping on the receipt image.
    • Confirm that the receipt opens correctly in the attachment view.

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native

image

Android: mWeb Chrome

image

iOS: Native Screenshot 2024-01-15 at 16 32 43
iOS: mWeb Safari Screenshot 2024-01-15 at 16 48 58
MacOS: Chrome / Safari Screenshot 2024-01-15 at 21 17 56
MacOS: Desktop Screenshot 2024-01-15 at 16 53 34

- Introduced `BaseImage` component that branches between native and web implementations.
    - **Native**: Utilizes `FastImage` directly.
    - **Web**: Minor adjustments made to the `onLoad` event signature for compatibility.
- Eliminated `Image/index.native.js` as both native and web components now leverage a unified high-level implementation for image loading and rendering.

(cherry picked from commit 2aa37d2)
This patch focuses on resolving issues encountered with avatar image loading, specifically addressing the challenges related to CORS (Cross-Origin Resource Sharing) errors.

Changes:
- Removed the `isAuthTokenRequired` flag from the `AttachmentModal` component in various files, including `ProfilePage.js`, `RoomHeaderAvatars.js`, and `DetailsPage.js`. This change is crucial for loading of avatar images that are hosted externally.

Rationale:
- The primary purpose of this modification is to streamline the loading process for avatars by removing the unnecessary inclusion of authentication tokens in requests for external images. This approach aligns with standard practices for handling externally hosted content and aims to enhance compatibility and performance.
- Raised a question here as whether there are cases of avatar images that need authentication: https://github.com/Expensify/App/pull/24425/files#r1404352872

This update is expected to resolve the CORS errors associated with avatar image loading, thereby improving the overall functionality and user experience in our application.

(cherry picked from commit cc49208)
- Adapt to `expo-image` deprecation of `event.nativeEvent` usage.
- Directly use the event object as recommended.
- Ensure compatibility with components using the `onLoad` prop.
@kidroca kidroca requested a review from a team as a code owner January 15, 2024 14:57
@melvin-bot melvin-bot bot requested review from 0xmiros and removed request for a team January 15, 2024 14:57
Copy link

melvin-bot bot commented Jan 15, 2024

@0xmiroslav Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@0xmiros
Copy link
Contributor

0xmiros commented Jan 15, 2024

Is this exact same PR as #13036?

All new files should be written in TS. Let's do that

@kidroca
Copy link
Contributor Author

kidroca commented Jan 15, 2024

@0xmiroslav

Is this exact same PR as #13036?

It has slight changes, so it's not exact.
In the past we used react-native-fast-image, now that we've switched to expo-image I had to make some small adjustments in BaseImage.native to handle the onLoad prop. The rest is exactly as in #13036

All new files should be written in TS. Let's do that

Allright

@0xmiros
Copy link
Contributor

0xmiros commented Jan 17, 2024

Author checklist is failing

@kidroca
Copy link
Contributor Author

kidroca commented Jan 17, 2024

Author checklist is failing

I had a little bit of trouble running the Android build, but resolved it today and added screenshots.

✔️ Ready for review

@Beamanator Beamanator self-requested a review January 19, 2024 11:46
@0xmiros
Copy link
Contributor

0xmiros commented Jan 19, 2024

@Beamanator can you please generate adhoc build?
Going to test both localhost and PR domain

@0xmiros
Copy link
Contributor

0xmiros commented Jan 19, 2024

@Beamanator this PR will conflict with #31296. Should we hold this or continue?

@Beamanator
Copy link
Contributor

@0xmiroslav good idea, I just triggered that build

@Beamanator
Copy link
Contributor

@Beamanator this PR will conflict with #31296. Should we hold this or continue?

Good call, but I think that's fine - whichever gets merged first won't have to deal with merge conflicts :D

@0xmiros
Copy link
Contributor

0xmiros commented Jan 19, 2024

@blazejkustra or @fabioh8010 can you please review this PR in TS aspect? 3 new files are added.

Copy link
Contributor

Copy link
Contributor

@blazejkustra blazejkustra left a comment

Choose a reason for hiding this comment

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

Just an idea, we could merge this PR with Ts migration and merge as one. Left one comment

Comment on lines 1 to 4
type ImageProps = {
/** Event called with image dimensions when image is loaded */
onLoad?: (event: {nativeEvent: {width: number; height: number}}) => void;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

ImageProps are missing some props, you can copy them from here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blazejkustra

Thank you for pointing out the ImageProps. I've reviewed the reference from here and appreciate your suggestion. However, I have some reservations about incorporating these types into my current Pull Request, as they seem tangential to the specific change I'm introducing:

  1. History of Changes: The file you referenced has undergone several revisions. Directly copying it might inadvertently overwrite its existing history, leading to potential misattribution of changes.
  2. Irrelevant Props: Some of the props, such as isAuthTokenRequired, are not relevant to the BaseImage component. Including them could introduce unnecessary complexity or errors.
  3. Platform Specificity: The BaseImage component is tailored for specific platforms. Using the full ImageProps could lead to compatibility issues between Expo and React Native Web image, given their differences.

Given these concerns, I propose two alternatives:

  • Option A: We could remove the types.ts file from this Pull Request and defer its introduction to the other PR, maintaining clarity and separation of concerns.
  • Option B: Alternatively, we could rename the type to BaseImageProps and limit its scope to only include essential props like onLoad, ensuring compatibility and relevance to the current changes.

I believe either approach would maintain the integrity and focus of the Pull Request while addressing the need for proper typing. I'm open to further discussion on this matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

@blazejkustra what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

@kidroca Thank you for your perspective on this one! And I agree with you. Both options sounds good to me, although I'm more inclined to option B.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've pushed an update that implements option B

@0xmiros
Copy link
Contributor

0xmiros commented Jan 19, 2024

Just an idea, we could merge this PR with Ts migration and merge as one. Left one comment

yes, I thought the same.
Image/index.js is the only remaining file to be migrated, along with one word change of resizeModes.js.

@0xmiros
Copy link
Contributor

0xmiros commented Jan 19, 2024

adhoc build (PR domain) shows error as expected.

Screenshot 2024-01-19 at 3 22 14 pm

Copy link
Contributor

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

LGTM besides @blazejkustra comments!

@aimane-chnaif
Copy link
Contributor

I will need to take this over again as @0xmiroslav is no longer C+

@aimane-chnaif
Copy link
Contributor

Please fix conflict

# Conflicts:
#	src/components/RoomHeaderAvatars.js
#	src/pages/ProfilePage.js
@kidroca
Copy link
Contributor Author

kidroca commented Jan 26, 2024

Please fix conflict

✔️ Done

@trjExpensify trjExpensify requested review from aimane-chnaif and removed request for 0xmiros January 29, 2024 20:02
@trjExpensify
Copy link
Contributor

👋 assigned you @aimane-chnaif

@aimane-chnaif

This comment was marked as resolved.

@aimane-chnaif
Copy link
Contributor

weird loading indicator but also happening on main. so out of scope

Screenshot 2024-01-30 at 4 54 56 PM

@aimane-chnaif
Copy link
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
android.mov
Android: mWeb Chrome
mchrome.mov
iOS: Native
ios.mov
iOS: mWeb Safari
msafari.mov
MacOS: Chrome / Safari
web.mov
MacOS: Desktop
desktop.mov

Copy link
Contributor

@aimane-chnaif aimane-chnaif left a comment

Choose a reason for hiding this comment

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

Hopefully pass staging this time 🙏

Copy link
Contributor

@Beamanator Beamanator left a comment

Choose a reason for hiding this comment

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

LGTM! 🙏

@Beamanator Beamanator merged commit 30ead56 into Expensify:main Jan 31, 2024
16 checks passed
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/Beamanator in version: 1.4.35-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Feb 2, 2024

🚀 Deployed to production by https://github.com/mountiny in version: 1.4.35-7 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants