BlueGit

ict rusty-teams

Files

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

manage_team_page.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
manage_team_page.rs 11.7 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(
62 &self,
63 team_id: &str,
64 team_name: &str,
65 directory: &Directory,
66 cx: &Context<Self>,
67 ) -> Div {
68 let photo = directory.avatar(&team_avatar_key(team_id));
69 h_flex()
70 .h(px(HEADER_HEIGHT))
71 .flex_none()
72 .px(px(PAGE_PADDING))
73 .gap(px(12.))
74 .items_center()
75 .child(
76 div()
77 .id("manage-team-back")
78 .size(px(28.))
79 .flex()
80 .items_center()
81 .justify_center()
82 .rounded(px(6.))
83 .cursor_pointer()
84 .hover(|button| button.bg(theme::row_hover()))
85 .child(icon(IconName::ArrowLeft, 16., theme::text_muted()))
86 .on_click(cx.listener(|this, _, _, cx| {
87 this.app.update(cx, |state, cx| state.close_manage_team(cx));
88 })),
89 )
90 .child(team_avatar(
91 photo,
92 team_name,
93 team_id,
94 TEAM_AVATAR_SIZE,
95 TEAM_AVATAR_RADIUS,
96 ))
97 .child(
98 div()
99 .min_w_0()
100 .truncate()
101 .text_size(px(16.))
102 .font_weight(FontWeight::BOLD)
103 .text_color(theme::text())
104 .child(team_name.to_owned()),
105 )
106 .child(
107 div()
108 .ml(px(12.))
109 .h_full()
110 .flex()
111 .items_center()
112 .border_b_2()
113 .border_color(theme::accent())
114 .text_size(px(14.))
115 .font_weight(FontWeight::SEMIBOLD)
116 .text_color(theme::text())
117 .child("Members"),
118 )
119 .child(div().flex_1())
120 .child(
121 h_flex()
122 .id("manage-team-in-browser")
123 .h(px(28.))
124 .px(px(10.))
125 .gap(px(6.))
126 .items_center()
127 .rounded(px(6.))
128 .cursor_pointer()
129 .text_size(px(13.))
130 .text_color(theme::text_soft())
131 .hover(|button| button.bg(theme::row_hover()))
132 .child(icon(IconName::ExternalLink, 14., theme::text_muted()))
133 .child("Open in browser")
134 .on_click(cx.listener(|this, _, _, cx| {
135 this.app.update(cx, |state, cx| {
136 if let Some(team_id) =
137 state.manage_team.as_ref().map(|open| open.team_id.clone())
138 {
139 state.manage_team_in_browser(&team_id, cx);
140 }
141 });
142 })),
143 )
144 }
145
146 fn search_box(&self) -> Div {
147 h_flex()
148 .w(px(SEARCH_WIDTH))
149 .max_w_full()
150 .h(px(SEARCH_HEIGHT))
151 .px(px(10.))
152 .gap(px(8.))
153 .items_center()
154 .rounded(px(6.))
155 .bg(theme::popup())
156 .child(icon(IconName::Search, 14., theme::text_muted()))
157 .child(
158 div()
159 .flex_1()
160 .min_w_0()
161 .child(Input::new(&self.search).appearance(false).bordered(false)),
162 )
163 }
164
165 fn section(
166 &self,
167 id: &'static str,
168 title: &str,
169 members: &[&TeamMember],
170 open: bool,
171 directory: &Directory,
172 cx: &Context<Self>,
173 ) -> Div {
174 let toggle = h_flex()
175 .id(id)
176 .h(px(32.))
177 .flex_none()
178 .gap(px(6.))
179 .items_center()
180 .cursor_pointer()
181 .text_size(px(14.))
182 .font_weight(FontWeight::SEMIBOLD)
183 .text_color(theme::text())
184 .child(icon(
185 if open {
186 IconName::ChevronDown
187 } else {
188 IconName::ChevronRight
189 },
190 14.,
191 theme::text_muted(),
192 ))
193 .child(format!("{title} ({})", members.len()))
194 .on_click(cx.listener(move |this, _, _, cx| {
195 if id == "manage-team-owners" {
196 this.owners_open = !this.owners_open;
197 } else {
198 this.others_open = !this.others_open;
199 }
200 cx.notify();
201 }));
202 v_flex().w_full().child(toggle).when(open, |section| {
203 section.children(members.iter().map(|member| member_row(member, directory)))
204 })
205 }
206}
207
208fn member_row(member: &TeamMember, directory: &Directory) -> Div {
209 h_flex()
210 .w_full()
211 .h(px(ROW_HEIGHT))
212 .flex_none()
213 .px(px(8.))
214 .gap(px(12.))
215 .items_center()
216 .rounded(px(8.))
217 .hover(|row| row.bg(theme::row_hover()))
218 .child(
219 div()
220 .flex_none()
221 .size(px(MEMBER_AVATAR_SIZE))
222 .child(person_avatar(
223 directory,
224 member.user_id.as_deref(),
225 &member.name,
226 MEMBER_AVATAR_SIZE,
227 )),
228 )
229 .child(
230 v_flex()
231 .flex_1()
232 .min_w_0()
233 .child(
234 div()
235 .truncate()
236 .text_size(px(14.))
237 .text_color(theme::text())
238 .child(member.name.clone()),
239 )
240 .children(member.email.clone().map(|email| {
241 div()
242 .truncate()
243 .text_size(px(12.))
244 .text_color(theme::text_muted())
245 .child(email)
246 })),
247 )
248 .child(
249 div()
250 .flex_none()
251 .w(px(ROLE_WIDTH))
252 .text_size(px(13.))
253 .text_color(theme::text_soft())
254 .child(if member.owner { "Owner" } else { "Member" }),
255 )
256}
257
258fn notice(text: String) -> Div {
259 div()
260 .py(px(32.))
261 .flex()
262 .justify_center()
263 .text_size(px(13.))
264 .text_color(theme::text_muted())
265 .child(text)
266}
267
268impl Render for ManageTeamPage {
269 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
270 let query = self.search.read(cx).value().to_string();
271 let state = self.app.read(cx);
272 let Some(manage_team) = state.manage_team.as_ref() else {
273 return div().into_any_element();
274 };
275 let directory = &state.directory;
276 let body: Vec<AnyElement> = match &manage_team.members {
277 MembersLoad::Loading => {
278 vec![notice("Loading members...".to_owned()).into_any_element()]
279 }
280 MembersLoad::Failed(message) => vec![notice(message.clone()).into_any_element()],
281 MembersLoad::Loaded(members) => {
282 let groups = member_groups(members, &query);
283 if groups.owners.is_empty() && groups.others.is_empty() {
284 vec![
285 notice(format!("No members match \"{}\"", query.trim())).into_any_element(),
286 ]
287 } else {
288 vec![
289 self.section(
290 "manage-team-owners",
291 "Owners",
292 &groups.owners,
293 self.owners_open,
294 directory,
295 cx,
296 )
297 .into_any_element(),
298 self.section(
299 "manage-team-others",
300 "Members and guests",
301 &groups.others,
302 self.others_open,
303 directory,
304 cx,
305 )
306 .into_any_element(),
307 ]
308 }
309 }
310 };
311 v_flex()
312 .debug_selector(|| MANAGE_TEAM_TAG.into())
313 .flex_1()
314 .w_full()
315 .min_w_0()
316 .h_full()
317 .rounded(px(10.))
318 .bg(theme::surface())
319 .overflow_hidden()
320 .child(self.header(&manage_team.team_id, &manage_team.team_name, directory, cx))
321 .child(div().w_full().h(px(1.)).flex_none().bg(theme::border()))
322 .child(with_top_edge(
323 v_flex()
324 .id("manage-team-list")
325 .flex_1()
326 .min_h_0()
327 .px(px(PAGE_PADDING - 8.))
328 .py(px(12.))
329 .overflow_y_scrollbar()
330 .track_scroll(&self.scroll)
331 .child(
332 v_flex()
333 .w_full()
334 .max_w(px(LIST_MAX_WIDTH))
335 .gap(px(12.))
336 .child(
337 h_flex()
338 .w_full()
339 .px(px(8.))
340 .justify_end()
341 .child(self.search_box()),
342 )
343 .children(body),
344 ),
345 &self.scroll,
346 MANAGE_TEAM_SCROLL_DIVIDER,
347 ))
348 .into_any_element()
349 }
350}