Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug-fix: when other user leaves, do not remove channel #937

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/modules/ChannelList/dux/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,36 @@ export default function channelListReducer(
.with({ type: channelListActions.ON_USER_LEFT }, (action) => {
const { channel, isMe } = action.payload;
const { allChannels, currentUserId, currentChannel, channelListQuery, disableAutoSelect } = state;
let nextChannels = allChannels.filter((ch) => ch.url !== channel.url);
Copy link
Author

@aldenquimby aldenquimby Jan 22, 2024

Choose a reason for hiding this comment

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

The diff here is worse than I'd like, and could be reduced if we want, but I found it much easier to reason about what was happening with an explicit top-level isMe if/else

Examples:

  1. if I left, we don't need to run filterChannelListParams - we know we need to remove the channel
  2. if someone else left, we don't need to touch currentChannel

let nextChannel = null;
if (channelListQuery) {
if (filterChannelListParams(channelListQuery, channel, currentUserId)) {
// Good to [add to/keep in] the ChannelList
nextChannels = getChannelsWithUpsertedChannel(allChannels, channel);
}
}
// Replace the currentChannel if I left the currentChannel

// If I left: remove from allChannels and replace the currentChannel if needed
if (isMe) {
nextChannel = getNextChannel({
const nextChannels = allChannels.filter((ch) => ch.url !== channel.url);
const nextChannel = getNextChannel({
channel,
currentChannel,
allChannels,
disableAutoSelect,
});

return {
...state,
currentChannel: nextChannel,
allChannels: nextChannels,
};
}
// If someone else left: update allChannels if needed (don't touch currentChannel)
else {
let nextChannels = allChannels.filter((ch) => ch.url !== channel.url);
if (!channelListQuery || filterChannelListParams(channelListQuery, channel, currentUserId)) {
Copy link
Author

Choose a reason for hiding this comment

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

There's a change in logic here... if no channelListQuery, we should not be removing the channel (but the prior logic was incorrectly removing the channel)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes you are correct, thanks for spotting this out.

// Good to [add to/keep in] the ChannelList
nextChannels = getChannelsWithUpsertedChannel(allChannels, channel);
}

return {
...state,
allChannels: nextChannels,
}
}
return {
...state,
currentChannel: nextChannel,
allChannels: nextChannels,
};
})
.with(
{
Expand Down
2 changes: 1 addition & 1 deletion src/ui/ChannelAvatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function ChannelAvatar({
alt={channel?.name}
/>
)
), [channel?.members, channel?.coverUrl, theme]);
), [utils.getChannelAvatarSource(channel, userId), theme]);
Copy link
Author

@aldenquimby aldenquimby Jan 22, 2024

Choose a reason for hiding this comment

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

Problem with prior memoization - when a user leaves a group, the channel.members array appears to be the same object reference, so even though there is 1 less member, the memoization doesn't refire even though it should

This manifested as a bug: a member who just left still accidentally appears in the ChannelAvatar (both on ChannelListUI and ChannelUI) until a page refresh occurs

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes correct! thanks for spotting this as well! React does not recognize changes of instance properties. We will apply your sugestion as well!

return (
<>{ memoizedAvatar }</>
);
Expand Down