Skip to content

[No QA] Avatar component decomposition#95050

Open
jmusial wants to merge 15 commits into
Expensify:mainfrom
software-mansion-labs:chore/decomposition-of-avatar-component
Open

[No QA] Avatar component decomposition#95050
jmusial wants to merge 15 commits into
Expensify:mainfrom
software-mansion-labs:chore/decomposition-of-avatar-component

Conversation

@jmusial

@jmusial jmusial commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

This PR decomposes the monolithic Avatar component into focused UserAvatar, WorkspaceAvatar, constructed from image, icon, and initials avatar primitives while preserving the existing public API.

It also keeps Avatar source resolution and load-error handling in dedicated hooks, making the rendering paths easier to understand, test, and extend.

Fixed Issues

$ #95597
PROPOSAL:

Tests

Pure refactor, should be unnoticable to user.

  1. Open the app
  2. Navigate through pages, verify that avatar sizes are the same as on prod/staging.

Offline tests

N/A

QA Steps

N/A

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
  • 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 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 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)
  • 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)
  • 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 new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • 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.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • 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

MacOS: Chrome / Safari
Screen.Recording.2026-07-09.at.13.41.52.mov

@jmusial

jmusial commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 5630d21512

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/components/Avatar/UserAvatar.tsx 100.00% <100.00%> (ø)
src/components/Avatar/WorkspaceAvatar.tsx 100.00% <100.00%> (ø)
src/components/Avatar/hooks/useAvatarLoadError.ts 100.00% <100.00%> (ø)
src/components/Avatar/hooks/useUserAvatarSource.ts 100.00% <100.00%> (ø)
...omponents/Avatar/hooks/useWorkspaceAvatarSource.ts 100.00% <100.00%> (ø)
src/components/Avatar/index.tsx 100.00% <100.00%> (ø)
src/components/Avatar/primitives/AvatarBody.tsx 100.00% <100.00%> (ø)
src/components/Avatar/primitives/AvatarIcon.tsx 100.00% <100.00%> (ø)
src/components/Avatar/primitives/AvatarImage.tsx 100.00% <100.00%> (ø)
...rc/components/Avatar/primitives/AvatarInitials.tsx 100.00% <100.00%> (ø)
... and 2 more
... and 170 files with indirect coverage changes

@jmusial jmusial changed the title Avatar component decomposition poc Avatar component decomposition Jul 7, 2026
@jmusial

jmusial commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: acd871a794

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@jmusial jmusial changed the title Avatar component decomposition [No QA] Avatar component decomposition Jul 8, 2026
@jmusial

jmusial commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 88b1ae1551

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@jmusial
jmusial marked this pull request as ready for review July 9, 2026 11:45
@jmusial
jmusial requested review from a team as code owners July 9, 2026 11:45
@melvin-bot
melvin-bot Bot requested a review from abdulrahuman5196 July 9, 2026 11:45
@melvin-bot
melvin-bot Bot requested review from joekaufmanexpensify and removed request for a team July 9, 2026 11:45
@melvin-bot

melvin-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

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

@melvin-bot
melvin-bot Bot removed the request for review from a team July 9, 2026 11:45
@jmusial

jmusial commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@roryabraham - this is the first PR from Avatar decomposition. Sizes, and current usage migration will be in follow ups

const {hasImageError, onImageError} = useAvatarLoadError(originalSource);

const source = originalSource;
const optimizedSource = optimizeAvatarSource(source);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❌ CONSISTENCY-3 (docs)

The source-to-renderable-model resolution logic here is duplicated almost verbatim in useUserAvatarSource.ts. Both hooks compute optimizeAvatarSource(source), derive useFallBackAvatar (hasImageError || !source || source === defaultAvatars.FallbackAvatar), compute avatarSource = useFallBackAvatar ? fallbackAvatar : (optimizedSource ?? fallbackAvatar), and then return the identical {variant: 'image', ...} / {variant: 'icon', ...} shapes. Duplicating this raises maintenance cost and bug risk if the resolution rules change.

Extract the shared logic into a helper that both hooks call, keeping only the user/workspace-specific inputs (fallback avatar, testID, icon colors) at each call site:

// e.g. resolveAvatarBody.ts
function resolveAvatarBody({source, hasImageError, onImageError, defaultAvatars, fallbackAvatar, fallbackAvatarTestID, iconColors}): ResolvedImageAvatar | ResolvedIconAvatar {
    const optimizedSource = optimizeAvatarSource(source);
    const useFallBackAvatar = hasImageError || !source || source === defaultAvatars.FallbackAvatar;
    const avatarSource = useFallBackAvatar ? fallbackAvatar : (optimizedSource ?? fallbackAvatar);
    if (typeof avatarSource === 'string') {
        return {avatarSource, variant: 'image', hasImageError, fallbackAvatarTestID, onImageError};
    }
    return {avatarSource, variant: 'icon', hasImageError, iconColors, fallbackAvatarTestID, onImageError};
}

Then useUserAvatarSource and useWorkspaceAvatarSource each only compute their own fallbackAvatar/fallbackAvatarTestID/iconColors and delegate to it.


Reviewed at: 88b1ae1 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure about this one. I personally like verbosity of current implementation even if it means 1 repetition of code

@joekaufmanexpensify

Copy link
Copy Markdown
Contributor

No product review needed

@joekaufmanexpensify
joekaufmanexpensify removed their request for review July 9, 2026 21:27
Comment thread src/components/Avatar/hooks/useAvatarLoadError.ts Outdated
Comment thread src/components/Avatar/hooks/useAvatarLoadError.ts
Comment thread src/components/Avatar/hooks/useAvatarLoadError.ts Outdated
import useAvatarLoadError from './useAvatarLoadError';

type UseUserAvatarSourceParams = {
source?: AvatarSource;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some comments explaining source and avatarID would be helpful. It is ever valid to have both, or would a discriminated union be more precise?

If we could make some of these fields non-optional it might ripple some simplifications through the hook.

import useAvatarLoadError from './useAvatarLoadError';

type UseWorkspaceAvatarSourceParams = {
source?: AvatarSource;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it really make sense for all of these to be optional?

Comment thread src/components/Avatar/hooks/useWorkspaceAvatarSource.ts Outdated
Comment thread src/components/Avatar/hooks/useWorkspaceAvatarSource.ts Outdated
@roryabraham

Copy link
Copy Markdown
Contributor

Thanks for making those changes @jmusial, but I remain skeptical that both source and avatarID should be optional. It sounds like they're not mutually exclusive, but what would happen if both were undefined?

@jmusial

jmusial commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for making those changes @jmusial, but I remain skeptical that both source and avatarID should be optional. It sounds like they're not mutually exclusive, but what would happen if both were undefined?

I see your point that maybe this may be split down further. Unfortunately I need to run for today, I will pick it up from here next week.

@jmusial

jmusial commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 77f2fd7407

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/components/Avatar/hooks/useAvatarLoadError.ts
@jmusial

jmusial commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b53c297188

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

size,
fill,
testID,
avatarID = CONST.DEFAULT_NUMBER_ID,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P3 Badge Preserve omitted workspace avatar IDs

When the compatibility Avatar is used for a workspace without avatarID (the prop is still optional), this default converts the missing value to 0; WorkspaceAvatar then hashes '0' for the default workspace icon colors. Before the refactor, the workspace path used avatarID?.toString() ?? '', so omitted IDs hashed the empty string. This silently changes the background/fill for legal calls such as a workspace avatar with only type and name, especially on fallback after an image error; pass undefined through for workspaces or keep the old empty-string fallback.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is deliberate and I think acceptable.

@jmusial

jmusial commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@roryabraham I updated the PR so now for UserAvatar and WorkspaceAvatar source is optional but avatarID always required. It didn't change internal logic too much. Also compatibility wrapper Avatar kept them optional.

I explored some options with splitting out fallback logic out of the hooks but it overcomplicated the code IMO.

let me know what you think, thx.

And FYI I'll be OOO for Thu and Fri.

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.

3 participants