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 #36560

Merged

Conversation

kidroca
Copy link
Contributor

@kidroca kidroca commented Feb 15, 2024

Note: This PR was previously reverted (see PR #35636 for reference). Our objective now is to identify an issue that manifests only post-deployment. We should be able to use the pr-testing link to detect any problems in loading/rendering images

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-02-15 at 10 07 25
MacOS: Chrome / Safari
Screen.Recording.2024-02-15.at.9.35.41.mov
Screenshot 2024-02-15 at 9 43 15
MacOS: Desktop Screenshot 2024-02-15 at 9 58 09

Copy link

melvin-bot bot commented Feb 15, 2024

@aimane-chnaif 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]

@aimane-chnaif
Copy link
Contributor

@Beamanator please generate adhoc build

@Beamanator Beamanator self-requested a review February 15, 2024 09:10
@Beamanator
Copy link
Contributor

Beamanator commented Feb 15, 2024

Creating adhoc build soon 👍

Update: Building... https://github.com/Expensify/App/actions/runs/7913538905

This comment has been minimized.

@kidroca
Copy link
Contributor Author

kidroca commented Feb 15, 2024

@aimane-chnaif, @francoisl, @Beamanator

I couldn't recreate the problem that led to reverting the previous PR, following these steps: #35636 (comment)

Furthermore, until the receipt image is uploaded and processed we're supplying the local file blob to represent the receipt image
image

This means the browser can release the local blob resource when it's no longer used, though I wasn't able to reproduce this either, but technically if you refresh, or better yet close the app window and open it again this local blob might be released to conserve resources

I have a working theory that since it takes some time to process a receipt, once the receipt result is ready the backend provides the remote attachment address, at that point the user happened to be offline and the remote image couldn't load
I see that in such cases we currently show an offline image placeholder
image

Or perhaps the issue @francoisl encountered is an unrelated, intermittent bug that manifests independently of the image update changes.

@francoisl
Copy link
Contributor

Not sure if it helps, but here's what I'm seeing from https://36560.pr-testing.expensify.com/

  • The image has a blob:https:// src, even after reloading the app, and even after the receipt is finished processing in the backend
  • While offline, switching back and forth to a chat that has the blob: image replaces it with the offline placeholder - even though I can still refresh the blob: image in a separate tab, which seems to indicate that the browser did not release the blob
Screen.Recording.2024-02-15.at.12.18.13.PM.mov

@kidroca
Copy link
Contributor Author

kidroca commented Feb 16, 2024

I believe I've identified the issue—the preflight response isn't being cached, resulting in its repeated execution with each image render. While the images themselves are cached, the browser is forced to reinitiate the preflight check due to the absence of caching headers. These headers are crucial as they instruct the browser on whether and how long to cache the preflight request.

  • a preflight request is being made even though the actual resource is cached
    image

Consequently, in offline scenarios, the browser is unable to perform the preflight request, leading to a failure in loading all images.

Here are some visuals to illustrate the point:

  • Image caching example (Notice how the GET response for the image is cached):
    Image caching example

  • Preflight request missing caching headers:
    Preflight request missing caching headers

To fix the issue in offline usage, and omit doing preflight request for cached items we should add some cache to the preflight response as well (this is a backend task)

@cubuspl42
Copy link
Contributor

We're holding on this PR in #35041. Is this going to land relatively soon, or should we just figure out the way forward without waiting for this?

@cubuspl42
Copy link
Contributor

Conflicts ⚔️

@kidroca
Copy link
Contributor Author

kidroca commented Feb 26, 2024

This Pull Request cannot be merged at this time due to unresolved issues related to preflight caching, as detailed in this comment. Proceeding with the merge before implementing the required backend adjustments would replicate the offline experience issues that previously necessitated a revert, as seen in PR #35636.

I will address any conflicts that arise in the meantime once the backend changes are completed, setting the stage for us to move forward with this PR.

@trjExpensify
Copy link
Contributor

To fix the issue in offline usage, and omit doing preflight request for cached items we should add some cache to the preflight response as well (this is a backend task)

@Beamanator are you going to take this on, or perhaps @justinpersaud, as you've been helping out on resolving some backend issues related to this?

@justinpersaud
Copy link
Contributor

@kidroca I just deployed a change to enable caching of preflight requests if you'd like to test again and report back.

@kidroca kidroca force-pushed the kidroca/feat/attachment-with-headers-2 branch 2 times, most recently from 5911738 to c5bf5fc Compare March 4, 2024 17:27
@kidroca
Copy link
Contributor Author

kidroca commented Mar 4, 2024

@kidroca I just deployed a change to enable caching of preflight requests if you'd like to test again and report back.

It doesn't seem possible to use the current PR review environment: https://36560.pr-testing.expensify.com/

image

I've pushed updates to sync with main and resolve conflicts, so we need to re-run the adhoc build: #36560 (comment)

@amyevans
Copy link
Contributor

This got built, however the workflow failed on the Post a GitHub comment with app download links for testing step, so I'm going to just post them manually:

kidroca added 3 commits March 13, 2024 13:18
(cherry picked from commit 19b605e)
(cherry picked from commit 37df3e3)
- Introduced `BaseImage` component that branches between native and web implementations.
    - **Native**: Utilizes `expo-image` 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.
- Added `BaseImage` specific props
- Adapt to `expo-image` deprecation of `event.nativeEvent` usage.
- Ensure compatibility with components using the `onLoad` prop.
@kidroca kidroca force-pushed the kidroca/feat/attachment-with-headers-2 branch from c5bf5fc to 22a1de0 Compare March 13, 2024 11:19
@kidroca
Copy link
Contributor Author

kidroca commented Mar 13, 2024

@amyevans, thank you for your assistance. However, it appears that this PR was still significantly out of sync with main, leading us to encounter the "Update Requested" screen again on the review environment: https://36560.pr-testing.expensify.com/.

I've just re-synced with main. Could you please trigger another ad-hoc build? 🥺

@Beamanator
Copy link
Contributor

Beamanator commented Mar 13, 2024

Sorry y'all, I just got back from vacay 🙏 I just triggered another adhoc build 👍 - https://github.com/Expensify/App/actions/runs/8264151190

Copy link
Contributor

@kidroca
Copy link
Contributor Author

kidroca commented Mar 13, 2024

Hello @Beamanator,

I noticed that the Web build and deploy process was successful: https://github.com/Expensify/App/actions/runs/8264151190/job/22607147679. However, there seems to be an issue, as the review environment still displays the "Update Requested" screen. This could either be because the build isn't using the latest code, or there might be a build issue. Upon inspecting the details, I observed that App's version in the network calls is 1.4.42-0, which is incorrect. After pulling the remote branch, I confirmed that the version should be 1.4.51-3. Therefore, it appears something is not functioning as expected with the ad hoc build process.

image

Could you investigate, or confirm you have the same issue on this review env: https://36560.pr-testing.expensify.com/

@aimane-chnaif
Copy link
Contributor

I have no issue. No "Update required" popup

Screenshot 2024-03-13 at 12 38 06 PM

@Beamanator
Copy link
Contributor

Same here 🤔 I'm able to sign in and I see appversion: 1.4.51-3

@kidroca
Copy link
Contributor Author

kidroca commented Mar 13, 2024

Same here 🤔 I'm able to sign in and I see appversion: 1.4.51-3

Thank you for checking, everyone. It appears to have been a caching issue, as switching to a different browser resolved the problem for me. I can no longer reproduce the issue with thumbnail images when offline, nor the issue with failed receipt OCRs. I've confirmed that the preflight requests are now cached for 24 hours, which means they are entirely skipped in CORS checks.

✔️ The PR is now ready for review. It has already been reviewed and approved in previous iterations, but it's worth taking another look since I introduced some changes while resolving conflicts with the recent conversion of the Image to TypeScript.

@Beamanator
Copy link
Contributor

Beamanator commented Mar 14, 2024

Amazing, thanks so much @kidroca 🙏 @aimane-chnaif can you please do another full review before we move forward with this PR?

@aimane-chnaif
Copy link
Contributor

Code looks good.
Retested past regressions (#32593, #35580) on both localhost and PR build. Not repro anymore

@aimane-chnaif
Copy link
Contributor

Hope this behavior would be fine: (at least not blocker)

Offline icon glitches for a brief moment (around 21s in video)

Screen.Recording.2024-03-17.at.4.24.16.PM.mov

@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 either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • 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 UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design 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

@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.

This looks great to me, Hoping this is the last time we have to merge this 🙏

@Beamanator Beamanator merged commit 135cfeb into Expensify:main Mar 18, 2024
15 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.54-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/AndrewGable in version: 1.4.54-4 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 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.

9 participants