Skip to content

Commit

Permalink
chat panel: Fix new messages indicator appearing when message was del…
Browse files Browse the repository at this point in the history
…eted (zed-industries#10278)

Release Notes:

Fixed an issue where the "New messages" indicator in the channel chat
would be shown even if the message was deleted
  • Loading branch information
bennetbo authored Apr 10, 2024
1 parent 39e0e26 commit 26299fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 16 additions & 2 deletions crates/channel/src/channel_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,27 @@ impl ChannelChat {
let mut messages = cursor.slice(&ChannelMessageId::Saved(id), Bias::Left, &());
if let Some(item) = cursor.item() {
if item.id == ChannelMessageId::Saved(id) {
let ix = messages.summary().count;
let deleted_message_ix = messages.summary().count;
cursor.next(&());
messages.append(cursor.suffix(&()), &());
drop(cursor);
self.messages = messages;

// If the message that was deleted was the last acknowledged message,
// replace the acknowledged message with an earlier one.
self.channel_store.update(cx, |store, _| {
let summary = self.messages.summary();
if summary.count == 0 {
store.set_acknowledged_message_id(self.channel_id, None);
} else if deleted_message_ix == summary.count {
if let ChannelMessageId::Saved(id) = summary.max_id {
store.set_acknowledged_message_id(self.channel_id, Some(id));
}
}
});

cx.emit(ChannelChatEvent::MessagesUpdated {
old_range: ix..ix + 1,
old_range: deleted_message_ix..deleted_message_ix + 1,
new_count: 0,
});
}
Expand Down
6 changes: 6 additions & 0 deletions crates/channel/src/channel_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ impl ChannelStore {
.is_some_and(|state| state.has_new_messages())
}

pub fn set_acknowledged_message_id(&mut self, channel_id: ChannelId, message_id: Option<u64>) {
if let Some(state) = self.channel_states.get_mut(&channel_id) {
state.latest_chat_message = message_id;
}
}

pub fn last_acknowledge_message_id(&self, channel_id: ChannelId) -> Option<u64> {
self.channel_states.get(&channel_id).and_then(|state| {
if let Some(last_message_id) = state.latest_chat_message {
Expand Down

0 comments on commit 26299fb

Please sign in to comment.