mod access; mod backups; mod ci_view; mod communities; mod log; mod messages; mod overview; pub(super) mod releases; mod reports; mod server; mod storage; mod ui; mod users; mod voice; use crate::lang::{Label as _, w}; use common::model::Id; use std::{fmt::Display, future::Future, path::PathBuf}; use common::{ call::{ListUsers, SetGhost}, model::{Member, Role}, permission, }; use gpui_kit::{ AnyElement, App, Context, Entity, IntoElement, ParentElement, Styled, Window, assets::IconName, component::input::InputState, div, px, }; use self::ui::icon_button; use super::{ Yumush, chat::View, entity_action, notify_error, parts::{self, Press}, settings_page::{frame, heading}, style::{Tokens, size, space, text}, widget::search_text, }; use crate::net::NetworkHandle; use common::share::Kind; pub(super) struct Transfer { pub(super) community_id: Id, pub(super) members: Vec, pub(super) pending: Option, } pub(super) fn save_directory() -> PathBuf { dirs::download_dir().unwrap_or_else(std::env::temp_dir) } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(super) enum BuilderTab { Overview, Users, Communities, Messages, Voice, Storage, Access, Reports, Server, Releases, Log, } impl BuilderTab { pub(super) const ALL: [Self; 11] = [ Self::Overview, Self::Users, Self::Communities, Self::Messages, Self::Voice, Self::Storage, Self::Access, Self::Reports, Self::Server, Self::Releases, Self::Log, ]; pub(super) fn id(self) -> &'static str { match self { Self::Overview => "overview", Self::Users => "users", Self::Communities => "communities", Self::Messages => "messages", Self::Voice => "voice", Self::Storage => "storage", Self::Access => "access", Self::Reports => "reports", Self::Server => "server", Self::Releases => "releases", Self::Log => "log", } } pub(super) fn label(self) -> &'static str { let w = &w().builder; match self { Self::Overview => w.overview, Self::Users => w.users, Self::Communities => w.communities, Self::Messages => crate::lang::w().common.messages, Self::Voice => w.voice, Self::Storage => w.storage, Self::Access => w.access, Self::Reports => w.reports, Self::Server => w.server, Self::Releases => w.releases, Self::Log => w.log, } } } impl Yumush { pub(super) fn builder_page( &self, entity: &Entity, t: Tokens, cx: &App, ) -> impl IntoElement { let tab = self.chat.admin.builder_tab; let title = tab.label(); let body = match tab { BuilderTab::Overview => self .overview_rows(t) .into_iter() .map(IntoElement::into_any_element) .collect(), BuilderTab::Users => self.users_body(entity, t, cx), BuilderTab::Communities => self.communities_body(entity, t, cx), BuilderTab::Messages => self.messages_body(entity, t), BuilderTab::Voice => self.voice_body(entity, t), BuilderTab::Storage => self.storage_body(entity, t), BuilderTab::Access => self.access_body(entity, t), BuilderTab::Reports => self.reports_body(entity, t), BuilderTab::Server => self.server_body(entity, t), BuilderTab::Releases => self.releases_body(entity, t), BuilderTab::Log => self.log_body(entity, t), }; let mut sections = vec![heading(title, self.tab_actions(tab, entity, t, cx), t).into_any_element()]; sections.extend(body); frame( "builder_scroll", entity, t, Role::Builder.label(), BuilderTab::ALL .map(|tab| self.builder_nav_item(tab, entity, t).into_any_element()) .into_iter() .collect(), vec![self.ghost_toggle(entity, t).into_any_element()], sections, ) } fn tab_actions( &self, tab: BuilderTab, entity: &Entity, t: Tokens, cx: &App, ) -> Vec { match tab { BuilderTab::Voice => vec![ parts::button( "refresh_voice_button".to_string(), w().builder.refresh, Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.load_voice_rooms(window, cx) }), ) .into_any_element(), ], BuilderTab::Server if self.server_form(cx) != self.server => vec![ parts::button( "save_server", w().builder.save, Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.save_server_settings(window, cx) }), ) .into_any_element(), ], BuilderTab::Releases => vec![ parts::button( "add_release_files".to_string(), w().builder_release.add_files, Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.pick_release_files(window, cx) }), ) .into_any_element(), ], BuilderTab::Log => vec![ icon_button( "export_builder_log".to_string(), IconName::Download, w().builder.export, t.dim(), t, entity_action(entity, |yumush, window, cx| { yumush.export_builder_log(window, cx) }), ) .into_any_element(), ], _ => Vec::new(), } } fn builder_nav_item( &self, tab: BuilderTab, entity: &Entity, t: Tokens, ) -> impl IntoElement { let open = (tab == BuilderTab::Reports) .then_some(self.chat.admin.open_reports) .filter(|count| *count > 0); parts::index_item( format!("builder_tab_{}", tab.id()), tab.label(), self.chat.admin.builder_tab == tab, t, entity_action(entity, move |yumush, window, cx| { yumush.select_builder_tab(tab, window, cx) }), ) .children(open.map(|count| { div() .ml(px(space::SM)) .text_size(t.text(text::CAPTION)) .text_color(t.bad()) .child(count.to_string()) })) } fn ghost_toggle(&self, entity: &Entity, t: Tokens) -> impl IntoElement { let enabled = self.chat.session.ghost; let toggle = entity_action(entity, move |yumush, window, cx| { yumush.set_ghost(!enabled, window, cx) }); div() .h(px(size::CONTROL)) .flex() .items_center() .justify_between() .text_size(t.text(text::LABEL)) .text_color(t.dim()) .child(w().builder.ghost) .child(parts::toggle("ghost_switch", enabled, t, toggle)) } pub(super) fn is_builder(&self) -> bool { self.user .as_ref() .is_some_and(|me| permission::is_builder(me.role)) } pub(super) fn load_builder(&mut self, window: &mut Window, cx: &mut Context) { self.load_open_reports(window, cx); match self.chat.admin.builder_tab { BuilderTab::Overview => self.load_overview(window, cx), BuilderTab::Users => self.load_users(window, cx), BuilderTab::Communities => self.load_builder_communities(window, cx), BuilderTab::Messages => { self.load_users(window, cx); self.load_builder_communities(window, cx); } BuilderTab::Storage => { self.load_users(window, cx); self.load_builder_communities(window, cx); self.load_storage(window, cx); } BuilderTab::Voice => { self.load_voice_rooms(window, cx); self.load_builder_communities(window, cx); } BuilderTab::Access => self.load_access(window, cx), BuilderTab::Reports => self.load_reports(window, cx), BuilderTab::Server => {} BuilderTab::Releases => self.load_releases(window, cx), BuilderTab::Log => { self.load_users(window, cx); self.load_builder_logs(window, cx); } } } pub(super) fn on_builder_tab(&self, tab: BuilderTab) -> bool { self.chat.session.view == View::Builder && self.chat.admin.builder_tab == tab } pub(super) fn report_created(&self, window: &mut Window, cx: &mut Context) { self.load_open_reports(window, cx); if self.on_builder_tab(BuilderTab::Reports) { self.load_reports(window, cx); } } pub(super) fn select_builder_tab( &mut self, tab: BuilderTab, window: &mut Window, cx: &mut Context, ) { self.chat.admin.builder_tab = tab; if tab == BuilderTab::Server { self.fill_server_form(window, cx); } self.load_builder(window, cx); cx.notify(); } pub(super) fn fetch_builder( &self, window: &mut Window, cx: &mut Context, fetch: impl FnOnce(NetworkHandle) -> Fut + 'static, apply: impl FnOnce(&mut Self, T, &mut Context) + 'static, ) where T: 'static, E: Display + 'static, Fut: Future> + 'static, { if !self.is_builder() { return; } let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { let value = match fetch(network).await { Ok(value) => value, Err(error) => { notify_error(&this, cx, error); return; } }; let _ = this.update(cx, |yumush, cx| apply(yumush, value, cx)); }) .detach(); } pub(super) fn load_users(&self, window: &mut Window, cx: &mut Context) { self.fetch_builder( window, cx, |network| async move { network.call(ListUsers).await }, |yumush, accounts, cx| { for account in &accounts { yumush.request_user_avatar(&account.user, cx); } let ghost = yumush .user .as_ref() .and_then(|me| accounts.iter().find(|account| account.user.id == me.id)) .is_some_and(|account| account.ghost); yumush.chat.admin.builder_users = accounts; yumush.set_ghost_state(ghost); cx.notify(); }, ); } pub(super) fn releases_changed(&mut self, window: &mut Window, cx: &mut Context) { if self.on_builder_tab(BuilderTab::Releases) { self.load_releases(window, cx); } } pub(super) fn voice_changed(&self, window: &mut Window, cx: &mut Context) { if self.on_builder_tab(BuilderTab::Voice) { self.load_voice_rooms(window, cx); } } fn resolve_user(&self, username: &str) -> Option { self.chat .admin .builder_users .iter() .find(|account| account.user.username.to_lowercase() == username) .map(|account| account.user.id) } fn resolve_community(&self, name: &str) -> Option { self.chat .admin .builder_communities .iter() .find(|summary| { summary.community.kind.is_group() && summary.community.name.to_lowercase() == name }) .map(|summary| summary.community.id) } pub(super) fn filter_user(&self, input: &Entity, cx: &App) -> Option> { let name = search_text(input, cx); if name.is_empty() { return Some(None); } self.resolve_user(&name).map(Some) } pub(super) fn filter_community( &self, input: &Entity, cx: &App, ) -> Option> { let name = search_text(input, cx); if name.is_empty() { return Some(None); } self.resolve_community(&name).map(Some) } fn set_ghost_state(&mut self, ghost: bool) { self.chat.session.ghost = ghost; let Some(me) = self.user.as_ref() else { return; }; if let Some(account) = self .chat .admin .builder_users .iter_mut() .find(|account| account.user.id == me.id) { account.ghost = ghost; } } fn set_ghost(&self, enabled: bool, window: &mut Window, cx: &mut Context) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { if enabled { let _ = network.stop(Kind::Screen).await; let _ = network.stop(Kind::Camera).await; } match network.call(SetGhost { enabled }).await { Ok(()) => { let _ = this.update(cx, |yumush, cx| { yumush.set_ghost_state(enabled); if enabled { yumush.set_muted(true, cx); } cx.notify(); }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } }