Flesh out channel member management
Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
a2486de045
commit
87b2d599c1
13 changed files with 728 additions and 196 deletions
|
@ -1,6 +1,7 @@
|
|||
use client::{proto, ChannelId, ChannelStore, User, UserId, UserStore};
|
||||
use client::{proto, ChannelId, ChannelMembership, ChannelStore, User, UserId, UserStore};
|
||||
use fuzzy::{match_strings, StringMatchCandidate};
|
||||
use gpui::{
|
||||
actions,
|
||||
elements::*,
|
||||
platform::{CursorStyle, MouseButton},
|
||||
AppContext, Entity, ModelHandle, MouseState, Task, View, ViewContext, ViewHandle,
|
||||
|
@ -10,8 +11,12 @@ use std::sync::Arc;
|
|||
use util::TryFutureExt;
|
||||
use workspace::Modal;
|
||||
|
||||
actions!(channel_modal, [SelectNextControl, ToggleMode]);
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
Picker::<ChannelModalDelegate>::init(cx);
|
||||
cx.add_action(ChannelModal::toggle_mode);
|
||||
cx.add_action(ChannelModal::select_next_control);
|
||||
}
|
||||
|
||||
pub struct ChannelModal {
|
||||
|
@ -21,6 +26,110 @@ pub struct ChannelModal {
|
|||
has_focus: bool,
|
||||
}
|
||||
|
||||
impl ChannelModal {
|
||||
pub fn new(
|
||||
user_store: ModelHandle<UserStore>,
|
||||
channel_store: ModelHandle<ChannelStore>,
|
||||
channel_id: ChannelId,
|
||||
mode: Mode,
|
||||
members: Vec<ChannelMembership>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
cx.observe(&channel_store, |_, _, cx| cx.notify()).detach();
|
||||
let picker = cx.add_view(|cx| {
|
||||
Picker::new(
|
||||
ChannelModalDelegate {
|
||||
matching_users: Vec::new(),
|
||||
matching_member_indices: Vec::new(),
|
||||
selected_index: 0,
|
||||
user_store: user_store.clone(),
|
||||
channel_store: channel_store.clone(),
|
||||
channel_id,
|
||||
match_candidates: members
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(id, member)| StringMatchCandidate {
|
||||
id,
|
||||
string: member.user.github_login.clone(),
|
||||
char_bag: member.user.github_login.chars().collect(),
|
||||
})
|
||||
.collect(),
|
||||
members,
|
||||
mode,
|
||||
selected_column: None,
|
||||
},
|
||||
cx,
|
||||
)
|
||||
.with_theme(|theme| theme.collab_panel.channel_modal.picker.clone())
|
||||
});
|
||||
|
||||
cx.subscribe(&picker, |_, _, e, cx| cx.emit(*e)).detach();
|
||||
let has_focus = picker.read(cx).has_focus();
|
||||
|
||||
Self {
|
||||
picker,
|
||||
channel_store,
|
||||
channel_id,
|
||||
has_focus,
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_mode(&mut self, _: &ToggleMode, cx: &mut ViewContext<Self>) {
|
||||
let mode = match self.picker.read(cx).delegate().mode {
|
||||
Mode::ManageMembers => Mode::InviteMembers,
|
||||
Mode::InviteMembers => Mode::ManageMembers,
|
||||
};
|
||||
self.set_mode(mode, cx);
|
||||
}
|
||||
|
||||
fn set_mode(&mut self, mode: Mode, cx: &mut ViewContext<Self>) {
|
||||
let channel_store = self.channel_store.clone();
|
||||
let channel_id = self.channel_id;
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
if mode == Mode::ManageMembers {
|
||||
let members = channel_store
|
||||
.update(&mut cx, |channel_store, cx| {
|
||||
channel_store.get_channel_member_details(channel_id, cx)
|
||||
})
|
||||
.await?;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.picker
|
||||
.update(cx, |picker, _| picker.delegate_mut().members = members);
|
||||
})?;
|
||||
}
|
||||
|
||||
this.update(&mut cx, |this, cx| {
|
||||
this.picker.update(cx, |picker, cx| {
|
||||
let delegate = picker.delegate_mut();
|
||||
delegate.mode = mode;
|
||||
picker.update_matches(picker.query(cx), cx);
|
||||
cx.notify()
|
||||
});
|
||||
})
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn select_next_control(&mut self, _: &SelectNextControl, cx: &mut ViewContext<Self>) {
|
||||
self.picker.update(cx, |picker, cx| {
|
||||
let delegate = picker.delegate_mut();
|
||||
match delegate.mode {
|
||||
Mode::ManageMembers => match delegate.selected_column {
|
||||
Some(UserColumn::Remove) => {
|
||||
delegate.selected_column = Some(UserColumn::ToggleAdmin)
|
||||
}
|
||||
Some(UserColumn::ToggleAdmin) => {
|
||||
delegate.selected_column = Some(UserColumn::Remove)
|
||||
}
|
||||
None => todo!(),
|
||||
},
|
||||
Mode::InviteMembers => {}
|
||||
}
|
||||
cx.notify()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for ChannelModal {
|
||||
type Event = PickerEvent;
|
||||
}
|
||||
|
@ -60,11 +169,7 @@ impl View for ChannelModal {
|
|||
})
|
||||
.on_click(MouseButton::Left, move |_, this, cx| {
|
||||
if !active {
|
||||
this.picker.update(cx, |picker, cx| {
|
||||
picker.delegate_mut().mode = mode;
|
||||
picker.update_matches(picker.query(cx), cx);
|
||||
cx.notify();
|
||||
})
|
||||
this.set_mode(mode, cx);
|
||||
}
|
||||
})
|
||||
.with_cursor_style(if active {
|
||||
|
@ -125,65 +230,29 @@ impl Modal for ChannelModal {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn build_channel_modal(
|
||||
user_store: ModelHandle<UserStore>,
|
||||
channel_store: ModelHandle<ChannelStore>,
|
||||
channel_id: ChannelId,
|
||||
mode: Mode,
|
||||
members: Vec<(Arc<User>, proto::channel_member::Kind)>,
|
||||
cx: &mut ViewContext<ChannelModal>,
|
||||
) -> ChannelModal {
|
||||
let picker = cx.add_view(|cx| {
|
||||
Picker::new(
|
||||
ChannelModalDelegate {
|
||||
matches: Vec::new(),
|
||||
selected_index: 0,
|
||||
user_store: user_store.clone(),
|
||||
channel_store: channel_store.clone(),
|
||||
channel_id,
|
||||
match_candidates: members
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(id, member)| StringMatchCandidate {
|
||||
id,
|
||||
string: member.0.github_login.clone(),
|
||||
char_bag: member.0.github_login.chars().collect(),
|
||||
})
|
||||
.collect(),
|
||||
members,
|
||||
mode,
|
||||
},
|
||||
cx,
|
||||
)
|
||||
.with_theme(|theme| theme.collab_panel.channel_modal.picker.clone())
|
||||
});
|
||||
|
||||
cx.subscribe(&picker, |_, _, e, cx| cx.emit(*e)).detach();
|
||||
let has_focus = picker.read(cx).has_focus();
|
||||
|
||||
ChannelModal {
|
||||
picker,
|
||||
channel_store,
|
||||
channel_id,
|
||||
has_focus,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum Mode {
|
||||
ManageMembers,
|
||||
InviteMembers,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum UserColumn {
|
||||
ToggleAdmin,
|
||||
Remove,
|
||||
}
|
||||
|
||||
pub struct ChannelModalDelegate {
|
||||
matches: Vec<(Arc<User>, Option<proto::channel_member::Kind>)>,
|
||||
matching_users: Vec<Arc<User>>,
|
||||
matching_member_indices: Vec<usize>,
|
||||
user_store: ModelHandle<UserStore>,
|
||||
channel_store: ModelHandle<ChannelStore>,
|
||||
channel_id: ChannelId,
|
||||
selected_index: usize,
|
||||
mode: Mode,
|
||||
selected_column: Option<UserColumn>,
|
||||
match_candidates: Arc<[StringMatchCandidate]>,
|
||||
members: Vec<(Arc<User>, proto::channel_member::Kind)>,
|
||||
members: Vec<ChannelMembership>,
|
||||
}
|
||||
|
||||
impl PickerDelegate for ChannelModalDelegate {
|
||||
|
@ -192,7 +261,10 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
}
|
||||
|
||||
fn match_count(&self) -> usize {
|
||||
self.matches.len()
|
||||
match self.mode {
|
||||
Mode::ManageMembers => self.matching_member_indices.len(),
|
||||
Mode::InviteMembers => self.matching_users.len(),
|
||||
}
|
||||
}
|
||||
|
||||
fn selected_index(&self) -> usize {
|
||||
|
@ -201,6 +273,10 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
|
||||
fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Picker<Self>>) {
|
||||
self.selected_index = ix;
|
||||
self.selected_column = match self.mode {
|
||||
Mode::ManageMembers => Some(UserColumn::ToggleAdmin),
|
||||
Mode::InviteMembers => None,
|
||||
};
|
||||
}
|
||||
|
||||
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
|
||||
|
@ -220,11 +296,10 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
.await;
|
||||
picker.update(&mut cx, |picker, cx| {
|
||||
let delegate = picker.delegate_mut();
|
||||
delegate.matches.clear();
|
||||
delegate.matches.extend(matches.into_iter().map(|m| {
|
||||
let member = &delegate.members[m.candidate_id];
|
||||
(member.0.clone(), Some(member.1))
|
||||
}));
|
||||
delegate.matching_member_indices.clear();
|
||||
delegate
|
||||
.matching_member_indices
|
||||
.extend(matches.into_iter().map(|m| m.candidate_id));
|
||||
cx.notify();
|
||||
})?;
|
||||
anyhow::Ok(())
|
||||
|
@ -242,10 +317,7 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
let users = search_users.await?;
|
||||
picker.update(&mut cx, |picker, cx| {
|
||||
let delegate = picker.delegate_mut();
|
||||
delegate.matches.clear();
|
||||
delegate
|
||||
.matches
|
||||
.extend(users.into_iter().map(|user| (user, None)));
|
||||
delegate.matching_users = users;
|
||||
cx.notify();
|
||||
})?;
|
||||
anyhow::Ok(())
|
||||
|
@ -258,29 +330,23 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
}
|
||||
|
||||
fn confirm(&mut self, _: bool, cx: &mut ViewContext<Picker<Self>>) {
|
||||
if let Some((user, _)) = self.matches.get(self.selected_index) {
|
||||
match self.mode {
|
||||
Mode::ManageMembers => self
|
||||
.channel_store
|
||||
.update(cx, |store, cx| {
|
||||
store.remove_member(self.channel_id, user.id, cx)
|
||||
})
|
||||
.detach(),
|
||||
Mode::InviteMembers => match self.member_status(user.id, cx) {
|
||||
Some(proto::channel_member::Kind::Member) => {}
|
||||
Some(proto::channel_member::Kind::Invitee) => self
|
||||
.channel_store
|
||||
if let Some((selected_user, admin)) = self.user_at_index(self.selected_index) {
|
||||
match self.member_status(selected_user.id, cx) {
|
||||
Some(proto::channel_member::Kind::Member)
|
||||
| Some(proto::channel_member::Kind::Invitee) => {
|
||||
if self.selected_column == Some(UserColumn::ToggleAdmin) {
|
||||
self.set_member_admin(selected_user.id, !admin.unwrap_or(false), cx);
|
||||
} else {
|
||||
self.remove_member(selected_user.id, cx);
|
||||
}
|
||||
}
|
||||
Some(proto::channel_member::Kind::AncestorMember) | None => {
|
||||
self.channel_store
|
||||
.update(cx, |store, cx| {
|
||||
store.remove_member(self.channel_id, user.id, cx)
|
||||
store.invite_member(self.channel_id, selected_user.id, false, cx)
|
||||
})
|
||||
.detach(),
|
||||
Some(proto::channel_member::Kind::AncestorMember) | None => self
|
||||
.channel_store
|
||||
.update(cx, |store, cx| {
|
||||
store.invite_member(self.channel_id, user.id, false, cx)
|
||||
})
|
||||
.detach(),
|
||||
},
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -297,17 +363,9 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
cx: &gpui::AppContext,
|
||||
) -> AnyElement<Picker<Self>> {
|
||||
let theme = &theme::current(cx).collab_panel.channel_modal;
|
||||
let (user, _) = &self.matches[ix];
|
||||
let (user, admin) = self.user_at_index(ix).unwrap();
|
||||
let request_status = self.member_status(user.id, cx);
|
||||
|
||||
let icon_path = match request_status {
|
||||
Some(proto::channel_member::Kind::AncestorMember) => Some("icons/check_8.svg"),
|
||||
Some(proto::channel_member::Kind::Member) => Some("icons/check_8.svg"),
|
||||
Some(proto::channel_member::Kind::Invitee) => Some("icons/x_mark_8.svg"),
|
||||
None => None,
|
||||
};
|
||||
let button_style = &theme.contact_button;
|
||||
|
||||
let style = theme.picker.item.in_state(selected).style_for(mouse_state);
|
||||
Flex::row()
|
||||
.with_children(user.avatar.clone().map(|avatar| {
|
||||
|
@ -323,20 +381,69 @@ impl PickerDelegate for ChannelModalDelegate {
|
|||
.aligned()
|
||||
.left(),
|
||||
)
|
||||
.with_children(icon_path.map(|icon_path| {
|
||||
Svg::new(icon_path)
|
||||
.with_color(button_style.color)
|
||||
.constrained()
|
||||
.with_width(button_style.icon_width)
|
||||
.aligned()
|
||||
.with_children(admin.map(|admin| {
|
||||
let member_style = theme.admin_toggle_part.in_state(!admin);
|
||||
let admin_style = theme.admin_toggle_part.in_state(admin);
|
||||
Flex::row()
|
||||
.with_child(
|
||||
Label::new("member", member_style.text.clone())
|
||||
.contained()
|
||||
.with_style(member_style.container),
|
||||
)
|
||||
.with_child(
|
||||
Label::new("admin", admin_style.text.clone())
|
||||
.contained()
|
||||
.with_style(admin_style.container),
|
||||
)
|
||||
.contained()
|
||||
.with_style(button_style.container)
|
||||
.constrained()
|
||||
.with_width(button_style.button_width)
|
||||
.with_height(button_style.button_width)
|
||||
.with_style(theme.admin_toggle)
|
||||
.aligned()
|
||||
.flex_float()
|
||||
}))
|
||||
.with_children({
|
||||
match self.mode {
|
||||
Mode::ManageMembers => match request_status {
|
||||
Some(proto::channel_member::Kind::Member) => Some(
|
||||
Label::new("remove member", theme.remove_member_button.text.clone())
|
||||
.contained()
|
||||
.with_style(theme.remove_member_button.container)
|
||||
.into_any(),
|
||||
),
|
||||
Some(proto::channel_member::Kind::Invitee) => Some(
|
||||
Label::new("cancel invite", theme.cancel_invite_button.text.clone())
|
||||
.contained()
|
||||
.with_style(theme.cancel_invite_button.container)
|
||||
.into_any(),
|
||||
),
|
||||
Some(proto::channel_member::Kind::AncestorMember) | None => None,
|
||||
},
|
||||
Mode::InviteMembers => {
|
||||
let svg = match request_status {
|
||||
Some(proto::channel_member::Kind::Member) => Some(
|
||||
Svg::new("icons/check_8.svg")
|
||||
.with_color(theme.member_icon.color)
|
||||
.constrained()
|
||||
.with_width(theme.member_icon.width)
|
||||
.aligned()
|
||||
.contained()
|
||||
.with_style(theme.member_icon.container),
|
||||
),
|
||||
Some(proto::channel_member::Kind::Invitee) => Some(
|
||||
Svg::new("icons/check_8.svg")
|
||||
.with_color(theme.invitee_icon.color)
|
||||
.constrained()
|
||||
.with_width(theme.invitee_icon.width)
|
||||
.aligned()
|
||||
.contained()
|
||||
.with_style(theme.invitee_icon.container),
|
||||
),
|
||||
Some(proto::channel_member::Kind::AncestorMember) | None => None,
|
||||
};
|
||||
|
||||
svg.map(|svg| svg.aligned().flex_float().into_any())
|
||||
}
|
||||
}
|
||||
})
|
||||
.contained()
|
||||
.with_style(style.container)
|
||||
.constrained()
|
||||
|
@ -353,11 +460,56 @@ impl ChannelModalDelegate {
|
|||
) -> Option<proto::channel_member::Kind> {
|
||||
self.members
|
||||
.iter()
|
||||
.find_map(|(user, status)| (user.id == user_id).then_some(*status))
|
||||
.find_map(|membership| (membership.user.id == user_id).then_some(membership.kind))
|
||||
.or(self
|
||||
.channel_store
|
||||
.read(cx)
|
||||
.has_pending_channel_invite(self.channel_id, user_id)
|
||||
.then_some(proto::channel_member::Kind::Invitee))
|
||||
}
|
||||
|
||||
fn user_at_index(&self, ix: usize) -> Option<(Arc<User>, Option<bool>)> {
|
||||
match self.mode {
|
||||
Mode::ManageMembers => self.matching_member_indices.get(ix).and_then(|ix| {
|
||||
let channel_membership = self.members.get(*ix)?;
|
||||
Some((
|
||||
channel_membership.user.clone(),
|
||||
Some(channel_membership.admin),
|
||||
))
|
||||
}),
|
||||
Mode::InviteMembers => Some((self.matching_users.get(ix).cloned()?, None)),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_member_admin(&mut self, user_id: u64, admin: bool, cx: &mut ViewContext<Picker<Self>>) {
|
||||
let update = self.channel_store.update(cx, |store, cx| {
|
||||
store.set_member_admin(self.channel_id, user_id, admin, cx)
|
||||
});
|
||||
cx.spawn(|picker, mut cx| async move {
|
||||
update.await?;
|
||||
picker.update(&mut cx, |picker, cx| {
|
||||
let this = picker.delegate_mut();
|
||||
if let Some(member) = this.members.iter_mut().find(|m| m.user.id == user_id) {
|
||||
member.admin = admin;
|
||||
}
|
||||
})
|
||||
})
|
||||
.detach_and_log_err(cx);
|
||||
}
|
||||
|
||||
fn remove_member(&mut self, user_id: u64, cx: &mut ViewContext<Picker<Self>>) {
|
||||
let update = self.channel_store.update(cx, |store, cx| {
|
||||
store.remove_member(self.channel_id, user_id, cx)
|
||||
});
|
||||
cx.spawn(|picker, mut cx| async move {
|
||||
update.await?;
|
||||
picker.update(&mut cx, |picker, cx| {
|
||||
let this = picker.delegate_mut();
|
||||
if let Some(ix) = this.members.iter_mut().position(|m| m.user.id == user_id) {
|
||||
this.members.remove(ix);
|
||||
}
|
||||
})
|
||||
})
|
||||
.detach_and_log_err(cx);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue