Allow people to leave channels

Co-Authored-By: Max <max@zed.dev>
This commit is contained in:
Conrad Irwin 2024-01-26 11:38:13 -07:00
parent 8bc105ca1d
commit fd5994bc0a
5 changed files with 71 additions and 5 deletions

View file

@ -1122,7 +1122,9 @@ impl CollabPanel {
}),
);
let mut has_destructive_actions = false;
if self.channel_store.read(cx).is_channel_admin(channel_id) {
has_destructive_actions = true;
context_menu = context_menu
.separator()
.entry(
@ -1194,6 +1196,17 @@ impl CollabPanel {
);
}
if self.channel_store.read(cx).is_root_channel(channel_id) {
if !has_destructive_actions {
context_menu = context_menu.separator()
}
context_menu = context_menu.entry(
"Leave Channel",
None,
cx.handler_for(&this, move |this, cx| this.leave_channel(channel_id, cx)),
);
}
context_menu
});
@ -1667,6 +1680,34 @@ impl CollabPanel {
.detach();
}
fn leave_channel(&self, channel_id: ChannelId, cx: &mut ViewContext<Self>) {
let Some(user_id) = self.user_store.read(cx).current_user().map(|u| u.id) else {
return;
};
let Some(channel) = self.channel_store.read(cx).channel_for_id(channel_id) else {
return;
};
let prompt_message = format!("Are you sure you want to leave \"#{}\"?", channel.name);
let answer = cx.prompt(
PromptLevel::Warning,
&prompt_message,
None,
&["Leave", "Cancel"],
);
cx.spawn(|this, mut cx| async move {
if answer.await? != 0 {
return Ok(());
}
this.update(&mut cx, |this, cx| {
this.channel_store.update(cx, |channel_store, cx| {
channel_store.remove_member(channel_id, user_id, cx)
})
})?
.await
})
.detach_and_prompt_err("Failed to leave channel", cx, |_, _| None)
}
fn remove_channel(&mut self, channel_id: ChannelId, cx: &mut ViewContext<Self>) {
let channel_store = self.channel_store.clone();
if let Some(channel) = channel_store.read(cx).channel_for_id(channel_id) {