@using Moonlight.App.Services @using Moonlight.App.Helpers @using Logging.Net @using BlazorContextMenu @using Moonlight.App.Database.Entities @using Moonlight.App.Events @using Moonlight.App.Services.Interop @inject ServerService ServerService @inject NavigationManager NavigationManager @inject AlertService AlertService @inject ToastService ToastService @inject ClipboardService ClipboardService @inject EventSystem Event @inject SmartTranslateService SmartTranslateService @implements IDisposable @if (3 > AllBackups.Length) { } else { }
@foreach (var backup in AllBackups) { }
Restore Copy url Download Delete
@code { [CascadingParameter] public Server CurrentServer { get; set; } private ServerBackup[]? AllBackups; private LazyLoader LazyLoader; protected override void OnInitialized() { Event.On("wings.backups.create", this, async (backup) => { if (AllBackups == null) return; if (AllBackups.Any(x => x.Id == backup.Id)) { await Task.Delay(TimeSpan.FromSeconds(1)); await ToastService.Success(SmartTranslateService.Translate("Backup successfully created")); await LazyLoader.Reload(); } }); Event.On("wings.backups.createFailed", this, async (backup) => { if (AllBackups == null) return; if (AllBackups.Any(x => x.Id == backup.Id)) { await ToastService.Error(SmartTranslateService.Translate("Backup creation failed")); await LazyLoader.Reload(); } }); Event.On("wings.backups.delete", this, async (backup) => { if (AllBackups == null) return; if (AllBackups.Any(x => x.Id == backup.Id)) { await ToastService.Success(SmartTranslateService.Translate("Backup successfully deleted")); await LazyLoader.Reload(); } }); Event.On("wings.backups.restore", this, async (backup) => { if (AllBackups == null) return; if (AllBackups.Any(x => x.Id == backup.Id)) { await ToastService.Success(SmartTranslateService.Translate("Backup successfully restored")); } }); } private async Task Refresh(LazyLoader lazyLoader) { await InvokeAsync(StateHasChanged); await lazyLoader.SetText(SmartTranslateService.Translate("Loading backups")); AllBackups = await ServerService.GetBackups(CurrentServer, true); await InvokeAsync(StateHasChanged); } private async Task Download(ServerBackup serverBackup) { var url = await ServerService.DownloadBackup(CurrentServer, serverBackup); NavigationManager.NavigateTo(url); await ToastService.Success(SmartTranslateService.Translate("Backup download successfully started")); /* try { } catch (Exception e) { Logger.Warn("Error starting backup download"); Logger.Warn(e); await ToastService.Error(SmartTranslateService.Translate("Backup download failed")); }*/ } private async Task CopyUrl(ServerBackup serverBackup) { var url = await ServerService.DownloadBackup(CurrentServer, serverBackup); await ClipboardService.Copy(url); await AlertService.Success( SmartTranslateService.Translate("Success"), SmartTranslateService.Translate("Backup URL successfully copied to your clipboard")); /* try { } catch (Exception e) { Logger.Warn("Error copying backup url"); Logger.Warn(e); await ToastService.Error(SmartTranslateService.Translate("An unknown error occured while generating backup url")); }*/ } private async Task Delete(ServerBackup serverBackup) { await ToastService.Info(SmartTranslateService.Translate("Backup deletion started")); await ServerService.DeleteBackup(CurrentServer, serverBackup); /* try { } catch (Exception e) { Logger.Warn("Error deleting backup"); Logger.Warn(e); await ToastService.Error(SmartTranslateService.Translate("An unknown error occured while starting backup deletion")); }*/ } private async Task Restore(ServerBackup serverBackup) { await ServerService.RestoreBackup(CurrentServer, serverBackup); await ToastService.Info(SmartTranslateService.Translate("Backup restore started")); /* try { } catch (Exception e) { Logger.Warn("Error restoring backup"); Logger.Warn(e); await ToastService.Error(SmartTranslateService.Translate("An unknown error occured while restoring a backup")); }*/ } private async Task Create() { await ToastService.Info(SmartTranslateService.Translate("Started backup creation")); await ServerService.CreateBackup(CurrentServer); await LazyLoader.Reload(); /* try { // Modify the backup list so no reload needed. Also the create event will work //var list = AllBackups!.ToList(); //list.Add(backup); //AllBackups = list.ToArray(); } catch (Exception e) { Logger.Warn("Error creating backup"); Logger.Warn(e); await ToastService.Error(SmartTranslateService.Translate("An unknown error has occured while creating a backup")); }*/ } private async Task OnContextMenuClick(ItemClickEventArgs args) { var backup = (ServerBackup)args.Data; switch (args.MenuItem.Id) { case "delete": await Delete(backup); break; case "copyurl": await CopyUrl(backup); break; case "restore": await Restore(backup); break; case "download": await Download(backup); break; } await Refresh(LazyLoader); } public async void Dispose() { await Event.Off("wings.backups.create", this); await Event.Off("wings.backups.createFailed", this); await Event.Off("wings.backups.restore", this); await Event.Off("wings.backups.delete", this); } }