Skip to content

Commit

Permalink
manage channels: fix issue w/updating local state after adding/deleti…
Browse files Browse the repository at this point in the history
…ng channels
  • Loading branch information
patosullivan committed Jan 13, 2025
1 parent d4c5e11 commit d953ffb
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,40 @@ export function ManageChannelsScreenView({
type: c.type,
})),
}));

// Only update if the total number of sections have changed
const currentTotalChannels = sections.reduce(
(acc, section) => acc + section.channels.length,
0
);
const newTotalChannels = newNavSections.reduce(
(acc, section) => acc + section.channels.length,
0
);

// Only update local state if the total number of sections have changed
// OR if a newly created channel has been added to the default section
// OR if a channel has been deleted
if (newNavSections.length === sections.length) {
if (newTotalChannels !== currentTotalChannels) {
// Check if a new channel has been added to the default section
if (
newNavSections.some(
(s) =>
s.id === 'default' &&
s.channels.length >
sections.find((s) => s.id === 'default')!.channels.length
)
) {
setSections(newNavSections);
}
// Check if a channel has been deleted
if (newTotalChannels < currentTotalChannels) {
console.log('Channel deleted');
setSections(newNavSections);
}
}

// No changes to the number of sections or channels. No-op because
// we don't want to re-render the UI unnecessarily
return;
}

Expand Down

0 comments on commit d953ffb

Please sign in to comment.