@using ErsatzTV.Application.Libraries @using Microsoft.Extensions.Caching.Memory @implements IDisposable @inject IMediator Mediator @inject IMemoryCache MemoryCache @inject ISnackbar Snackbar @inject ILogger Logger Select the destination library @foreach (LocalLibraryViewModel library in _libraries) { @library.Name } Cancel Move To Library @code { private readonly CancellationTokenSource _cts = new(); [CascadingParameter] MudDialogInstance MudDialog { get; set; } [Parameter] public LibraryMediaKind MediaKind { get; set; } [Parameter] public int SourceLibraryId { get; set; } private LocalLibraryViewModel _newLibrary; private string _newLibraryName; private List _libraries; private LocalLibraryViewModel _selectedLibrary; private record DummyModel; private readonly DummyModel _dummyModel = new(); public void Dispose() { _cts.Cancel(); _cts.Dispose(); } private bool CanSubmit() => _selectedLibrary != null && (_selectedLibrary != _newLibrary || !string.IsNullOrWhiteSpace(_newLibraryName)); protected override async Task OnParametersSetAsync() { _newLibrary = new LocalLibraryViewModel(-1, "(New Library)", MediaKind, -1); _libraries = await Mediator.Send(new GetAllLocalLibraries(), _cts.Token) .Map(list => list.Filter(ll => ll.MediaKind == MediaKind && ll.Id != SourceLibraryId)) .Map(list => new[] { _newLibrary }.Append(list.OrderBy(vm => vm.Name, StringComparer.CurrentCultureIgnoreCase)).ToList()); if (MemoryCache.TryGetValue("MoveLocalLibraryPathDialog.SelectedLibraryId", out int id)) { _selectedLibrary = _libraries.SingleOrDefault(c => c.Id == id) ?? _newLibrary; } else { _selectedLibrary = _newLibrary; } } private async Task Submit() { if (!CanSubmit()) { return; } if (_selectedLibrary == _newLibrary) { Either maybeResult = await Mediator.Send(new CreateLocalLibrary(_newLibraryName, MediaKind, new List()), _cts.Token); maybeResult.Match( collection => { MemoryCache.Set("MoveLocalLibraryPathDialog.SelectedLibraryId", collection.Id); MudDialog.Close(DialogResult.Ok(collection)); }, error => { Snackbar.Add(error.Value, Severity.Error); Logger.LogError("Error creating new local library: {Error}", error.Value); MudDialog.Close(DialogResult.Cancel()); }); } else { MemoryCache.Set("MoveLocalLibraryPathDialog.SelectedLibraryId", _selectedLibrary.Id); MudDialog.Close(DialogResult.Ok(_selectedLibrary)); } } private async Task Cancel(MouseEventArgs e) { // this is gross, but [enter] seems to sometimes trigger cancel instead of submit if (e.Detail == 0) { await Submit(); } else { MudDialog.Cancel(); } } }