@page "/media/sources/plex" @using ErsatzTV.Core.Interfaces.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 @inject IPlexSecretStore _plexSecretStore @inject ChannelWriter _channel Plex Media Sources Name Address @context.Name @context.Address
@if (_mediaSources.Any()) { Sign out of plex } else { Sign in to plex } @if (_mediaSources.Any() && !_isAuthorized) { Fix Plex Credentials }
@code { private List _mediaSources = new(); private bool _isAuthorized; protected override async Task OnParametersSetAsync() => await LoadMediaSources(); protected override void OnInitialized() => _locker.OnPlexChanged += PlexChanged; private async Task LoadMediaSources() { _isAuthorized = await _plexSecretStore.GetUserAuthTokens().Map(list => Prelude.Optional(list).Flatten().Any()); _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 async void PlexChanged(object sender, EventArgs e) { await InvokeAsync(LoadMediaSources); await InvokeAsync(StateHasChanged); } private async Task RefreshLibraries(int mediaSourceId) => await _channel.WriteAsync(new SynchronizePlexLibraries(mediaSourceId)); void IDisposable.Dispose() => _locker.OnPlexChanged -= PlexChanged; }