Skip to content

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

Description

@yash113gadia

Description

fetchAndSetPermissions in packages/react/src/hooks/useFetchChatData.js is meant to skip re-applying permissions when they haven't changed. It guards the work with:

if (
  !permissionsRef.current ||
  JSON.stringify(permissions) !== JSON.stringify(permissionsRef.current.raw)
) {
  const permissionsMap = createPermissionsMap(permissions);
  permissionsRef.current = {
    map: permissionsMap,   // <-- `.raw` is never stored
  };
  applyPermissions(permissionsMap);
}
return permissionsRef.current.map;

But permissionsRef.current is only ever assigned { map: permissionsMap } — the .raw property the comparison reads is never written anywhere (grep confirms line 105 is the only reference to .raw).

Effect

permissionsRef.current.raw is always undefined, so JSON.stringify(permissions) !== JSON.stringify(undefined) is always true. The guard never short-circuits, so on every call the permission map is rebuilt and applyPermissions runs — firing all six zustand setters (setViewUserInfoPermissions, setDeleteMessageRoles, setDeleteOwnMessageRoles, setForceDeleteMessageRoles, setUserPinPermissions, setEditMessagePermissions) — even when permissions are unchanged. The intended change-detection / caching is effectively dead, causing redundant state updates and re-renders.

Fix

Store the raw permissions alongside the map so the comparison has something to compare against:

permissionsRef.current = {
  raw: permissions,
  map: permissionsMap,
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions