use common::model::Id; use common::{ call::{DeleteAttachment, ListAttachments}, model::AttachmentInfo, }; use gpui_kit::{ AnyElement, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, assets::IconName, component::{Icon, WindowExt, notification::Notification}, div, px, relative, }; use super::ui::{card, card_header, delete_icon_button, fixed, list_card, list_row, toolbar}; use crate::gui::chat::Armed; use crate::gui::{ Yumush, community_settings::user_name, entity_action, millis_time, notify_error, storage, style::{Tokens, radius, size, space, text}, text, widget::field, }; use crate::lang::w; fn usage_row(name: &str, bytes: u64, count: u64, fraction: f32, t: Tokens) -> Div { div() .px(px(space::LG)) .py(px(space::SM)) .flex() .flex_col() .gap(px(space::SM)) .child( div() .flex() .items_center() .gap(px(space::SM)) .child( div() .flex_1() .min_w_0() .truncate() .text_size(text(text::LABEL)) .text_color(t.paper()) .child(name.to_string()), ) .child( div() .flex_shrink_0() .text_size(text(text::LABEL)) .font_weight(FontWeight::MEDIUM) .text_color(t.paper()) .child(storage(bytes)), ) .child( div() .w(px(size::ROW)) .flex_shrink_0() .flex() .justify_end() .text_size(text(text::CAPTION)) .text_color(t.dim()) .child(count.to_string()), ), ) .child( div() .h(px(space::XXS)) .rounded(px(radius::SM)) .bg(t.hov()) .child( div() .h_full() .w(relative(fraction)) .rounded(px(radius::SM)) .bg(t.paper()), ), ) } fn usage_card(title: &'static str, rows: Vec<(&str, u64, u64)>, t: Tokens) -> Div { let max = rows.iter().map(|&(_, bytes, _)| bytes).max().unwrap_or(0); let rows = rows .into_iter() .map(|(name, bytes, count)| { usage_row(name, bytes, count, bytes as f32 / max.max(1) as f32, t) }) .collect(); div() .flex_1() .min_w_0() .child(list_card(Some(title), rows, w().builder_storage.no_data, t).h_full()) } fn attachment_row(info: &AttachmentInfo, pending: bool, entity: &Entity, t: Tokens) -> Div { let attachment_id = info.attachment.id; let delete = delete_icon_button( format!("delete_attachment_{}", info.attachment.id), pending, t, entity_action(entity, move |yumush, window, cx| { yumush.delete_builder_attachment(attachment_id, window, cx) }), ); list_row() .child( Icon::new(IconName::File) .size(px(size::ICON)) .flex_shrink_0() .text_color(t.dim()), ) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .gap(px(space::XXS)) .child( div() .min_w_0() .text_size(text(text::LABEL)) .text_color(t.paper()) .child(super::super::glyph::line( info.attachment.name.clone().into(), super::super::text(super::super::style::text::LABEL), )), ) .child( div() .truncate() .text_size(text(text::CAPTION)) .text_color(t.dim()) .child(format!( "{} · {} · {}", user_name(&info.uploader), info.community, millis_time(info.attachment.created_at, crate::lang::w().common.date) )), ), ) .child( div() .flex_shrink_0() .text_size(text(text::CAPTION)) .text_color(t.dim()) .child(storage(info.attachment.size)), ) .child(delete) } impl Yumush { pub(super) fn storage_body(&self, entity: &Entity, t: Tokens) -> Vec { let usage = div() .flex() .gap(px(space::XL)) .child(usage_card( w().builder_storage.users, self.chat .admin .builder_usage_users .iter() .map(|usage| (usage.user.username.as_str(), usage.bytes, usage.count)) .collect(), t, )) .child(usage_card( w().builder_storage.communities, self.chat .admin .builder_usage_communities .iter() .map(|usage| (usage.community.as_str(), usage.bytes, usage.count)) .collect(), t, )); let controls = toolbar(vec![ fixed( size::THUMB, field(t, &self.chat.admin.builder_attachment_user), ), fixed( size::THUMB, field(t, &self.chat.admin.builder_attachment_community), ), crate::gui::parts::button( "search_attachments", w().builder_storage.search, crate::gui::parts::Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.search_attachments(window, cx) }), ) .into_any_element(), ]) .into_any_element(); let rows: Vec
= self .chat .admin .builder_attachments .iter() .map(|info| { let pending = self.armed(Armed::DeleteAttachment(info.attachment.id)); attachment_row(info, pending, entity, t) }) .collect(); let body = if rows.is_empty() { crate::gui::parts::empty(w().builder_storage.no_attachments, t) } else { div().flex().flex_col().children(rows) }; let attachments = card() .child(card_header( w().builder_storage.attachments, vec![controls], t, )) .child(body); vec![ usage.into_any_element(), attachments.into_any_element(), self.backups_card(entity, t).into_any_element(), ] } pub(super) fn load_storage(&self, window: &mut Window, cx: &mut Context) { self.search_attachments(window, cx); self.load_backups(window, cx); self.fetch_builder( window, cx, |network| async move { network.storage_usage().await }, |yumush, (users, communities), cx| { yumush.chat.admin.builder_usage_users = users; yumush.chat.admin.builder_usage_communities = communities; cx.notify(); }, ); } pub(super) fn search_attachments(&self, window: &mut Window, cx: &mut Context) { let Some(user_id) = self.filter_user(&self.chat.admin.builder_attachment_user, cx) else { window.push_notification(Notification::error(w().builder_storage.unknown_user), cx); return; }; let Some(community_id) = self.filter_community(&self.chat.admin.builder_attachment_community, cx) else { window.push_notification( Notification::error(w().builder_storage.unknown_community), cx, ); return; }; let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { let attachments = match network .call(ListAttachments { user_id, community_id, }) .await { Ok(attachments) => attachments, Err(error) => { notify_error(&this, cx, error); return; } }; let _ = this.update(cx, |yumush, cx| { yumush.chat.admin.builder_attachments = attachments; cx.notify(); }); }) .detach(); } pub(super) fn delete_builder_attachment( &mut self, attachment_id: Id, window: &mut Window, cx: &mut Context, ) { if !self.arm(Armed::DeleteAttachment(attachment_id), cx) { return; } let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(DeleteAttachment { attachment_id }).await { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| { yumush.load_storage(window, cx); }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } }