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

Use fallback user avatar in cases where the user is unknown to us #39229

Merged

Conversation

Kicu
Copy link
Contributor

@Kicu Kicu commented Mar 29, 2024

Details

  • the logic that decides which avatar to show is now heavily consolidated into the <Avatar> component
  • right now the source of truth is almost always avatar prop from personalDetails. It is returned from backend, and any user "known" to us will have such accountId
  • we will now display FallbackAvatar when:
    • there is no details/accountID
    • when accountID was optimistically set in TS code
    • as a final fallback in Avatar component (that behaviour is unchanged)
  • I eliminated 30+ calls to getAvatar() because what they mostly did was use source + accountID to decide whether to show SVG version of default avatar or Concierge avatar. Now source prop is simply passed through the components and the call to getAvatar() is done only once inside <Avatar />.

special note for icons:

Fixed Issues

$ #38743
PROPOSAL:

Tests

  1. go to every place in the app where we can display an unknown user and verify that the avatar next to them is the fallback one (and not the colorful one)
  2. places to verify:
  • search option from Chats list
  • searching users from "Start chat"
  • searching users from "Request money"
  • searching users from "Send money"
  • searching users from "Assign Task"
  • searching users from Workspace list > Workspace > Members > Invite member
  1. browse through the app and verify that no other avatar logic was broken

Offline tests

QA Steps

  • Verify that no errors appear in the JS console

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

Screenshots/Videos

Android: Native
rec-fallback-andr-SM.mp4
Android: mWeb Chrome
iOS: Native
rec-fallback-ios-SM.mp4
iOS: mWeb Safari
MacOS: Chrome / Safari
rec-fallback-web-1.mp4
rec-fallback-web-2.mp4
MacOS: Desktop

@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch from 8d44e9d to f606775 Compare March 29, 2024 10:09
@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch from f606775 to fb6d6d6 Compare April 2, 2024 07:21
@Kicu Kicu marked this pull request as ready for review April 2, 2024 07:22
@Kicu Kicu requested a review from a team as a code owner April 2, 2024 07:22
@melvin-bot melvin-bot bot requested review from s77rt and removed request for a team April 2, 2024 07:22
Copy link

melvin-bot bot commented Apr 2, 2024

@s77rt 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]

Copy link
Contributor

@s77rt s77rt left a comment

Choose a reason for hiding this comment

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

Avoid explicitly setting the fallback avatar i.e. replace all instance were we return the fallback avatar with returning null or undefined. Then in the Avatar component if no source is provided then use the fallback avatar

@@ -70,7 +70,7 @@ function DetailsPage({personalDetails, route, session}: DetailsPageProps) {
accountID: optimisticAccountID,
login,
displayName: login,
avatar: UserUtils.getDefaultAvatar(optimisticAccountID),
avatar: UserUtils.getFallbackAvatar(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
avatar: UserUtils.getFallbackAvatar(),

@Kicu
Copy link
Contributor Author

Kicu commented Apr 3, 2024

@s77rt that is a good comment in general and I'd prefer to not return the fallback avatar explicitly.
However originally I didn't do it, since there are places that use icons and they require a source, which means because of typescript I need to go over multiple places in the app and allow a missing avatar/icon to pass through so that in the end the fallback can be applied.
This might require a lot of changes, but perhaps its worth it.

I will try to clean this up so that we never pass FallbackAvatar through function calls

@Kicu
Copy link
Contributor Author

Kicu commented Apr 4, 2024

@s77rt I have now spent a few hours trying to refactor to returning undefined instead of fallback avatar.
That will require changes in a lot of places, because the function getAvatar (https://github.com/Expensify/App/blob/main/src/libs/UserUtils.ts#L158) would have to start returning AvatarSource | undefined.

That function is used in 10+ files multiple times, and it affects many places - multiple parts of the app that display avatars depend on it. Often its used when creating icons[] which are of type OnyxCommon.Icon which requires a source to always be there. I don't want to touch logic for displaying multiple icons or multiple avatars.

Some examples of usage that you can check:

(as a quick test you can try to modify the return type of getAvatar() to AvatarSource | undefined and then run typecheck and see what is affected)

In general this change would affect so many places in the code that I believe its not worth doing in this PR.
I suggest that we keep my original code. What do you think?

Copy link
Contributor

@s77rt s77rt left a comment

Choose a reason for hiding this comment

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

Found 2 missing cases where we should use the fallback avatar if user is not found in onyx

  • In ReportUtils.getPersonalDetailsForAccountID
  • In ProfilePage

src/libs/OptionsListUtils.ts Outdated Show resolved Hide resolved
@s77rt
Copy link
Contributor

s77rt commented Apr 5, 2024

We may have a problem in ReportUtils.getDisplayNamesWithTooltips because it has a param personalDetailsList that may accept data that is both from or not from onyx. It's not clear if the avatar should use default or fallback.

Currently using default which causes the following bug (in money request)
Screenshot 2024-04-05 at 1 31 50 AM

@Kicu
Copy link
Contributor Author

Kicu commented Apr 8, 2024

@s77rt
Thanks for pointing out the problem. That bug made me go down the rabbit hole of the avatar logic and I spend a lot of time trying come up with a reasonable fix.
... at this point I don't believe any sensible fix is possible 😅

tl;dr
We are generating optimistic IDs for participants before we even call backend and we do this in several places of the app. Because of it there is no easy way to guess whether a participantDetails object represents an actual user that we "know" or if it represents just temporary optimistic data create somewhere in the app.

Example places where this happens (there might be more):

As you can see in the described examples ☝️ I think there is no way to simply tell whether the user IS or IS NOT known to us, and base the fallback avatar logic on this.
Calling @grgia since you originally created this issue.

At this point Im not sure what is the best way forward, I'm reluctant to modify code related to optimistic accountID generation since it touches multiple places in the app.
Or perhaps I misunderstood something related to this task, in that case point me in a better direction.

@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch from fb6d6d6 to 9293f31 Compare April 8, 2024 14:25
@grgia
Copy link
Contributor

grgia commented Apr 9, 2024

@Kicu

We can definitely call it on this one if it's proving a rabbit hole to fix. It's not high enough priority if it's proven to be complex.

One idea to fix this is by tracing/removing all of the getDefaultAvatar calls in app, if we just store the default avatar in the source for users (BE) then we'd be able to tell which users we know/have an avatar set. Then default to the fallback whenever the source is empty.

We'd then use one single call to getDefaultAvatar in the avatar component to switch to the crisp SVGs/grab the local concierge, etc. avatars.

Let me know if that's something you already ruled out

@Kicu
Copy link
Contributor Author

Kicu commented Apr 10, 2024

@grgia Thanks for the suggestion, I think this is something that is worth investigating.

if we just store the default avatar in the source for users (BE) then we'd be able to tell which users we know/have an avatar set.

In this scenario who would do the storing? Frontend code when making an api call and sending it to backend? or backend would do this on their end and always return an avatar url?
I'm asking because this is related to the "is the user known to us" issue - Im no longer sure I know when we decide that the user "is known" to us 😅

Otherwise I think this is a sound idea to base the avatar on 1 field (url) instead of calling getDefaultAvatar + getAvatar in 20+ places in the app as we do right now.

The only complication I can see is that in some places we don't use a property avatar but rather create an array of icons (example: https://github.com/Expensify/App/blob/main/src/libs/OptionsListUtils.ts#L373-L380 or here: https://github.com/Expensify/App/blob/main/src/libs/OptionsListUtils.ts#L1838).
I worry if that no icons will be returned then that might affect some display logic.
But that is something I will try to quickly investigate now.

Tell me what you think

@Kicu Kicu changed the title Use fallback user avatar in cases where the user is unknown to us [Temporary WIP] Use fallback user avatar in cases where the user is unknown to us Apr 12, 2024
@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch 4 times, most recently from 6e978c8 to 42d2a5e Compare April 12, 2024 10:09
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.

Code looks much cleaner now, but you have conflicts and a lint failing 👍

@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch 2 times, most recently from db4484f to 3cdf8f5 Compare April 15, 2024 09:32
const fallbackAvatar = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatar(name) : fallbackIcon || Expensicons.FallbackAvatar;
const fallbackAvatarTestID = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatarTestID(name) : fallbackIconTestID || 'SvgFallbackAvatar Icon';
const avatarSource = useFallBackAvatar ? fallbackAvatar : source;
const avatarSource = useFallBackAvatar ? fallbackAvatar : UserUtils.getAvatar(source, accountID) ?? Expensicons.FallbackAvatar;
Copy link
Contributor Author

@Kicu Kicu Apr 15, 2024

Choose a reason for hiding this comment

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

this line gives us 100% guarantee that there will be something in the source prop, so that we will at the worst always display FallbackAvatar (if source and accountID are missing).

Thanks to that we can just pass props directly into the Avatar component, and we don't have to call .getAvatar() in multiple places in code.

@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch from 3cdf8f5 to 7af9cd0 Compare April 15, 2024 09:40
@s77rt
Copy link
Contributor

s77rt commented Apr 18, 2024

This bug #39229 (comment) is on staging so let's not block on that

@Kicu
Copy link
Contributor Author

Kicu commented Apr 19, 2024

@s77rt ready for re-review

@melvin-bot melvin-bot bot requested a review from grgia April 19, 2024 16:42
@Kicu Kicu force-pushed the kicu/38743-use-fallback-avatar branch from be930bc to 53b3251 Compare April 22, 2024 07:29
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.

LGTM!

src/pages/ProfilePage.tsx Outdated Show resolved Hide resolved
};

function Avatar({
source,
source: originalSource,
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the originalSource?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's just the source prop renamed to originalSource. We defined a new variable source variable inside.

const source = isWorkspace ? originalSource : UserUtils.getAvatar(originalSource, accountID);

The new source variable will take the original source and use the svg version of the avatar if possible.

Copy link
Contributor

@grgia grgia left a comment

Choose a reason for hiding this comment

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

Code / changes LGTM, quick question before merging:

Are we still displaying the SVG files?

@s77rt
Copy link
Contributor

s77rt commented Apr 24, 2024

Yes #39229 (comment)

@grgia
Copy link
Contributor

grgia commented Apr 24, 2024

Thanks @s77rt !

@grgia grgia merged commit 08b7ff4 into Expensify:main Apr 24, 2024
18 checks passed
@grgia
Copy link
Contributor

grgia commented Apr 24, 2024

Nice work on this one @Kicu

@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/grgia in version: 1.4.66-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/grgia in version: 1.4.66-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 1.4.66-5 🚀

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants