@typeparam TViewModel @typeparam TSecrets @implements IDisposable @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 { private readonly CancellationTokenSource _cts = new(); [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; public void Dispose() { _cts.Cancel(); _cts.Dispose(); } 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, _cts.Token); } 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, _cts.Token); await LoadMediaSources(); } } } private async Task RefreshLibraries(int mediaSourceId) => await RefreshLibrariesCommand(mediaSourceId); }