use common::model::Id; use common::{ call::{ DeleteCommunity, GetCommunity, ListAllCommunities, ListMembers, SetCommunityVoice, TransferOwnership, }, model::{Community, CommunitySummary, Member, Rank}, }; use gpui_kit::{ AnyElement, App, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, assets::IconName, div, prelude::FluentBuilder as _, px, }; use super::{ Transfer, ui::{card, confirmed, icon_button, list_row, pill, rows_body}, }; use crate::gui::{ Palette, Yumush, avatar, community_icon, entity_action, notify_error, parts::{self, Press}, profile::user_link, style::{radius, size, space, text}, text, widget::search_text, }; use crate::lang::w; impl Yumush { pub(super) fn set_builder_community_voice( &mut self, community_id: Id, enabled: bool, window: &mut Window, cx: &mut Context, ) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network .call(SetCommunityVoice { community_id, enabled, }) .await { Ok(_) => { let _ = this.update_in(cx, |yumush, window, cx| { yumush.load_builder_communities(window, cx) }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn load_builder_communities(&self, window: &mut Window, cx: &mut Context) { self.fetch_builder( window, cx, |network| async move { network.call(ListAllCommunities).await }, |yumush, summaries, cx| { for summary in &summaries { if let Some(avatar_id) = summary.community.avatar_id { yumush.request_avatar(avatar_id, cx); } if let Some(owner) = &summary.owner { yumush.request_user_avatar(owner, cx); } } yumush.chat.builder_communities = summaries; cx.notify(); }, ); } pub(super) fn delete_builder_community( &mut self, community_id: Id, window: &mut Window, cx: &mut Context, ) { if !confirmed(&mut self.chat.pending_community_delete, community_id) { cx.notify(); return; } let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(DeleteCommunity { community_id }).await { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| { yumush.chat.remove_community(community_id); yumush.load_builder_communities(window, cx); }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn open_transfer( &mut self, community_id: Id, window: &mut Window, cx: &mut Context, ) { if self .chat .transfer .as_ref() .is_some_and(|transfer| transfer.community_id == community_id) { self.chat.transfer = None; cx.notify(); return; } self.chat.transfer = Some(Transfer { community_id, members: Vec::new(), pending: None, }); cx.notify(); self.load_transfer_members(community_id, window, cx); } pub(super) fn load_transfer_members( &self, community_id: Id, window: &mut Window, cx: &mut Context, ) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { let members = match network.call(ListMembers { community_id }).await { Ok(members) => members, Err(error) => { notify_error(&this, cx, error); return; } }; let _ = this.update_in(cx, |yumush, window, cx| { for member in &members { yumush.request_user_avatar(&member.user, cx); } let Some(transfer) = yumush.chat.transfer.as_mut() else { return; }; if transfer.community_id == community_id { transfer.members = members; window.refresh(); cx.notify(); } }); }) .detach(); } pub(super) fn transfer_rows(&self, entity: &Entity, palette: Palette) -> Vec { let Some(transfer) = &self.chat.transfer else { return Vec::new(); }; transfer .members .iter() .filter(|member| member.rank != Rank::Owner) .map(|member| { let pending = transfer.pending == Some(member.user.id); self.transfer_row(member, pending, entity, palette) .into_any_element() }) .collect() } pub(super) fn transfer_to(&mut self, user_id: Id, window: &mut Window, cx: &mut Context) { let Some(transfer) = self.chat.transfer.as_mut() else { return; }; if !confirmed(&mut transfer.pending, user_id) { window.refresh(); cx.notify(); return; } let community_id = transfer.community_id; let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network .call(TransferOwnership { community_id, user_id, }) .await { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| { yumush.chat.transfer = None; yumush.load_builder_communities(window, cx); }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn jump_to( &mut self, community_id: Id, voice: bool, window: &mut Window, cx: &mut Context, ) { if let Some(community) = self .chat .communities() .iter() .find(|community| community.id == community_id) .cloned() { self.enter(community, voice, window, cx); return; } let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(GetCommunity { community_id }).await { Ok(community) => { let _ = this.update_in(cx, |yumush, window, cx| { yumush.enter(community, voice, window, cx) }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } fn enter( &mut self, community: Community, voice: bool, window: &mut Window, cx: &mut Context, ) { let community_id = community.id; if self.chat.joined(community_id) { self.select_community(community_id, window, cx); } else { self.open_builder_community(community, window, cx); } if voice && !self.chat.in_voice(community_id) { self.toggle_voice(community_id, window, cx); } } pub(super) fn open_builder_community( &mut self, community: Community, window: &mut Window, cx: &mut Context, ) { let community_id = community.id; if !self.chat.joined(community_id) { self.chat.opened = Some(community); } self.select_community(community_id, window, cx); } pub(super) fn communities_body( &self, entity: &Entity, palette: Palette, cx: &App, ) -> Vec { let query = search_text(&self.chat.builder_search, cx); let rows: Vec
= self .chat .builder_communities .iter() .filter(|summary| { summary .community .name .to_lowercase() .contains(query.as_str()) }) .map(|summary| self.community_row(summary, entity, palette)) .collect(); vec![ card(palette) .child( div().pb(px(space::SM)).flex().items_center().child( div() .w(px(size::SIDEBAR)) .child(crate::gui::sidebar::input_box( &self.chat.builder_search, palette, )), ), ) .child(rows_body(rows, w().builder_communities.empty, palette)) .into_any_element(), ] } pub(super) fn transfer_row( &self, member: &Member, pending: bool, entity: &Entity, palette: Palette, ) -> Div { let user_id = member.user.id; div() .h(px(size::ROW)) .px(px(space::MD)) .flex() .items_center() .gap(px(space::SM)) .child( div().flex_1().min_w_0().flex().child(user_link( format!("transfer_user_{}", member.user.id), Some(&member.user), entity, div() .flex() .items_center() .gap(px(space::SM)) .child(avatar( palette, &member.user, px(size::AVATAR), text(text::CAPTION), &self.chat.avatars, )) .child( div() .min_w_0() .truncate() .child(member.user.username.clone()), ), )), ) .child(parts::button( format!("transfer_to_{}", member.user.id), if pending { w().builder_communities.confirm } else { w().builder_communities.transfer }, Press::Plain, palette.tokens, entity_action(entity, move |yumush, window, cx| { yumush.transfer_to(user_id, window, cx) }), )) } pub(super) fn community_row( &self, summary: &CommunitySummary, entity: &Entity, palette: Palette, ) -> Div { let community = &summary.community; let opened = community.clone(); let id = community.id; let log_id = id; let transfer_id = id; let delete_id = id; let voice_id = id; let voice_enabled = community.voice_enabled; let pending_delete = self.chat.pending_community_delete == Some(id); let expanded = self .chat .transfer .as_ref() .is_some_and(|transfer| transfer.community_id == id); let owner = summary.owner.as_ref().map_or(String::new(), |owner| { format!(" ยท {}", w().builder_communities.owner(&owner.username)) }); let meta = format!( "{}{owner}", w().builder_communities.members(summary.members) ); let row = list_row() .px(px(space::LG)) .child(community_icon( palette, community, px(size::AVATAR), text(text::CAPTION), &self.chat.avatars, )) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .child( div() .text_size(text(text::LABEL)) .font_weight(FontWeight::MEDIUM) .text_color(palette.text) .truncate() .child(community.name.clone()), ) .child( div() .text_size(text(text::CAPTION)) .text_color(palette.muted) .truncate() .child(meta), ), ) .children( (!voice_enabled) .then(|| pill(w().builder_communities.voice_off, palette.muted, palette)), ) .child(icon_button( format!("open_community_{id}"), IconName::ArrowUpRight, w().builder_communities.open, palette.muted, palette, entity_action(entity, move |yumush, window, cx| { yumush.open_builder_community(opened.clone(), window, cx) }), )) .child(icon_button( format!("log_community_{id}"), IconName::ScrollText, w().builder_communities.log, palette.muted, palette, entity_action(entity, move |yumush, window, cx| { yumush.show_logs(log_id, window, cx) }), )) .child(icon_button( format!("voice_community_{id}_{voice_enabled}"), if voice_enabled { IconName::Phone } else { IconName::PhoneOff }, if voice_enabled { w().builder_communities.voice_on } else { w().builder_communities.voice_off }, palette.muted, palette, entity_action(entity, move |yumush, window, cx| { yumush.set_builder_community_voice(voice_id, !voice_enabled, window, cx) }), )) .child(icon_button( format!("transfer_community_{id}"), IconName::ArrowRightLeft, w().builder_communities.transfer, if expanded { palette.text } else { palette.muted }, palette, entity_action(entity, move |yumush, window, cx| { yumush.open_transfer(transfer_id, window, cx) }), )) .child( icon_button( format!("delete_community_{id}_{pending_delete}"), IconName::Trash, if pending_delete { w().builder_communities.confirm_delete } else { w().builder_communities.delete }, if pending_delete { palette.on_accent } else { palette.danger }, palette, entity_action(entity, move |yumush, window, cx| { yumush.delete_builder_community(delete_id, window, cx) }), ) .when(pending_delete, |this| this.bg(palette.danger)), ); div() .flex() .flex_col() .child(row) .children(expanded.then(|| { div().px(px(space::LG)).pb(px(space::MD)).child( div() .flex() .flex_col() .rounded(px(radius::MD)) .children(self.transfer_rows(entity, palette)), ) })) } }