Skip to content

fix(react): memoize fetched permissions and prevent redundant store u… - #1445

Open
swarnendusahu10-dotcom wants to merge 2 commits into
RocketChat:developfrom
swarnendusahu10-dotcom:fix/issue-1317-permissions-memoization-leak
Open

swarnendusahu10-dotcom wants to merge 2 commits into
RocketChat:developfrom
swarnendusahu10-dotcom:fix/issue-1317-permissions-memoization-leak

Conversation

@swarnendusahu10-dotcom

Copy link
Copy Markdown
Contributor

Summary of Changes

Closes #1317.

In packages/react/src/hooks/useFetchChatData.js, the permissions change-detection check in fetchAndSetPermissions:

if (!permissionsRef.current || JSON.stringify(permissions) !== JSON.stringify(permissionsRef.current.raw))

intended to memoize permissions and avoid redundant store updates. However, permissionsRef.current.raw was never assigned when setting permissionsRef.current (only { map: permissionsMap } was assigned).

The Problem

  1. Broken Memoization: permissionsRef.current.raw remained undefined across all cycles.
  2. Infinite Dispatches & Re-renders: Because JSON.stringify(permissions) !== JSON.stringify(undefined) evaluated to true on every single invocation, applyPermissions() fired continuously, executing 6 separate Zustand store updates (setViewUserInfoPermissions, setDeleteMessageRoles, setDeleteOwnMessageRoles, setForceDeleteMessageRoles, setUserPinPermissions, setEditMessagePermissions) and triggering unnecessary re-renders across the component tree.
  3. Unhandled Exception: createPermissionsMap lacked an array guard on permissions.update, causing an unhandled TypeError: Cannot read properties of undefined (reading 'map') if the API returned an error or incomplete payload.

Fix

  • Saved raw: permissions inside permissionsRef.current so memoization functions as originally intended.
  • Added defensive validation (Array.isArray(permissions?.update)) in both createPermissionsMap and fetchAndSetPermissions to guard against network glitches or non-200 responses.
  • Verified with Prettier 2.8.1 and complete rollup build.

Testing & Verification

  • Initial fetch properly maps and applies permissions.
  • Subsequent fetches with identical permissions skip redundant store dispatches.
  • Server-side permission changes are detected and applied immediately.
  • Network glitches / error payloads preserve existing cache without throwing unhandled exceptions.
  • Formatted with prettier@2.8.1 matching .prettierrc (All matched files use Prettier code style!).
  • Monorepo production build passes cleanly (yarn workspace @embeddedchat/react build).

@swarnendusahu10-dotcom
swarnendusahu10-dotcom force-pushed the fix/issue-1317-permissions-memoization-leak branch from 99d5277 to fb64966 Compare November 27, 2026 10:45

This branch has not been deployed

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

Permissions change-detection in useFetchChatData is dead — applyPermissions re-runs on every call

1 participant