yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·7.2 MiB·4b67e80·Tahinli·1 hour ago·400 commits
| 1 | use common::model::Id; |
| 2 | use common::{ |
| 3 | call::{DeleteAttachment, ListAttachments}, |
| 4 | model::AttachmentInfo, |
| 5 | }; |
| 6 | use gpui_kit::{ |
| 7 | AnyElement, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, |
| 8 | assets::IconName, |
| 9 | component::{Icon, WindowExt, notification::Notification}, |
| 10 | div, px, relative, |
| 11 | }; |
| 12 | |
| 13 | use super::ui::{ |
| 14 | card, card_header, confirmed, delete_icon_button, fixed, list_card, list_row, toolbar, |
| 15 | }; |
| 16 | use crate::gui::{ |
| 17 | Palette, Yumush, |
| 18 | community_settings::user_name, |
| 19 | entity_action, millis_time, notify_error, storage, |
| 20 | style::{radius, size, space, text}, |
| 21 | text, |
| 22 | widget::field, |
| 23 | }; |
| 24 | use crate::lang::w; |
| 25 | |
| 26 | fn share(bytes: u64, max: u64) -> f32 { |
| 27 | if max == 0 { |
| 28 | 0. |
| 29 | } else { |
| 30 | bytes as f32 / max as f32 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn usage_row(name: &str, bytes: u64, count: u64, fraction: f32, palette: Palette) -> Div { |
| 35 | div() |
| 36 | .px(px(space::LG)) |
| 37 | .py(px(space::SM)) |
| 38 | .flex() |
| 39 | .flex_col() |
| 40 | .gap(px(space::SM)) |
| 41 | .child( |
| 42 | div() |
| 43 | .flex() |
| 44 | .items_center() |
| 45 | .gap(px(space::SM)) |
| 46 | .child( |
| 47 | div() |
| 48 | .flex_1() |
| 49 | .min_w_0() |
| 50 | .truncate() |
| 51 | .text_size(text(text::LABEL)) |
| 52 | .text_color(palette.text) |
| 53 | .child(name.to_string()), |
| 54 | ) |
| 55 | .child( |
| 56 | div() |
| 57 | .flex_shrink_0() |
| 58 | .text_size(text(text::LABEL)) |
| 59 | .font_weight(FontWeight::MEDIUM) |
| 60 | .text_color(palette.text) |
| 61 | .child(storage(bytes)), |
| 62 | ) |
| 63 | .child( |
| 64 | div() |
| 65 | .w(px(size::ROW)) |
| 66 | .flex_shrink_0() |
| 67 | .flex() |
| 68 | .justify_end() |
| 69 | .text_size(text(text::CAPTION)) |
| 70 | .text_color(palette.muted) |
| 71 | .child(count.to_string()), |
| 72 | ), |
| 73 | ) |
| 74 | .child( |
| 75 | div() |
| 76 | .h(px(space::XXS)) |
| 77 | .rounded(px(radius::SM)) |
| 78 | .bg(palette.hover) |
| 79 | .child( |
| 80 | div() |
| 81 | .h_full() |
| 82 | .w(relative(fraction)) |
| 83 | .rounded(px(radius::SM)) |
| 84 | .bg(palette.accent), |
| 85 | ), |
| 86 | ) |
| 87 | } |
| 88 | |
| 89 | fn attachment_row( |
| 90 | info: &AttachmentInfo, |
| 91 | pending: bool, |
| 92 | entity: &Entity<Yumush>, |
| 93 | palette: Palette, |
| 94 | ) -> Div { |
| 95 | let attachment_id = info.attachment.id; |
| 96 | let delete = delete_icon_button( |
| 97 | format!("delete_attachment_{}", info.attachment.id), |
| 98 | pending, |
| 99 | palette, |
| 100 | entity_action(entity, move |yumush, window, cx| { |
| 101 | yumush.delete_builder_attachment(attachment_id, window, cx) |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | list_row() |
| 106 | .child( |
| 107 | Icon::new(IconName::File) |
| 108 | .size(px(size::ICON)) |
| 109 | .flex_shrink_0() |
| 110 | .text_color(palette.muted), |
| 111 | ) |
| 112 | .child( |
| 113 | div() |
| 114 | .flex_1() |
| 115 | .min_w_0() |
| 116 | .flex() |
| 117 | .flex_col() |
| 118 | .gap(px(space::XXS)) |
| 119 | .child( |
| 120 | div() |
| 121 | .min_w_0() |
| 122 | .text_size(text(text::LABEL)) |
| 123 | .text_color(palette.text) |
| 124 | .child(super::super::glyph::line( |
| 125 | info.attachment.name.clone().into(), |
| 126 | super::super::text(super::super::style::text::LABEL), |
| 127 | )), |
| 128 | ) |
| 129 | .child( |
| 130 | div() |
| 131 | .truncate() |
| 132 | .text_size(text(text::CAPTION)) |
| 133 | .text_color(palette.muted) |
| 134 | .child(format!( |
| 135 | "{} · {} · {}", |
| 136 | user_name(&info.uploader), |
| 137 | info.community, |
| 138 | millis_time(info.attachment.created_at, crate::lang::w().common.date) |
| 139 | )), |
| 140 | ), |
| 141 | ) |
| 142 | .child( |
| 143 | div() |
| 144 | .flex_shrink_0() |
| 145 | .text_size(text(text::CAPTION)) |
| 146 | .text_color(palette.muted) |
| 147 | .child(storage(info.attachment.size)), |
| 148 | ) |
| 149 | .child(delete) |
| 150 | } |
| 151 | |
| 152 | impl Yumush { |
| 153 | pub(super) fn storage_body(&self, entity: &Entity<Self>, palette: Palette) -> Vec<AnyElement> { |
| 154 | let user_max = self |
| 155 | .chat |
| 156 | .builder_usage_users |
| 157 | .iter() |
| 158 | .map(|usage| usage.bytes) |
| 159 | .max() |
| 160 | .unwrap_or(0); |
| 161 | let community_max = self |
| 162 | .chat |
| 163 | .builder_usage_communities |
| 164 | .iter() |
| 165 | .map(|usage| usage.bytes) |
| 166 | .max() |
| 167 | .unwrap_or(0); |
| 168 | let users: Vec<Div> = self |
| 169 | .chat |
| 170 | .builder_usage_users |
| 171 | .iter() |
| 172 | .map(|usage| { |
| 173 | usage_row( |
| 174 | &usage.user.username, |
| 175 | usage.bytes, |
| 176 | usage.count, |
| 177 | share(usage.bytes, user_max), |
| 178 | palette, |
| 179 | ) |
| 180 | }) |
| 181 | .collect(); |
| 182 | let communities: Vec<Div> = self |
| 183 | .chat |
| 184 | .builder_usage_communities |
| 185 | .iter() |
| 186 | .map(|usage| { |
| 187 | usage_row( |
| 188 | &usage.community, |
| 189 | usage.bytes, |
| 190 | usage.count, |
| 191 | share(usage.bytes, community_max), |
| 192 | palette, |
| 193 | ) |
| 194 | }) |
| 195 | .collect(); |
| 196 | let usage = div() |
| 197 | .flex() |
| 198 | .gap(px(space::XL)) |
| 199 | .child( |
| 200 | div().flex_1().min_w_0().child( |
| 201 | list_card( |
| 202 | Some(w().builder_storage.users), |
| 203 | users, |
| 204 | w().builder_storage.no_data, |
| 205 | palette, |
| 206 | ) |
| 207 | .h_full(), |
| 208 | ), |
| 209 | ) |
| 210 | .child( |
| 211 | div().flex_1().min_w_0().child( |
| 212 | list_card( |
| 213 | Some(w().builder_storage.communities), |
| 214 | communities, |
| 215 | w().builder_storage.no_data, |
| 216 | palette, |
| 217 | ) |
| 218 | .h_full(), |
| 219 | ), |
| 220 | ); |
| 221 | |
| 222 | let controls = toolbar(vec![ |
| 223 | fixed( |
| 224 | size::THUMB, |
| 225 | field(palette, &self.chat.builder_attachment_user), |
| 226 | ), |
| 227 | fixed( |
| 228 | size::THUMB, |
| 229 | field(palette, &self.chat.builder_attachment_community), |
| 230 | ), |
| 231 | crate::gui::parts::button( |
| 232 | "search_attachments", |
| 233 | w().builder_storage.search, |
| 234 | crate::gui::parts::Press::Plain, |
| 235 | palette.tokens, |
| 236 | entity_action(entity, |yumush, window, cx| { |
| 237 | yumush.search_attachments(window, cx) |
| 238 | }), |
| 239 | ) |
| 240 | .into_any_element(), |
| 241 | ]) |
| 242 | .into_any_element(); |
| 243 | let rows: Vec<Div> = self |
| 244 | .chat |
| 245 | .builder_attachments |
| 246 | .iter() |
| 247 | .map(|info| { |
| 248 | let pending = self.chat.pending_attachment_delete == Some(info.attachment.id); |
| 249 | attachment_row(info, pending, entity, palette) |
| 250 | }) |
| 251 | .collect(); |
| 252 | let body = if rows.is_empty() { |
| 253 | crate::gui::parts::empty(w().builder_storage.no_attachments, palette.tokens) |
| 254 | } else { |
| 255 | div().flex().flex_col().children(rows) |
| 256 | }; |
| 257 | let attachments = card(palette) |
| 258 | .child(card_header( |
| 259 | w().builder_storage.attachments, |
| 260 | vec![controls], |
| 261 | palette, |
| 262 | )) |
| 263 | .child(body); |
| 264 | |
| 265 | vec![ |
| 266 | usage.into_any_element(), |
| 267 | attachments.into_any_element(), |
| 268 | self.backups_card(entity, palette).into_any_element(), |
| 269 | ] |
| 270 | } |
| 271 | |
| 272 | pub(super) fn load_storage(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 273 | if !self.is_builder() { |
| 274 | return; |
| 275 | } |
| 276 | self.search_attachments(window, cx); |
| 277 | self.load_backups(window, cx); |
| 278 | self.fetch_builder( |
| 279 | window, |
| 280 | cx, |
| 281 | |network| async move { network.storage_usage().await }, |
| 282 | |yumush, (users, communities), cx| { |
| 283 | yumush.chat.builder_usage_users = users; |
| 284 | yumush.chat.builder_usage_communities = communities; |
| 285 | cx.notify(); |
| 286 | }, |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | pub(super) fn search_attachments(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 291 | let Some(user_id) = self.filter_user(&self.chat.builder_attachment_user, cx) else { |
| 292 | window.push_notification(Notification::error(w().builder_storage.unknown_user), cx); |
| 293 | return; |
| 294 | }; |
| 295 | let Some(community_id) = self.filter_community(&self.chat.builder_attachment_community, cx) |
| 296 | else { |
| 297 | window.push_notification( |
| 298 | Notification::error(w().builder_storage.unknown_community), |
| 299 | cx, |
| 300 | ); |
| 301 | return; |
| 302 | }; |
| 303 | let network = self.network.clone(); |
| 304 | |
| 305 | cx.spawn_in(window, async move |this, cx| { |
| 306 | let attachments = match network |
| 307 | .call(ListAttachments { |
| 308 | user_id, |
| 309 | community_id, |
| 310 | }) |
| 311 | .await |
| 312 | { |
| 313 | Ok(attachments) => attachments, |
| 314 | Err(error) => { |
| 315 | notify_error(&this, cx, error); |
| 316 | return; |
| 317 | } |
| 318 | }; |
| 319 | |
| 320 | let _ = this.update(cx, |yumush, cx| { |
| 321 | yumush.chat.builder_attachments = attachments; |
| 322 | cx.notify(); |
| 323 | }); |
| 324 | }) |
| 325 | .detach(); |
| 326 | } |
| 327 | |
| 328 | pub(super) fn delete_builder_attachment( |
| 329 | &mut self, |
| 330 | attachment_id: Id, |
| 331 | window: &mut Window, |
| 332 | cx: &mut Context<Self>, |
| 333 | ) { |
| 334 | if !confirmed(&mut self.chat.pending_attachment_delete, attachment_id) { |
| 335 | cx.notify(); |
| 336 | return; |
| 337 | } |
| 338 | let network = self.network.clone(); |
| 339 | |
| 340 | cx.spawn_in(window, async move |this, cx| { |
| 341 | match network.call(DeleteAttachment { attachment_id }).await { |
| 342 | Ok(()) => { |
| 343 | let _ = this.update_in(cx, |yumush, window, cx| { |
| 344 | yumush.load_storage(window, cx); |
| 345 | }); |
| 346 | } |
| 347 | Err(error) => notify_error(&this, cx, error), |
| 348 | } |
| 349 | }) |
| 350 | .detach(); |
| 351 | } |
| 352 | } |