use common::model::Id; use common::{ call::{CreateInviteCode, DeleteInviteCode, ListInviteCodes, ListServerBans, RemoveServerBan}, model::{InviteCode, ServerBan, ServerBanKind}, }; use gpui_kit::{ AnyElement, ClipboardItem, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, assets::IconName, div, px, }; use super::ui::{ card, card_header, delete_icon_button, fixed, icon_button, list_card, list_row, number, pill, rows_body, toolbar, }; use crate::gui::chat::Armed; use crate::gui::style::{Tokens, size, space, text}; use crate::gui::{Yumush, entity_action, millis_time, notify_error, text, widget::field}; use crate::lang::w; fn invite_row(invite: &InviteCode, pending: bool, entity: &Entity, t: Tokens) -> Div { let copy_code = invite.code.clone(); let delete_code = invite.code.clone(); let expiry = invite.expires_at.map_or_else( || w().builder_access.never.to_string(), |at| { w().builder_access .expires(millis_time(at, crate::lang::w().common.stamp)) }, ); let left = match invite.uses_left { 1 => w().builder_access.one_use_left.to_string(), uses => w().builder_access.uses_left(uses), }; list_row() .px(px(space::LG)) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .child( div() .font_weight(FontWeight::MEDIUM) .truncate() .child(invite.code.clone()), ) .child( div() .text_size(text(text::CAPTION)) .text_color(t.dim()) .truncate() .child(format!("{left} ยท {expiry}")), ), ) .child(icon_button( format!("copy_invite_{}", invite.code), IconName::Copy, w().builder_access.copy, t.dim(), t, move |_, cx| cx.write_to_clipboard(ClipboardItem::new_string(copy_code.clone())), )) .child(delete_icon_button( format!("delete_invite_{}", invite.code), pending, t, entity_action(entity, move |yumush, window, cx| { yumush.delete_invite(&delete_code, window, cx) }), )) } fn ban_row(ban: &ServerBan, entity: &Entity, t: Tokens) -> Div { let remove_id = ban.id; let kind = match ban.kind { ServerBanKind::Ip => "IP", ServerBanKind::Username => w().builder_access.username, }; list_row() .px(px(space::LG)) .child(pill(kind, t.paper())) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .child( div() .font_weight(FontWeight::MEDIUM) .truncate() .child(ban.value.clone()), ) .children(ban.reason.as_ref().map(|reason| { div() .text_size(text(text::CAPTION)) .text_color(t.dim()) .min_w_0() .child(super::super::glyph::line( reason.clone().into(), super::super::text(super::super::style::text::CAPTION), )) })), ) .child(icon_button( format!("remove_ban_{}", ban.id), IconName::X, w().builder_access.remove, t.dim(), t, entity_action(entity, move |yumush, window, cx| { yumush.remove_ban(remove_id, window, cx) }), )) } impl Yumush { pub(super) fn load_access(&self, window: &mut Window, cx: &mut Context) { self.load_invites(window, cx); self.load_server_bans(window, cx); } pub(super) fn load_invites(&self, window: &mut Window, cx: &mut Context) { self.fetch_builder( window, cx, |network| async move { network.call(ListInviteCodes).await }, |yumush, codes, cx| { yumush.chat.admin.builder_invites = codes; cx.notify(); }, ); } pub(super) fn load_server_bans(&self, window: &mut Window, cx: &mut Context) { self.fetch_builder( window, cx, |network| async move { network.call(ListServerBans).await }, |yumush, bans, cx| { yumush.chat.admin.builder_bans = bans; cx.notify(); }, ); } pub(super) fn create_invite(&mut self, window: &mut Window, cx: &mut Context) { let uses = self.chat.admin.invite_uses.read(cx).text().to_string(); let Some(uses) = number(&uses) else { return; }; let hours = self.chat.admin.invite_hours.read(cx).text().to_string(); let hours = if hours.trim().is_empty() { None } else { let Some(hours) = number(&hours) else { return; }; Some(hours) }; let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(CreateInviteCode { uses, hours }).await { Ok(_) => { let _ = this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn delete_invite( &mut self, code: &str, window: &mut Window, cx: &mut Context, ) { if !self.arm(Armed::DeleteInvite(code.to_string()), cx) { return; } let network = self.network.clone(); let code = code.to_string(); cx.spawn_in(window, async move |this, cx| { match network.call(DeleteInviteCode { code }).await { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| yumush.load_invites(window, cx)); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn remove_ban(&mut self, id: Id, window: &mut Window, cx: &mut Context) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(RemoveServerBan { id }).await { Ok(()) => { let _ = this .update_in(cx, |yumush, window, cx| yumush.load_server_bans(window, cx)); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } pub(super) fn access_body(&self, entity: &Entity, t: Tokens) -> Vec { let controls = toolbar(vec![ fixed(80., field(t, &self.chat.admin.invite_uses)), fixed(size::THUMB, field(t, &self.chat.admin.invite_hours)), crate::gui::parts::button( "create_invite", w().builder_access.create, crate::gui::parts::Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.create_invite(window, cx) }), ) .into_any_element(), ]) .into_any_element(); let invite_header = card_header(w().builder_access.invite_codes, vec![controls], t); let invites = self.chat.admin.builder_invites.iter().map(|invite| { let pending = self.armed(Armed::DeleteInvite(invite.code.clone())); invite_row(invite, pending, entity, t) }); let bans = self .chat .admin .builder_bans .iter() .map(|ban| ban_row(ban, entity, t)) .collect(); vec![ card() .child(invite_header) .child(rows_body( invites.collect(), w().builder_access.no_invite_codes, t, )) .into_any_element(), list_card(Some(w().builder_access.bans), bans, w().common.no_bans, t) .into_any_element(), ] } }