@typeparam TViewModel
@typeparam TSecrets
@using Unit = LanguageExt.Unit
@using ErsatzTV.Core.Interfaces.MediaSources
@typeparam TMediaSource
@inject IMediator _mediator
@inject IDialogService _dialog
@inject IEntityLocker _locker
@Name Media Sources
Name
Address
@context.Name
@context.Address
@if (_mediaSources.Any())
{
Disconnect @Name
}
else
{
Connect @Name
}
@if (_mediaSources.Any() && !_isAuthorized)
{
Fix @Name Connection
}
@code {
[Parameter]
public string Name { get; set; }
[Parameter]
public IRequest> GetAllMediaSourcesCommand { get; set; }
[Parameter]
public IRequest> DisconnectCommand { get; set; }
[Parameter]
public Func RefreshLibrariesCommand { get; set; }
[Parameter]
public IRemoteMediaSourceSecretStore SecretStore { get; set; }
private List _mediaSources = new();
private bool _isAuthorized;
protected override async Task OnParametersSetAsync() => await LoadMediaSources();
private async Task LoadMediaSources()
{
_isAuthorized = await SecretStore.ReadSecrets()
.Map(secrets => !string.IsNullOrWhiteSpace(secrets.Address) && !string.IsNullOrWhiteSpace(secrets.ApiKey));
_mediaSources = await _mediator.Send(GetAllMediaSourcesCommand);
}
private async Task Disconnect()
{
var parameters = new DialogParameters { { "Name", Name } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small };
IDialogReference dialog = _dialog.Show($"Disconnect {Name}", parameters, options);
DialogResult result = await dialog.Result;
if (!result.Cancelled)
{
if (_locker.LockRemoteMediaSource())
{
await _mediator.Send(DisconnectCommand);
await LoadMediaSources();
}
}
}
private async Task RefreshLibraries(int mediaSourceId) => await RefreshLibrariesCommand(mediaSourceId);
}