Files
ersatztv/ErsatzTV/Pages/PlexMediaSources.razor
T
Jason DoveandGitHub 5d081ceeff fix editorconfig and run code cleanup (#2324)
* fix formatting rules

* reformat ersatztv

* reformat ersatztv.application

* reformat ersatztv.core

* refactor ersatztv.core.tests

* reformat ersatztv.ffmpeg

* reformat ersatztv.ffmpeg.tests

* reformat ersatztv.infrastructure

* cleanup infra mysql

* cleanup infra sqlite

* cleanup infra tests

* cleanup ersatztv.scanner

* cleanup ersatztv.scanner.tests

* sln cleanup

* update dependencies
2025-08-16 14:44:48 +00:00

171 lines
6.3 KiB
Plaintext

@page "/media/sources/plex"
@using ErsatzTV.Application.Plex
@using ErsatzTV.Core.Interfaces.Plex
@implements IDisposable
@inject IDialogService Dialog
@inject IMediator Mediator
@inject IEntityLocker Locker
@inject ISnackbar Snackbar
@inject ILogger<PlexMediaSources> Logger
@inject IJSRuntime JsRuntime
@inject IPlexSecretStore PlexSecretStore
@inject ChannelWriter<IScannerBackgroundServiceRequest> ScannerWorkerChannel
<MudForm Style="max-height: 100%">
<MudPaper Square="true" Style="display: flex; height: 64px; min-height: 64px; width: 100%; z-index: 100; align-items: center">
@if (_mediaSources.Any())
{
<MudButton Variant="Variant.Filled"
Color="Color.Error"
OnClick="@(_ => SignOutOfPlex())"
Disabled="@Locker.IsPlexLocked()"
Class="ml-8">
Sign out of plex
</MudButton>
}
else
{
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="@(_ => AddPlexMediaSource())"
Disabled="@Locker.IsPlexLocked()"
Class="ml-8">
Sign in to plex
</MudButton>
}
@if (_mediaSources.Any() && !_isAuthorized)
{
<MudButton Variant="Variant.Filled"
Color="Color.Secondary"
OnClick="@(_ => AddPlexMediaSource())"
Disabled="@Locker.IsPlexLocked()"
Class="ml-4">
Fix Plex Credentials
</MudButton>
}
</MudPaper>
<div class="d-flex flex-column" style="height: 100vh; overflow-x: auto">
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8">
<MudText Typo="Typo.h5" Class="mb-2">Plex Media Sources</MudText>
<MudDivider Class="mb-6"/>
<MudTable T="PlexMediaSourceViewModel" Hover="true" Dense="true" Items="_mediaSources">
<ColGroup>
<MudHidden Breakpoint="Breakpoint.Xs">
<col/>
<col/>
<col style="width: 120px;"/>
</MudHidden>
</ColGroup>
<HeaderContent>
<MudTh>Name</MudTh>
<MudTh>Address</MudTh>
<MudTh/>
</HeaderContent>
<RowTemplate>
<MudTd>@context.Name</MudTd>
<MudTd Style="overflow-wrap: anywhere;">@context.Address</MudTd>
<MudTd>
<div style="align-items: center; display: flex;">
<MudTooltip Text="Refresh Libraries">
<MudIconButton Icon="@Icons.Material.Filled.Refresh"
Disabled="@(Locker.IsPlexLocked())"
OnClick="@(_ => RefreshLibraries(context.Id))">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Edit Libraries">
<MudIconButton Icon="@Icons.Material.Filled.VideoLibrary"
Href="@($"media/sources/plex/{context.Id}/libraries")">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Edit Path Replacements">
<MudIconButton Icon="@Icons.Material.Filled.Folder"
Href="@($"media/sources/plex/{context.Id}/paths")">
</MudIconButton>
</MudTooltip>
</div>
</MudTd>
</RowTemplate>
</MudTable>
</MudContainer>
</div>
</MudForm>
@code {
private List<PlexMediaSourceViewModel> _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 => 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 = await Dialog.ShowAsync<SignOutOfPlexDialog>("Sign out of Plex", options);
DialogResult result = await dialog.Result;
if (result is { Canceled: false })
{
if (Locker.LockPlex())
{
await Mediator.Send(new SignOutOfPlex());
await LoadMediaSources();
}
}
}
private async Task AddPlexMediaSource()
{
if (Locker.LockPlex())
{
Either<BaseError, string> maybeUrl = await Mediator.Send(new StartPlexPinFlow());
await maybeUrl.Match(
async url =>
{
try
{
await JsRuntime.InvokeAsync<object>("open", [url, "_blank"]);
}
catch (Exception)
{
// ignored
}
},
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)
{
try
{
await InvokeAsync(LoadMediaSources);
await InvokeAsync(StateHasChanged);
}
catch (Exception)
{
// do nothing
}
}
private async Task RefreshLibraries(int mediaSourceId) =>
await ScannerWorkerChannel.WriteAsync(new SynchronizePlexLibraries(mediaSourceId));
void IDisposable.Dispose() => Locker.OnPlexChanged -= PlexChanged;
}