BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / manage_team_page.rs

manage_team_page.rs

manage_team_page.rs 10.3 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::input::{Input, InputEvent, InputState};
3use gpui_kit::component::scroll::ScrollableElement as _;
4use gpui_kit::component::{h_flex, v_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7use teams_core::team_avatar_key;
8
9use super::avatar::{person_avatar, team_avatar};
10use super::widgets::icon;
11use crate::app_state::AppState;
12use crate::data::Directory;
13use crate::manage_team::{MembersLoad, TeamMember, member_groups};
14use crate::theme;
15use app_ui_kit::scroll_edge::with_top_edge;
16
17const MANAGE_TEAM_TAG: &str = "shell/islands/main/manage-team";
18const MANAGE_TEAM_SCROLL_DIVIDER: &str = "shell/islands/main/manage-team-divider-scroll";
19const HEADER_HEIGHT: f32 = 60.;
20const PAGE_PADDING: f32 = 20.;
21const TEAM_AVATAR_SIZE: f32 = 32.;
22const TEAM_AVATAR_RADIUS: f32 = 8.;
23const MEMBER_AVATAR_SIZE: f32 = 32.;
24const ROW_HEIGHT: f32 = 52.;
25const SEARCH_WIDTH: f32 = 260.;
26const SEARCH_HEIGHT: f32 = 32.;
27const ROLE_WIDTH: f32 = 96.;
28const LIST_MAX_WIDTH: f32 = 960.;
29const SEARCH_PLACEHOLDER: &str = "Search for members";
30
31pub struct ManageTeamPage {
32 app: Entity<AppState>,
33 search: Entity<InputState>,
34 owners_open: bool,
35 others_open: bool,
36 scroll: ScrollHandle,
37 _subscriptions: [Subscription; 2],
38}
39
40impl ManageTeamPage {
41 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
42 let search = cx.new(|cx| InputState::new(window, cx).placeholder(SEARCH_PLACEHOLDER));
43 let subscriptions = [
44 cx.observe(&app, |_, _, cx| cx.notify()),
45 cx.subscribe(&search, |_, _, event: &InputEvent, cx| {
46 if matches!(event, InputEvent::Change) {
47 cx.notify();
48 }
49 }),
50 ];
51 ManageTeamPage {
52 app,
53 search,
54 owners_open: true,
55 others_open: true,
56 scroll: ScrollHandle::new(),
57 _subscriptions: subscriptions,
58 }
59 }
60
61 fn header(&self, team_id: &str, team_name: &str, directory: &Directory, cx: &Context<Self>) -> Div {
62 let photo = directory.avatar(&team_avatar_key(team_id));
63 h_flex()
64 .h(px(HEADER_HEIGHT))
65 .flex_none()
66 .px(px(PAGE_PADDING))
67 .gap(px(12.))
68 .items_center()
69 .child(
70 div()
71 .id("manage-team-back")
72 .size(px(28.))
73 .flex()
74 .items_center()
75 .justify_center()
76 .rounded(px(6.))
77 .cursor_pointer()
78 .hover(|button| button.bg(theme::row_hover()))
79 .child(icon(IconName::ArrowLeft, 16., theme::text_muted()))
80 .on_click(cx.listener(|this, _, _, cx| {
81 this.app.update(cx, |state, cx| state.close_manage_team(cx));
82 })),
83 )
84 .child(team_avatar(photo, team_name, team_id, TEAM_AVATAR_SIZE, TEAM_AVATAR_RADIUS))
85 .child(
86 div()
87 .min_w_0()
88 .truncate()
89 .text_size(px(16.))
90 .font_weight(FontWeight::BOLD)
91 .text_color(theme::text())
92 .child(team_name.to_owned()),
93 )
94 .child(
95 div()
96 .ml(px(12.))
97 .h_full()
98 .flex()
99 .items_center()
100 .border_b_2()
101 .border_color(theme::accent())
102 .text_size(px(14.))
103 .font_weight(FontWeight::SEMIBOLD)
104 .text_color(theme::text())
105 .child("Members"),
106 )
107 .child(div().flex_1())
108 .child(
109 h_flex()
110 .id("manage-team-in-browser")
111 .h(px(28.))
112 .px(px(10.))
113 .gap(px(6.))
114 .items_center()
115 .rounded(px(6.))
116 .cursor_pointer()
117 .text_size(px(13.))
118 .text_color(theme::text_soft())
119 .hover(|button| button.bg(theme::row_hover()))
120 .child(icon(IconName::ExternalLink, 14., theme::text_muted()))
121 .child("Open in browser")
122 .on_click(cx.listener(|this, _, _, cx| {
123 this.app.update(cx, |state, cx| {
124 if let Some(team_id) = state.manage_team.as_ref().map(|open| open.team_id.clone()) {
125 state.manage_team_in_browser(&team_id, cx);
126 }
127 });
128 })),
129 )
130 }
131
132 fn search_box(&self) -> Div {
133 h_flex()
134 .w(px(SEARCH_WIDTH))
135 .max_w_full()
136 .h(px(SEARCH_HEIGHT))
137 .px(px(10.))
138 .gap(px(8.))
139 .items_center()
140 .rounded(px(6.))
141 .bg(theme::popup())
142 .child(icon(IconName::Search, 14., theme::text_muted()))
143 .child(div().flex_1().min_w_0().child(Input::new(&self.search).appearance(false).bordered(false)))
144 }
145
146 fn section(
147 &self,
148 id: &'static str,
149 title: &str,
150 members: &[&TeamMember],
151 open: bool,
152 directory: &Directory,
153 cx: &Context<Self>,
154 ) -> Div {
155 let toggle = h_flex()
156 .id(id)
157 .h(px(32.))
158 .flex_none()
159 .gap(px(6.))
160 .items_center()
161 .cursor_pointer()
162 .text_size(px(14.))
163 .font_weight(FontWeight::SEMIBOLD)
164 .text_color(theme::text())
165 .child(icon(if open { IconName::ChevronDown } else { IconName::ChevronRight }, 14., theme::text_muted()))
166 .child(format!("{title} ({})", members.len()))
167 .on_click(cx.listener(move |this, _, _, cx| {
168 if id == "manage-team-owners" {
169 this.owners_open = !this.owners_open;
170 } else {
171 this.others_open = !this.others_open;
172 }
173 cx.notify();
174 }));
175 v_flex()
176 .w_full()
177 .child(toggle)
178 .when(open, |section| {
179 section.children(members.iter().map(|member| member_row(member, directory)))
180 })
181 }
182}
183
184fn member_row(member: &TeamMember, directory: &Directory) -> Div {
185 h_flex()
186 .w_full()
187 .h(px(ROW_HEIGHT))
188 .flex_none()
189 .px(px(8.))
190 .gap(px(12.))
191 .items_center()
192 .rounded(px(8.))
193 .hover(|row| row.bg(theme::row_hover()))
194 .child(
195 div()
196 .flex_none()
197 .size(px(MEMBER_AVATAR_SIZE))
198 .child(person_avatar(directory, member.user_id.as_deref(), &member.name, MEMBER_AVATAR_SIZE)),
199 )
200 .child(
201 v_flex()
202 .flex_1()
203 .min_w_0()
204 .child(div().truncate().text_size(px(14.)).text_color(theme::text()).child(member.name.clone()))
205 .children(member.email.clone().map(|email| {
206 div().truncate().text_size(px(12.)).text_color(theme::text_muted()).child(email)
207 })),
208 )
209 .child(
210 div()
211 .flex_none()
212 .w(px(ROLE_WIDTH))
213 .text_size(px(13.))
214 .text_color(theme::text_soft())
215 .child(if member.owner { "Owner" } else { "Member" }),
216 )
217}
218
219fn notice(text: String) -> Div {
220 div()
221 .py(px(32.))
222 .flex()
223 .justify_center()
224 .text_size(px(13.))
225 .text_color(theme::text_muted())
226 .child(text)
227}
228
229impl Render for ManageTeamPage {
230 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
231 let query = self.search.read(cx).value().to_string();
232 let state = self.app.read(cx);
233 let Some(manage_team) = state.manage_team.as_ref() else {
234 return div().into_any_element();
235 };
236 let directory = &state.directory;
237 let body: Vec<AnyElement> = match &manage_team.members {
238 MembersLoad::Loading => vec![notice("Loading members...".to_owned()).into_any_element()],
239 MembersLoad::Failed(message) => vec![notice(message.clone()).into_any_element()],
240 MembersLoad::Loaded(members) => {
241 let groups = member_groups(members, &query);
242 if groups.owners.is_empty() && groups.others.is_empty() {
243 vec![notice(format!("No members match \"{}\"", query.trim())).into_any_element()]
244 } else {
245 vec![
246 self.section("manage-team-owners", "Owners", &groups.owners, self.owners_open, directory, cx)
247 .into_any_element(),
248 self.section("manage-team-others", "Members and guests", &groups.others, self.others_open, directory, cx)
249 .into_any_element(),
250 ]
251 }
252 }
253 };
254 v_flex()
255 .debug_selector(|| MANAGE_TEAM_TAG.into())
256 .flex_1()
257 .w_full()
258 .min_w_0()
259 .h_full()
260 .rounded(px(10.))
261 .bg(theme::surface())
262 .overflow_hidden()
263 .child(self.header(&manage_team.team_id, &manage_team.team_name, directory, cx))
264 .child(div().w_full().h(px(1.)).flex_none().bg(theme::border()))
265 .child(with_top_edge(
266 v_flex()
267 .id("manage-team-list")
268 .flex_1()
269 .min_h_0()
270 .px(px(PAGE_PADDING - 8.))
271 .py(px(12.))
272 .overflow_y_scrollbar()
273 .track_scroll(&self.scroll)
274 .child(
275 v_flex()
276 .w_full()
277 .max_w(px(LIST_MAX_WIDTH))
278 .gap(px(12.))
279 .child(h_flex().w_full().px(px(8.)).justify_end().child(self.search_box()))
280 .children(body),
281 ),
282 &self.scroll,
283 MANAGE_TEAM_SCROLL_DIVIDER,
284 ))
285 .into_any_element()
286 }
287}