Skip to content

fix(client): sync profile avatar editor state across cancel, save, an… - #40380

Open
opaksbyron wants to merge 1 commit into
RocketChat:developfrom
opaksbyron:fix/profile-avatar-state-sync
Open

fix(client): sync profile avatar editor state across cancel, save, an…#40380
opaksbyron wants to merge 1 commit into
RocketChat:developfrom
opaksbyron:fix/profile-avatar-state-sync

Conversation

@opaksbyron

@opaksbyron opaksbyron commented May 3, 2026

Copy link
Copy Markdown

Proposed changes

Closes #38697.

The "My Account → Profile" avatar editor's local preview state was not synchronized with the parent form's avatar field, and the hidden <input type="file"> was never reset between selections. This caused several inconsistencies after cancel/save/reset cycles:

  • Re-uploading the same file after Cancel produced no preview (file input's change event doesn't fire when the value is unchanged).
  • Cancelling after a click on Reset/Initials sometimes left a stale preview instead of restoring the saved avatar.
  • Reset → Save would leave the old photo's URL still rendered because the cached image URL had no cache-buster.

Changes

  • useSingleFileInput — clear the file input's value before each click() and after each successful selection so the same file always re-fires the change event.
  • UserAvatarEditor — accept the form's current avatar value as a prop and clear the local preview state in a useEffect whenever the form transitions from a set value back to empty (i.e. a Cancel/Save reset).
  • AccountProfileForm — pass the watched avatar value down to UserAvatarEditor, and include avatar: '' in the post-save reset() so the editor sees the transition.
  • The reset preview URL now includes the user's etag to bust the browser cache when the saved avatar changes.

Issue(s)

Closes #38697.

Steps to test or reproduce

  1. Go to My Account → Profile.
  2. Test A: Upload a picture → Cancel → upload the same picture again → preview reappears immediately.
  3. Test B: Upload a picture → Save → profile photo updates.
  4. Test C: Upload a picture → click Reset/Initials → Cancel → preview restores to the previously saved avatar.
  5. Test D: Upload a picture → Save → click Reset/Initials → Save → avatar resets to initials in your profile.

Further comments

The broader UI cache (sidebar, header avatar) refreshing without a full page reload is a separate useUser invalidation concern and is out of scope here — this PR fixes the editor state itself.

Summary by CodeRabbit

  • Bug Fixes
    • Improved profile avatar editor state sync across cancel, save, and reset flows.
    • Avatar file input now clears after selection and before re-opening, ensuring the same file can be re-selected after cancel.
    • Canceling the editor correctly restores the saved avatar and removes any local preview.
    • Saving an avatar no longer leaves stale preview imagery.
    • Profile form reset now triggers only after a successful save.
    • Avatar refresh now uses cache-busting based on the user’s ETag when the saved avatar changes.

@opaksbyron
opaksbyron requested a review from a team as a code owner May 3, 2026 23:05
@dionisio-bot

dionisio-bot Bot commented May 3, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot

changeset-bot Bot commented May 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: aa80d5c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@rocket.chat/meteor Patch
@rocket.chat/core-typings Patch
@rocket.chat/rest-typings Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented May 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6f8fa68c-5abb-4f7a-bc16-fbe3dfa765ad

📥 Commits

Reviewing files that changed from the base of the PR and between be873ad and aa80d5c.

📒 Files selected for processing (4)
  • .changeset/fix-profile-avatar-state-sync.md
  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
  • apps/meteor/client/hooks/useSingleFileInput.ts
  • apps/meteor/client/views/account/profile/AccountProfileForm.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • .changeset/fix-profile-avatar-state-sync.md
  • apps/meteor/client/views/account/profile/AccountProfileForm.tsx
📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
🧠 Learnings (7)
📚 Learning: 2026-02-10T16:32:42.586Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 38528
File: apps/meteor/client/startup/roles.ts:14-14
Timestamp: 2026-02-10T16:32:42.586Z
Learning: In Rocket.Chat's Meteor client code, DDP streams use EJSON and Date fields arrive as Date objects; do not manually construct new Date() in stream handlers (for example, in sdk.stream()). Only REST API responses return plain JSON where dates are strings, so implement explicit conversion there if needed. Apply this guidance to all TypeScript files under apps/meteor/client to ensure consistent date handling in DDP streams and REST responses.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-05-11T20:30:35.265Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 40480
File: apps/meteor/client/meteor/startup/accounts.ts:59-61
Timestamp: 2026-05-11T20:30:35.265Z
Learning: In Rocket.Chat’s Meteor client code, when calling `dispatchToastMessage` with `{ type: 'error' }`, pass the raw caught error object as `message` without manual normalization. `dispatchToastMessage` is designed to accept `message: unknown` for error toasts, so avoid converting errors to strings (e.g., `String(error)`) or extracting `error.message` before passing them.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-04-14T21:10:31.855Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 36292
File: apps/meteor/client/hooks/useHasValidLocationHash.ts:7-12
Timestamp: 2026-04-14T21:10:31.855Z
Learning: When reviewing files in apps/meteor/client/hooks/, do not treat JSDoc-style comments on React hooks (especially exported hooks) as a violation of any “avoid code comments in implementation” guideline. It’s acceptable to use JSDoc to document the public API of exported hooks (e.g., parameter/return types, intended usage), as long as it documents behavior/contracts rather than adding narrative implementation comments.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
📚 Learning: 2026-03-27T14:52:56.865Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 39892
File: apps/meteor/client/views/room/contextualBar/Threads/Thread.tsx:150-155
Timestamp: 2026-03-27T14:52:56.865Z
Learning: In Rocket.Chat, there are two different `ModalBackdrop` components with different prop APIs. During review, confirm the import source: (1) `rocket.chat/fuselage` `ModalBackdrop` uses `ModalBackdropProps` based on `BoxProps` (so it supports `onClick` and other Box/DOM props) and does not have an `onDismiss` prop; (2) `rocket.chat/ui-client` `ModalBackdrop` uses a narrower props interface like `{ children?: ReactNode; onDismiss?: () => void }` and handles Escape keypress and outside mouse-up, and it does not forward arbitrary DOM props such as `onClick`. Flag mismatched props (e.g., `onDismiss` passed to the fuselage component or `onClick` passed to the ui-client component) and ensure the usage matches the correct component being imported.

Applied to files:

  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
🔇 Additional comments (2)
apps/meteor/client/hooks/useSingleFileInput.ts (1)

57-57: LGTM!

Also applies to: 67-72

apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx (1)

6-7: LGTM!

Also applies to: 20-46, 90-91


Walkthrough

This PR synchronizes avatar input, preview, and form state across cancel, save, and reset flows. It clears file inputs for same-file re-selection, resets local previews when the form clears the avatar, adds etag cache busting, and clears the avatar field after successful saves.

Changes

Avatar State Synchronization

Layer / File(s) Summary
File input re-selection
apps/meteor/client/hooks/useSingleFileInput.ts
Clears the file input before processing selections and before opening it, allowing the same file to trigger change events again.
Editor preview synchronization
apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx, apps/meteor/client/views/account/profile/AccountProfileForm.tsx
Passes the form avatar value into the editor, clears local preview state when that value is cleared, and adds the user etag to reset avatar URLs.
Successful form reset and release metadata
apps/meteor/client/views/account/profile/AccountProfileForm.tsx, .changeset/fix-profile-avatar-state-sync.md
Resets the form with an explicitly cleared avatar field after successful saves and records the patch release changeset.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested labels: type: bug

Suggested reviewers: ggazzo, dougfabris

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the main change: synchronizing profile avatar state across cancel/save flows.
Linked Issues check ✅ Passed The changes address the reported avatar preview, reset, cancel, and same-file reupload issues described in #38697.
Out of Scope Changes check ✅ Passed The changes stay focused on avatar editor state sync and cache busting, with no clear unrelated scope expansion.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx (1)

41-42: ⚡ Quick win

Remove inline implementation comments in component logic.

Please drop these two inline comments and keep the intent expressed by code structure/naming.

As per coding guidelines: "Avoid code comments in the implementation".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx`
around lines 41 - 42, Remove the two inline implementation comments inside the
UserAvatarEditor component (the comment block that explains the form-level reset
clearing the avatar field and dropping the local preview) so the component logic
is self-descriptive via variable and function names; update or rename local
symbols if necessary (e.g., preview state, handlers like onCancel/onSave or
variables managing avatar/etag) to preserve intent and readability, but do not
change behavior in UserAvatarEditor — only delete the inline explanatory
comments and ensure tests/props still rely on the existing preview fallback
logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/meteor/client/views/account/profile/AccountProfileForm.tsx`:
- Line 121: The current finally block in AccountProfileForm calls reset({ ...,
avatar: '' }) which clears the avatar even when updateAvatar() fails; change
this so the avatar field is only cleared on successful save: remove avatar: ''
from the finally reset and instead clear avatar in the success path (after a
successful updateProfile/updateAvatar resolution) or conditionally reset avatar
only when updateAvatar returned success; keep the existing reset({...}) in
error/finally paths without touching avatar so the pending preview remains on
failure.

---

Nitpick comments:
In `@apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx`:
- Around line 41-42: Remove the two inline implementation comments inside the
UserAvatarEditor component (the comment block that explains the form-level reset
clearing the avatar field and dropping the local preview) so the component logic
is self-descriptive via variable and function names; update or rename local
symbols if necessary (e.g., preview state, handlers like onCancel/onSave or
variables managing avatar/etag) to preserve intent and readability, but do not
change behavior in UserAvatarEditor — only delete the inline explanatory
comments and ensure tests/props still rely on the existing preview fallback
logic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9b4c3139-0357-4b5f-9d1a-eb689aabbd42

📥 Commits

Reviewing files that changed from the base of the PR and between d33009a and 92341b5.

📒 Files selected for processing (4)
  • .changeset/fix-profile-avatar-state-sync.md
  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
  • apps/meteor/client/hooks/useSingleFileInput.ts
  • apps/meteor/client/views/account/profile/AccountProfileForm.tsx
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
  • apps/meteor/client/views/account/profile/AccountProfileForm.tsx
  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
🧠 Learnings (6)
📚 Learning: 2026-02-10T16:32:42.586Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 38528
File: apps/meteor/client/startup/roles.ts:14-14
Timestamp: 2026-02-10T16:32:42.586Z
Learning: In Rocket.Chat's Meteor client code, DDP streams use EJSON and Date fields arrive as Date objects; do not manually construct new Date() in stream handlers (for example, in sdk.stream()). Only REST API responses return plain JSON where dates are strings, so implement explicit conversion there if needed. Apply this guidance to all TypeScript files under apps/meteor/client to ensure consistent date handling in DDP streams and REST responses.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-04-14T21:10:31.855Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 36292
File: apps/meteor/client/hooks/useHasValidLocationHash.ts:7-12
Timestamp: 2026-04-14T21:10:31.855Z
Learning: When reviewing files in apps/meteor/client/hooks/, do not treat JSDoc-style comments on React hooks (especially exported hooks) as a violation of any “avoid code comments in implementation” guideline. It’s acceptable to use JSDoc to document the public API of exported hooks (e.g., parameter/return types, intended usage), as long as it documents behavior/contracts rather than adding narrative implementation comments.

Applied to files:

  • apps/meteor/client/hooks/useSingleFileInput.ts
📚 Learning: 2026-03-16T21:50:37.589Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39676
File: .changeset/migrate-users-register-openapi.md:3-3
Timestamp: 2026-03-16T21:50:37.589Z
Learning: For changes related to OpenAPI migrations in Rocket.Chat/OpenAPI, when removing endpoint types and validators from rocket.chat/rest-typings (e.g., UserRegisterParamsPOST, /v1/users.register) document this as a minor changeset (not breaking) per RocketChat/Rocket.Chat-Open-API#150 Rule 7. Note that the endpoint type is re-exposed via a module augmentation .d.ts in the consuming package (e.g., packages/web-ui-registration/src/users-register.d.ts). In reviews, ensure the changeset clearly states: this is a non-breaking change, the major version should not be bumped, and the changeset reflects a minor version bump. Do not treat this as a breaking change during OpenAPI migrations.

Applied to files:

  • .changeset/fix-profile-avatar-state-sync.md
📚 Learning: 2026-03-27T14:52:56.865Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 39892
File: apps/meteor/client/views/room/contextualBar/Threads/Thread.tsx:150-155
Timestamp: 2026-03-27T14:52:56.865Z
Learning: In Rocket.Chat, there are two different `ModalBackdrop` components with different prop APIs. During review, confirm the import source: (1) `rocket.chat/fuselage` `ModalBackdrop` uses `ModalBackdropProps` based on `BoxProps` (so it supports `onClick` and other Box/DOM props) and does not have an `onDismiss` prop; (2) `rocket.chat/ui-client` `ModalBackdrop` uses a narrower props interface like `{ children?: ReactNode; onDismiss?: () => void }` and handles Escape keypress and outside mouse-up, and it does not forward arbitrary DOM props such as `onClick`. Flag mismatched props (e.g., `onDismiss` passed to the fuselage component or `onClick` passed to the ui-client component) and ensure the usage matches the correct component being imported.

Applied to files:

  • apps/meteor/client/views/account/profile/AccountProfileForm.tsx
  • apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx
🔇 Additional comments (4)
.changeset/fix-profile-avatar-state-sync.md (1)

2-7: Changeset scope/versioning looks correct.

Patch bump and description align with the behavior fixes in this PR.

apps/meteor/client/hooks/useSingleFileInput.ts (1)

46-61: File input reset flow is solid.

Clearing on both pre-click and post-selection paths is a good fix for same-file re-upload reliability.

apps/meteor/client/components/avatar/UserAvatarEditor/UserAvatarEditor.tsx (1)

37-48: Preview reset synchronization and etag cache-busting look correct.

This should correctly restore saved avatar rendering across cancel/save/reset transitions.

Also applies to: 92-93

apps/meteor/client/views/account/profile/AccountProfileForm.tsx (1)

135-142: Controller value propagation to UserAvatarEditor is correct.

Passing value as avatarObj is the right sync point for cancel/save/reset transitions.

Comment thread apps/meteor/client/views/account/profile/AccountProfileForm.tsx Outdated
@coderabbitai coderabbitai Bot removed the type: bug label May 3, 2026
@opaksbyron
opaksbyron force-pushed the fix/profile-avatar-state-sync branch from 9e036a6 to be873ad Compare July 28, 2026 15:52
…d reset (closes RocketChat#38697)

Keeps the avatar editor preview in sync with form-level cancel/save/reset,
and preserves a pending avatar preview when a profile save fails.
@opaksbyron
opaksbyron force-pushed the fix/profile-avatar-state-sync branch from be873ad to aa80d5c Compare July 28, 2026 15:58
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.

Profile avatar preview and reset behave inconsistently after cancel/save cycles

1 participant