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

[Avatar] Expose onError hook #4052

Merged
merged 3 commits into from
Mar 10, 2021
Merged

[Avatar] Expose onError hook #4052

merged 3 commits into from
Mar 10, 2021

Conversation

whizkydee
Copy link
Member

WHY are these changes introduced?

Due to surface area constraints, in our app, we use custom a user menu — the activator is a standalone avatar within a button which toggles a Popover. The popover includes an avatar and the full name of the user in question, as opposed to displaying the full name alongside the avatar directly on the top bar.

For Identity users that haven't uploaded an avatar, we attempt to grab an image from gravatar based on their uuid which eventually resolves to their email — that request can either return the image or 404 if the account doesn't exist.

The problem with that is each time the user menu activator is clicked, the popover is re-rendered which results in extraneous requests to the same url that previously failed in an attempt to retrieve the gravatar.

WHAT is this pull request doing?

This adds an onError hook to the Avatar component, which provides consumers with an avenue to make decisions when the HTTP request for an avatar fails. In our case, we'll simply prevent subsequent requests to the same url.

How to 🎩

🖥 Local development instructions
🗒 General tophatting guidelines
📄 Changelog guidelines

Copy-paste this code in playground/Playground.tsx:
import React from 'react';
import {Page, Avatar} from '../src';

export function Playground() {
  return (
    <Page title="Playground">
      <Avatar source="invalidUrl" onError={() => console.log('bummer')} />
    </Page>
  );
}

🎩 checklist

  • Tested on mobile
  • Tested on multiple browsers
  • Tested for accessibility
  • Updated the component's README.md with documentation changes
  • Tophatted documentation changes in the style guide
  • For visual design changes, pinged one of @ HYPD, @ mirualves, @ sarahill, or @ ry5n to update the Polaris UI kit

@whizkydee whizkydee self-assigned this Mar 9, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2021

🟡 This pull request modifies 3 files and might impact 12 other files. This is an average splash zone for a change, remember to tophat areas that could be affected.

Details:
All files potentially affected (total: 12)
📄 UNRELEASED.md (total: 0)

Files potentially affected (total: 0)

🧩 src/components/Avatar/Avatar.tsx (total: 12)

Files potentially affected (total: 12)

🧩 src/components/Avatar/tests/Avatar.test.tsx (total: 0)

Files potentially affected (total: 0)

@alex-page
Copy link
Member

@whizkydee I am wondering if the error could be handled in your app before using the component? It feels like the wrong level of abstraction. Interested to know what blockers exist there?

@whizkydee
Copy link
Member Author

@alex-page The problem is we have know way of knowing that the image errored, so we can't handle it. This hook is meant to inform us.

Copy link
Member

@alex-page alex-page left a comment

Choose a reason for hiding this comment

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

Happy to ship this, but I still think this check should happen before rendering the Avatar component.

Copy link
Contributor

@kyledurand kyledurand left a comment

Choose a reason for hiding this comment

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

I think this makes sense to add but also agree with Alex that the best UX would be to determine if the image request 404'd before trying to render the Avatar.

Sounds like your situation is a bit different with the re-rendering on click though so I think if this works for your use case then 🚢

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.

None yet

3 participants