use common::{ call::{CreateBackup, DeleteBackup, ListBackups}, model::BackupInfo, }; use gpui_kit::{ Context, Div, Entity, IntoElement, ParentElement, Styled, Window, assets::IconName, component::Icon, div, px, }; use super::{ save_directory, ui::{card, card_header, confirmed, delete_icon_button, icon_button, list_row, rows_body}, }; use crate::gui::{ Yumush, entity_action, millis_time, notify_error, storage, style::{Tokens, size, text}, }; use crate::lang::w; const STORAGE_WIDTH: f32 = 72.; fn backup_row(info: &BackupInfo, pending: bool, entity: &Entity, t: Tokens) -> Div { let export_name = info.name.clone(); let export = icon_button( format!("export_backup_{}", info.name), IconName::Download, w().builder_backups.export, t.dim(), t, entity_action(entity, move |yumush, window, cx| { yumush.export_backup(&export_name, window, cx) }), ); let delete_name = info.name.clone(); let delete = delete_icon_button( format!("delete_backup_{}", info.name), pending, t, entity_action(entity, move |yumush, window, cx| { yumush.delete_backup(&delete_name, window, cx) }), ); list_row() .child( Icon::new(IconName::HardDrive) .size(px(size::ICON)) .flex_shrink_0() .text_color(t.dim()), ) .child( div() .flex_1() .min_w_0() .truncate() .text_size(t.text(text::LABEL)) .text_color(t.paper()) .child(info.name.clone()), ) .child( div() .flex_shrink_0() .text_size(t.text(text::CAPTION)) .text_color(t.dim()) .child(millis_time(info.created_ms, crate::lang::w().common.date)), ) .child( div() .w(px(STORAGE_WIDTH)) .flex_shrink_0() .flex() .justify_end() .text_size(t.text(text::CAPTION)) .text_color(t.dim()) .child(storage(info.size_bytes)), ) .child(export) .child(delete) } impl Yumush { pub(super) fn backups_card(&self, entity: &Entity, t: Tokens) -> Div { let create = crate::gui::parts::button( "create_backup", w().builder_backups.create, crate::gui::parts::Press::Plain, t, entity_action(entity, |yumush, window, cx| { yumush.create_backup(window, cx) }), ) .into_any_element(); let rows: Vec
= self .desk .backups .iter() .map(|info| { let pending = self.desk.pending_backup.as_deref() == Some(info.name.as_str()); backup_row(info, pending, entity, t) }) .collect(); card() .child(card_header(w().builder_backups.backups, vec![create], t)) .child(rows_body(rows, w().builder_backups.no_backups, t)) } pub(super) fn load_backups(&self, window: &mut Window, cx: &mut Context) { self.fetch_builder( window, cx, |network| async move { network.call(ListBackups).await }, |yumush, backups, cx| { yumush.desk.backups = backups; cx.notify(); }, ); } fn create_backup(&self, window: &mut Window, cx: &mut Context) { let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { match network.call(CreateBackup).await { Ok(_) => { let _ = this.update_in(cx, |yumush, window, cx| yumush.load_backups(window, cx)); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } fn delete_backup(&mut self, name: &str, window: &mut Window, cx: &mut Context) { if !confirmed(&mut self.desk.pending_backup, name.to_string()) { cx.notify(); return; } let network = self.network.clone(); let name = name.to_string(); cx.spawn_in(window, async move |this, cx| { match network.call(DeleteBackup { name }).await { Ok(()) => { let _ = this.update_in(cx, |yumush, window, cx| yumush.load_backups(window, cx)); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } fn export_backup(&self, name: &str, window: &mut Window, cx: &mut Context) { let target = cx.prompt_for_new_path(&save_directory(), Some(name)); let network = self.network.clone(); let name = name.to_string(); cx.spawn_in(window, async move |this, cx| { let target = match target.await { Ok(Ok(Some(target))) => target, Ok(Err(error)) => { notify_error(&this, cx, error); return; } _ => return, }; if let Err(error) = network.download_backup(&name, target).await { notify_error(&this, cx, error); } }) .detach(); } }