Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
132 changes: 128 additions & 4 deletions crates/tinyclient/src/gui/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use gpui::{
use history::History;
use pages::Pages;
use protocol::{
AccountKey, Badge, CallSupport, Conversation, ConversationKind, InboxCounts, InboxFilter, Key, Link, Network,
AccountKey, Badge, CallSupport, Conversation, ConversationKind, GroupKind, InboxCounts, InboxFilter, Key, Link, Network,
Occupants, Request, Response, PAGE_LIMIT,
};
use std::collections::{HashMap, HashSet};
Expand All @@ -53,6 +53,19 @@ const WATCH: &str = "sidebar";
/// Drafts remembered ahead of their rows being read again.
const DRAFTS_HELD: usize = 256;

#[derive(Clone, PartialEq, Eq, Debug)]
struct MovedSlackSection {
space: Key,
id: String,
}

#[derive(Clone, PartialEq, Eq, Debug)]
struct MovedMattermostGroup {
space: Key,
id: String,
name: String,
}

/// The id to ask before on the next mention page. `None` when this page is the last.
/// A page that was filtered down to nothing still moves on, using the raw page's last id.
fn mention_cursor(items: &[protocol::MentionHit], more: bool, cursor: Option<String>) -> Option<String> {
Expand Down Expand Up @@ -187,6 +200,8 @@ pub struct State {
/// Groups the user opened or closed against how they start, and what they revealed.
opened: HashSet<String>,
closed: HashSet<String>,
/// Mattermost's displayed team groups have no server-side reorder API.
mattermost_group_order: HashMap<Key, Vec<String>>,
quiet_shown: HashSet<String>,
threads_shown: HashSet<Key>,
muted_shown: HashSet<Key>,
Expand Down Expand Up @@ -505,6 +520,7 @@ impl State {
spaces_loaded: false,
opened: HashSet::new(),
closed: HashSet::new(),
mattermost_group_order: HashMap::new(),
quiet_shown: HashSet::new(),
threads_shown: HashSet::new(),
muted_shown: HashSet::new(),
Expand Down Expand Up @@ -963,7 +979,23 @@ impl Shell {
threads_shown: Some(&state.threads_shown),
unread: state.home_unread && matches!(space.network, Network::Slack | Network::Mattermost),
};
tree::flatten(space, &tree.groups, &tree.conversations, self.open.as_ref(), depth, &rules)
if space.network == Network::Mattermost {
let mut groups = tree.groups.clone();
if let Some(order) = state.mattermost_group_order.get(space) {
groups.sort_by_key(|group| {
(
order.iter().position(|id| id == &group.id).unwrap_or(usize::MAX),
group.position,
)
});
for (position, group) in groups.iter_mut().enumerate() {
group.position = position as i32;
}
}
tree::flatten(space, &groups, &tree.conversations, self.open.as_ref(), depth, &rules)
} else {
tree::flatten(space, &tree.groups, &tree.conversations, self.open.as_ref(), depth, &rules)
}
}
None => vec![Row::Loading { depth }; 6],
};
Expand Down Expand Up @@ -2117,7 +2149,7 @@ impl Shell {
.when(can_moderate && channel.rights.voice_move && !phone, |row| row
.on_drag(drag, |drag, _, _, cx| cx.new(|_| VoiceMovePreview(drag.name.clone().into()))))
}
Row::Group { space, id, name, collapsed, muted, rollup, depth, .. } => {
Row::Group { space, id, name, kind, collapsed, muted, rollup, depth, .. } => {
let lit = *collapsed && (rollup.attention > 0 || rollup.unread);
let dot = rollup_badge(rollup.attention, rollup.unread).filter(|_| *collapsed);
let chevron = if *collapsed { "icons/chevron-right.svg" } else { "icons/chevron-down.svg" };
Expand All @@ -2128,8 +2160,20 @@ impl Shell {
let menu_id = id.clone();
let menu_name = name.clone();
let menu_muted = *muted;
let discord = space.network == Network::Discord;
let phone = super::phone::active();
let discord = space.network == Network::Discord;
let slack = !phone
&& space.network == Network::Slack
&& *kind == GroupKind::Section
&& id != tree::APPS_GROUP
&& id != protocol::STARS_SECTION;
let moved_section = MovedSlackSection { space: space.clone(), id: id.clone() };
let mattermost = !phone && space.network == Network::Mattermost;
let moved_mattermost_group = MovedMattermostGroup { space: space.clone(), id: id.clone(), name: name.clone() };
let drop_space = space.clone();
let drop_id = id.clone();
let mattermost_drop_space = space.clone();
let mattermost_drop_id = id.clone();
let add_btn = can_create.then(|| {
div()
.id((gpui::ElementId::from("group-add"), id.clone()))
Expand Down Expand Up @@ -2185,6 +2229,22 @@ impl Shell {
}),
)
})
.when(slack, |row| row
.on_drag(moved_section, |dragged, _, _, cx| cx.new(|_| home_view::MoveGhost(SharedString::from(dragged.id.clone()))))
.drag_over::<MovedSlackSection>(|style, _, _, _| style.bg(theme::accent_soft()))
.on_drop(cx.listener(move |this, dragged: &MovedSlackSection, _, cx| {
if dragged.space == drop_space {
this.move_slack_section(drop_space.clone(), dragged.id.clone(), drop_id.clone(), cx);
}
})))
.when(mattermost, |row| row
.on_drag(moved_mattermost_group, |dragged, _, _, cx| cx.new(|_| home_view::MoveGhost(SharedString::from(dragged.name.clone()))))
.drag_over::<MovedMattermostGroup>(|style, _, _, _| style.bg(theme::accent_soft()))
.on_drop(cx.listener(move |this, dragged: &MovedMattermostGroup, _, cx| {
if dragged.space == mattermost_drop_space {
this.move_mattermost_group(mattermost_drop_space.clone(), dragged.id.clone(), mattermost_drop_id.clone(), cx);
}
})))
}
Row::Conversation { conversation: c, depth } => self.place_row(c, *depth, focused, cx),
}
Expand Down Expand Up @@ -3145,6 +3205,70 @@ impl Shell {
);
}

/// Reorders Slack's custom sidebar sections (`Request::SectionReorder`).
fn move_slack_section(&mut self, space: Key, moved: String, target: String, cx: &mut Context<Self>) {
if moved == target {
return;
}
let Some(tree) = self.trees.get(&space) else {
return;
};
let mut groups: Vec<_> = tree.groups.iter().filter(|group| group.kind == GroupKind::Section).collect();
groups.sort_by_key(|group| group.position);
let stars_present = groups.iter().any(|group| group.id == protocol::STARS_SECTION);
let mut section_ids: Vec<String> = groups
.into_iter()
.filter(|group| group.id != protocol::STARS_SECTION)
.map(|group| group.id.clone())
.collect();
if stars_present {
section_ids.insert(0, protocol::STARS_SECTION.to_owned());
}
let (Some(from), Some(to)) = (
section_ids.iter().position(|id| id == &moved),
section_ids.iter().position(|id| id == &target),
) else {
return;
};
section_ids.swap(from, to);
let request = Request::SectionReorder { space, section_ids };
self.off_thread(
cx,
move || crate::client::ask(&request),
|this, answer, cx| {
if let Some(why) = refused(&answer) {
this.toast(None, why, cx);
}
},
);
}

/// Reorders the synthetic team groups in Mattermost's workspace locally.
fn move_mattermost_group(&mut self, space: Key, moved: String, target: String, cx: &mut Context<Self>) {
if moved == target {
return;
}
let Some(tree) = self.trees.get(&space) else {
return;
};
let mut order = self.sidebar_state.mattermost_group_order.get(&space).cloned().unwrap_or_else(|| {
let mut groups: Vec<_> = tree.groups.iter().collect();
groups.sort_by_key(|group| group.position);
groups.into_iter().map(|group| group.id.clone()).collect()
});
for group in &tree.groups {
if !order.contains(&group.id) {
order.push(group.id.clone());
}
}
let (Some(from), Some(to)) = (order.iter().position(|id| id == &moved), order.iter().position(|id| id == &target)) else {
return;
};
order.swap(from, to);
self.sidebar_state.mattermost_group_order.insert(space, order);
self.rebuild_rows(cx);
}

/// The Slack or Mattermost header's menu on a phone: the same rows as a bottom sheet over the screen.
pub(in crate::gui) fn phone_slack_sheet(&self, window: &Window, cx: &mut Context<Self>) -> Option<Div> {
let body = self.slack_menu_body(cx).or_else(|| self.mattermost_menu_body(cx))?;
Expand Down
Loading
Loading