Skip to content

Commit

Permalink
fix(prebuilt): 🐛 participants list showing local peer in two groups a…
Browse files Browse the repository at this point in the history
…fter role change
  • Loading branch information
stanwolverine committed Jan 8, 2024
1 parent 7bbdd69 commit 1aa1ffb
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions packages/react-native-room-kit/src/redux/reducers/hmsStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,40 @@ const hmsStatesReducer = (
let updatedGroupedParticipants = state.groupedParticipants;

if (action.localPeer !== null) {
const savedLocalPeer = Object.values(state.groupedParticipants)
.flat()
.find(
(participant) => participant.peerID === action.localPeer?.peerID
);
let previousRoleName: HMSRole['name'] | null = null;
let savedLocalPeer: HMSLocalPeer | HMSPeer | null = null;

for (const groupName in state.groupedParticipants) {
if (
Object.prototype.hasOwnProperty.call(
state.groupedParticipants,
groupName
)
) {
const participantsList = state.groupedParticipants[groupName];

if (Array.isArray(participantsList)) {
const result = participantsList.find(
(participant) => participant.peerID === action.localPeer?.peerID
);

if (result) {
previousRoleName = groupName;
savedLocalPeer = result;
break;
}
}
}
}

// update peer or check if role change happened
if (savedLocalPeer) {
const previousRoleName = savedLocalPeer.role?.name!;
const currentRoleName = action.localPeer.role?.name!;

const roleChanged = previousRoleName !== currentRoleName;
const roleChanged =
previousRoleName && previousRoleName !== currentRoleName;

if (roleChanged) {
if (roleChanged && previousRoleName) {
const previousList = state.groupedParticipants[previousRoleName];
const currentList = state.groupedParticipants[currentRoleName];

Expand Down Expand Up @@ -405,7 +425,12 @@ const hmsStatesReducer = (
let previousRoleName: HMSRole['name'] | null = null;

for (const groupName in state.groupedParticipants) {
if (Object.prototype.hasOwnProperty.call(state.groupedParticipants, groupName)) {
if (
Object.prototype.hasOwnProperty.call(
state.groupedParticipants,
groupName
)
) {
const participantsList = state.groupedParticipants[groupName];

if (Array.isArray(participantsList)) {
Expand Down Expand Up @@ -460,9 +485,7 @@ const hmsStatesReducer = (
...state.groupedParticipants,
[currentRoleName]: Array.isArray(currentList)
? currentList.map((p) =>
p.peerID === action.participant.peerID
? action.participant
: p
p.peerID === action.participant.peerID ? action.participant : p
)
: [action.participant],
},
Expand All @@ -473,7 +496,7 @@ const hmsStatesReducer = (
...state,
groupedParticipants: {
...state.groupedParticipants,
[action.roleName]: action.participants
[action.roleName]: action.participants,
},
};
}
Expand Down

0 comments on commit 1aa1ffb

Please sign in to comment.