forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.07.6 MiB189b6d8Tahinli1 hour ago411 commits

communities.rs517 lines, 16.4 KiBHistoryRawDownload
1use common::model::Id;
2use common::{
3 call::{
4 DeleteCommunity, GetCommunity, ListAllCommunities, ListMembers, SetCommunityVoice,
5 TransferOwnership,
6 },
7 model::{Community, CommunitySummary, Member, Rank},
8};
9use gpui_kit::{
10 AnyElement, App, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window,
11 assets::IconName, div, prelude::FluentBuilder as _, px,
12};
13
14use super::{
15 Transfer,
16 ui::{card, confirmed, icon_button, list_row, pill, rows_body},
17};
18use crate::gui::{
19 Palette, Yumush, avatar, community_icon, entity_action, notify_error,
20 parts::{self, Press},
21 profile::user_link,
22 style::{radius, size, space, text},
23 text,
24 widget::search_text,
25};
26use crate::lang::w;
27
28impl Yumush {
29 pub(super) fn set_builder_community_voice(
30 &mut self,
31 community_id: Id,
32 enabled: bool,
33 window: &mut Window,
34 cx: &mut Context<Self>,
35 ) {
36 let network = self.network.clone();
37
38 cx.spawn_in(window, async move |this, cx| {
39 match network
40 .call(SetCommunityVoice {
41 community_id,
42 enabled,
43 })
44 .await
45 {
46 Ok(_) => {
47 let _ = this.update_in(cx, |yumush, window, cx| {
48 yumush.load_builder_communities(window, cx)
49 });
50 }
51 Err(error) => notify_error(&this, cx, error),
52 }
53 })
54 .detach();
55 }
56
57 pub(super) fn load_builder_communities(&self, window: &mut Window, cx: &mut Context<Self>) {
58 self.fetch_builder(
59 window,
60 cx,
61 |network| async move { network.call(ListAllCommunities).await },
62 |yumush, summaries, cx| {
63 for summary in &summaries {
64 if let Some(avatar_id) = summary.community.avatar_id {
65 yumush.request_avatar(avatar_id, cx);
66 }
67 if let Some(owner) = &summary.owner {
68 yumush.request_user_avatar(owner, cx);
69 }
70 }
71 yumush.chat.builder_communities = summaries;
72 cx.notify();
73 },
74 );
75 }
76
77 pub(super) fn delete_builder_community(
78 &mut self,
79 community_id: Id,
80 window: &mut Window,
81 cx: &mut Context<Self>,
82 ) {
83 if !confirmed(&mut self.chat.pending_community_delete, community_id) {
84 cx.notify();
85 return;
86 }
87 let network = self.network.clone();
88
89 cx.spawn_in(window, async move |this, cx| {
90 match network.call(DeleteCommunity { community_id }).await {
91 Ok(()) => {
92 let _ = this.update_in(cx, |yumush, window, cx| {
93 yumush.chat.remove_community(community_id);
94 yumush.load_builder_communities(window, cx);
95 });
96 }
97 Err(error) => notify_error(&this, cx, error),
98 }
99 })
100 .detach();
101 }
102
103 pub(super) fn open_transfer(
104 &mut self,
105 community_id: Id,
106 window: &mut Window,
107 cx: &mut Context<Self>,
108 ) {
109 if self
110 .chat
111 .transfer
112 .as_ref()
113 .is_some_and(|transfer| transfer.community_id == community_id)
114 {
115 self.chat.transfer = None;
116 cx.notify();
117 return;
118 }
119 self.chat.transfer = Some(Transfer {
120 community_id,
121 members: Vec::new(),
122 pending: None,
123 });
124 cx.notify();
125 self.load_transfer_members(community_id, window, cx);
126 }
127
128 pub(super) fn load_transfer_members(
129 &self,
130 community_id: Id,
131 window: &mut Window,
132 cx: &mut Context<Self>,
133 ) {
134 let network = self.network.clone();
135
136 cx.spawn_in(window, async move |this, cx| {
137 let members = match network.call(ListMembers { community_id }).await {
138 Ok(members) => members,
139 Err(error) => {
140 notify_error(&this, cx, error);
141 return;
142 }
143 };
144
145 let _ = this.update_in(cx, |yumush, window, cx| {
146 for member in &members {
147 yumush.request_user_avatar(&member.user, cx);
148 }
149 let Some(transfer) = yumush.chat.transfer.as_mut() else {
150 return;
151 };
152 if transfer.community_id == community_id {
153 transfer.members = members;
154 window.refresh();
155 cx.notify();
156 }
157 });
158 })
159 .detach();
160 }
161
162 pub(super) fn transfer_rows(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> {
163 let Some(transfer) = &self.chat.transfer else {
164 return Vec::new();
165 };
166
167 transfer
168 .members
169 .iter()
170 .filter(|member| member.rank != Rank::Owner)
171 .map(|member| {
172 let pending = transfer.pending == Some(member.user.id);
173 self.transfer_row(member, pending, entity, palette)
174 .into_any_element()
175 })
176 .collect()
177 }
178
179 pub(super) fn transfer_to(&mut self, user_id: Id, window: &mut Window, cx: &mut Context<Self>) {
180 let Some(transfer) = self.chat.transfer.as_mut() else {
181 return;
182 };
183 if !confirmed(&mut transfer.pending, user_id) {
184 window.refresh();
185 cx.notify();
186 return;
187 }
188 let community_id = transfer.community_id;
189 let network = self.network.clone();
190
191 cx.spawn_in(window, async move |this, cx| {
192 match network
193 .call(TransferOwnership {
194 community_id,
195 user_id,
196 })
197 .await
198 {
199 Ok(()) => {
200 let _ = this.update_in(cx, |yumush, window, cx| {
201 yumush.chat.transfer = None;
202 yumush.load_builder_communities(window, cx);
203 });
204 }
205 Err(error) => notify_error(&this, cx, error),
206 }
207 })
208 .detach();
209 }
210
211 pub(super) fn jump_to(
212 &mut self,
213 community_id: Id,
214 voice: bool,
215 window: &mut Window,
216 cx: &mut Context<Self>,
217 ) {
218 if let Some(community) = self
219 .chat
220 .communities()
221 .iter()
222 .find(|community| community.id == community_id)
223 .cloned()
224 {
225 self.enter(community, voice, window, cx);
226 return;
227 }
228 let network = self.network.clone();
229
230 cx.spawn_in(window, async move |this, cx| {
231 match network.call(GetCommunity { community_id }).await {
232 Ok(community) => {
233 let _ = this.update_in(cx, |yumush, window, cx| {
234 yumush.enter(community, voice, window, cx)
235 });
236 }
237 Err(error) => notify_error(&this, cx, error),
238 }
239 })
240 .detach();
241 }
242
243 fn enter(
244 &mut self,
245 community: Community,
246 voice: bool,
247 window: &mut Window,
248 cx: &mut Context<Self>,
249 ) {
250 let community_id = community.id;
251 if self.chat.joined(community_id) {
252 self.select_community(community_id, window, cx);
253 } else {
254 self.open_builder_community(community, window, cx);
255 }
256 if voice && !self.chat.in_voice(community_id) {
257 self.toggle_voice(community_id, window, cx);
258 }
259 }
260
261 pub(super) fn open_builder_community(
262 &mut self,
263 community: Community,
264 window: &mut Window,
265 cx: &mut Context<Self>,
266 ) {
267 let community_id = community.id;
268 if !self.chat.joined(community_id) {
269 self.chat.opened = Some(community);
270 }
271 self.select_community(community_id, window, cx);
272 }
273
274 pub(super) fn communities_body(
275 &self,
276 entity: &Entity<Self>,
277 palette: Palette,
278 cx: &App,
279 ) -> Vec<AnyElement> {
280 let query = search_text(&self.chat.builder_search, cx);
281 let rows: Vec<Div> = self
282 .chat
283 .builder_communities
284 .iter()
285 .filter(|summary| {
286 summary
287 .community
288 .name
289 .to_lowercase()
290 .contains(query.as_str())
291 })
292 .map(|summary| self.community_row(summary, entity, palette))
293 .collect();
294
295 vec![
296 card(palette)
297 .child(
298 div().pb(px(space::SM)).flex().items_center().child(
299 div()
300 .w(px(size::SIDEBAR))
301 .child(crate::gui::sidebar::input_box(
302 &self.chat.builder_search,
303 palette,
304 )),
305 ),
306 )
307 .child(rows_body(rows, w().builder_communities.empty, palette))
308 .into_any_element(),
309 ]
310 }
311
312 pub(super) fn transfer_row(
313 &self,
314 member: &Member,
315 pending: bool,
316 entity: &Entity<Self>,
317 palette: Palette,
318 ) -> Div {
319 let user_id = member.user.id;
320
321 div()
322 .h(px(size::ROW))
323 .px(px(space::MD))
324 .flex()
325 .items_center()
326 .gap(px(space::SM))
327 .child(
328 div().flex_1().min_w_0().flex().child(user_link(
329 format!("transfer_user_{}", member.user.id),
330 Some(&member.user),
331 entity,
332 div()
333 .flex()
334 .items_center()
335 .gap(px(space::SM))
336 .child(avatar(
337 palette,
338 &member.user,
339 px(size::AVATAR),
340 text(text::CAPTION),
341 &self.chat.avatars,
342 ))
343 .child(
344 div()
345 .min_w_0()
346 .truncate()
347 .child(member.user.username.clone()),
348 ),
349 )),
350 )
351 .child(parts::button(
352 format!("transfer_to_{}", member.user.id),
353 if pending {
354 w().builder_communities.confirm
355 } else {
356 w().builder_communities.transfer
357 },
358 Press::Plain,
359 palette.tokens,
360 entity_action(entity, move |yumush, window, cx| {
361 yumush.transfer_to(user_id, window, cx)
362 }),
363 ))
364 }
365
366 pub(super) fn community_row(
367 &self,
368 summary: &CommunitySummary,
369 entity: &Entity<Self>,
370 palette: Palette,
371 ) -> Div {
372 let community = &summary.community;
373 let opened = community.clone();
374 let id = community.id;
375 let log_id = id;
376 let transfer_id = id;
377 let delete_id = id;
378 let voice_id = id;
379 let voice_enabled = community.voice_enabled;
380 let pending_delete = self.chat.pending_community_delete == Some(id);
381 let expanded = self
382 .chat
383 .transfer
384 .as_ref()
385 .is_some_and(|transfer| transfer.community_id == id);
386 let owner = summary.owner.as_ref().map_or(String::new(), |owner| {
387 format!(" · {}", w().builder_communities.owner(&owner.username))
388 });
389 let meta = format!(
390 "{}{owner}",
391 w().builder_communities.members(summary.members)
392 );
393
394 let row = list_row()
395 .px(px(space::LG))
396 .child(community_icon(
397 palette,
398 community,
399 px(size::AVATAR),
400 text(text::CAPTION),
401 &self.chat.avatars,
402 ))
403 .child(
404 div()
405 .flex_1()
406 .min_w_0()
407 .flex()
408 .flex_col()
409 .child(
410 div()
411 .text_size(text(text::LABEL))
412 .font_weight(FontWeight::MEDIUM)
413 .text_color(palette.text)
414 .truncate()
415 .child(community.name.clone()),
416 )
417 .child(
418 div()
419 .text_size(text(text::CAPTION))
420 .text_color(palette.muted)
421 .truncate()
422 .child(meta),
423 ),
424 )
425 .children(
426 (!voice_enabled)
427 .then(|| pill(w().builder_communities.voice_off, palette.muted, palette)),
428 )
429 .child(icon_button(
430 format!("open_community_{id}"),
431 IconName::ArrowUpRight,
432 w().builder_communities.open,
433 palette.muted,
434 palette,
435 entity_action(entity, move |yumush, window, cx| {
436 yumush.open_builder_community(opened.clone(), window, cx)
437 }),
438 ))
439 .child(icon_button(
440 format!("log_community_{id}"),
441 IconName::ScrollText,
442 w().builder_communities.log,
443 palette.muted,
444 palette,
445 entity_action(entity, move |yumush, window, cx| {
446 yumush.show_logs(log_id, window, cx)
447 }),
448 ))
449 .child(icon_button(
450 format!("voice_community_{id}_{voice_enabled}"),
451 if voice_enabled {
452 IconName::Phone
453 } else {
454 IconName::PhoneOff
455 },
456 if voice_enabled {
457 w().builder_communities.voice_on
458 } else {
459 w().builder_communities.voice_off
460 },
461 palette.muted,
462 palette,
463 entity_action(entity, move |yumush, window, cx| {
464 yumush.set_builder_community_voice(voice_id, !voice_enabled, window, cx)
465 }),
466 ))
467 .child(icon_button(
468 format!("transfer_community_{id}"),
469 IconName::ArrowRightLeft,
470 w().builder_communities.transfer,
471 if expanded {
472 palette.text
473 } else {
474 palette.muted
475 },
476 palette,
477 entity_action(entity, move |yumush, window, cx| {
478 yumush.open_transfer(transfer_id, window, cx)
479 }),
480 ))
481 .child(
482 icon_button(
483 format!("delete_community_{id}_{pending_delete}"),
484 IconName::Trash,
485 if pending_delete {
486 w().builder_communities.confirm_delete
487 } else {
488 w().builder_communities.delete
489 },
490 if pending_delete {
491 palette.on_accent
492 } else {
493 palette.danger
494 },
495 palette,
496 entity_action(entity, move |yumush, window, cx| {
497 yumush.delete_builder_community(delete_id, window, cx)
498 }),
499 )
500 .when(pending_delete, |this| this.bg(palette.danger)),
501 );
502
503 div()
504 .flex()
505 .flex_col()
506 .child(row)
507 .children(expanded.then(|| {
508 div().px(px(space::LG)).pb(px(space::MD)).child(
509 div()
510 .flex()
511 .flex_col()
512 .rounded(px(radius::MD))
513 .children(self.transfer_rows(entity, palette)),
514 )
515 }))
516 }
517}