@page "/media/plex" @using ErsatzTV.Application.Plex @using ErsatzTV.Application.Plex.Commands @using ErsatzTV.Application.Plex.Queries @implements IDisposable @inject IDialogService Dialog @inject IMediator Mediator @inject IEntityLocker Locker @inject ISnackbar Snackbar @inject ILogger Logger @inject IJSRuntime JsRuntime Plex Media Sources Name Address @context.Name @context.Address
@if (_mediaSources.Any()) { Sign out of plex } else { Sign in to plex }
@code { private List _mediaSources; protected override async Task OnParametersSetAsync() => await LoadMediaSources(); protected override void OnInitialized() => Locker.OnPlexChanged += PlexChanged; private async Task LoadMediaSources() => _mediaSources = await Mediator.Send(new GetAllPlexMediaSources()); private async Task SignOutOfPlex() { var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small }; IDialogReference dialog = Dialog.Show("Sign out of Plex", options); DialogResult result = await dialog.Result; if (!result.Cancelled) { if (Locker.LockPlex()) { await Mediator.Send(new SignOutOfPlex()); await LoadMediaSources(); } } } private async Task AddPlexMediaSource() { if (Locker.LockPlex()) { Either maybeUrl = await Mediator.Send(new StartPlexPinFlow()); await maybeUrl.Match( async url => await JsRuntime.InvokeAsync("open", new object[] { url, "_blank" }), error => { Locker.UnlockPlex(); Snackbar.Add(error.Value, Severity.Error); Logger.LogError("Unexpected error generating plex auth app url: {Error}", error.Value); return Task.CompletedTask; }); } } private void PlexChanged(object sender, EventArgs e) { InvokeAsync(LoadMediaSources); InvokeAsync(StateHasChanged); } void IDisposable.Dispose() => Locker.OnPlexChanged -= PlexChanged; }