[CHANGE] Add account dialog

This commit is contained in:
max
2025-09-04 18:05:56 +02:00
parent a8cfbbe0db
commit 431a103fac
12 changed files with 382 additions and 111 deletions

View File

@@ -0,0 +1,106 @@
<ForcedLoadingOverlay Visible="_isLoading"/>
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">Add new account</MudText>
</TitleContent>
<DialogContent>
<MudStack Spacing="2">
@if (_showCookieTextImport)
{
<MudTextField @bind-Value="@_cookieDomain" Required HelperText="Domain"/>
<MudTextField AutoGrow @bind-Value="@_cookieText" HelperText="Cookie text" Placeholder="EXAMPLE: Cookie1=Value1; Cookie2=Value2;"/>
<MudButton Variant="Variant.Outlined" Disabled="@(string.IsNullOrWhiteSpace(_cookieDomain) || string.IsNullOrWhiteSpace(_cookieText))" OnClick="ApplyTextCookies">Apply</MudButton>
}
<MudStack Row Spacing="2" AlignItems="AlignItems.Stretch" Justify="Justify.SpaceEvenly" StretchItems="StretchItems.All">
<MudStack Spacing="2" Style="width: 100%">
<MudTextField Label="UserAgent" Required @bind-Value="@Client.UserAgent"/>
</MudStack>
<MudSimpleTable Style="width: 100%" Bordered Dense Elevation="0" Outlined Square Hover>
<tbody>
<tr>
<td>Account id:</td>
<td>@Client.Id</td>
</tr>
<tr>
<td>Account name:</td>
<td>@Client.AccountName</td>
</tr>
<tr>
<td>User agent:</td>
<td>@Client.UserAgent</td>
</tr>
<tr>
<td>Logged in:</td>
<td style="@($"color: {(Client.ClientState?.LoggedIn ?? false ? "green" : "red")}")">@Client.ClientState?.LoggedIn</td>
</tr>
<tr>
<td>InnerTube API key:</td>
<td>@Client.ClientState?.InnertubeApiKey</td>
</tr>
<tr>
<td>InnerTube client version:</td>
<td>@Client.ClientState?.InnerTubeClientVersion</td>
</tr>
<tr>
<td>Language:</td>
<td>@Client.ClientState?.InnerTubeContext?.InnerTubeClient?.HLanguage</td>
</tr>
</tbody>
</MudSimpleTable>
</MudStack>
<MudDataGrid Items="Client.CookieContainer.GetAllCookies()" Dense Elevation="0" Outlined>
<Header>
<MudStack Class="ma-2">
<MudText>Cookies</MudText>
</MudStack>
<MudStack Row Spacing="2" Class="ma-1">
<MudTooltip Text="Add cookie">
<MudIconButton Icon="@Icons.Material.Filled.Add" Size="Size.Small" Color="Color.Success" Disabled="_showCookieTextImport" OnClick="AddCookie"/>
</MudTooltip>
<MudTooltip Text="Add from text">
<MudIconButton Icon="@Icons.Material.Filled.InsertDriveFile" Size="Size.Small" Color="Color.Primary" OnClick="ToggleCookieTextImport"/>
</MudTooltip>
</MudStack>
</Header>
<Columns>
<TemplateColumn Title="Name">
<CellTemplate>
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Name" Immediate/>
</CellTemplate>
</TemplateColumn>
<TemplateColumn Title="Domain">
<CellTemplate>
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Domain" Immediate/>
</CellTemplate>
</TemplateColumn>
<TemplateColumn Title="Value">
<CellTemplate>
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Value" Immediate/>
</CellTemplate>
</TemplateColumn>
<PropertyColumn Title="Expires" Property="x => x.Expires"/>
<TemplateColumn>
<CellTemplate>
<MudTooltip Text="Remove">
<MudIconButton Size="Size.Small" Icon="@Icons.Material.Filled.Remove" Color="Color.Error" OnClick="@(() => RemoveCookie(context.Item))"/>
</MudTooltip>
</CellTemplate>
</TemplateColumn>
</Columns>
</MudDataGrid>
</MudStack>
</DialogContent>
<DialogActions>
<MudStack Spacing="2" Row>
<MudButton Color="Color.Error" OnClick="() => MudDialog?.Cancel()" Variant="Variant.Outlined">Cancel</MudButton>
<MudButton Color="Color.Info" Variant="Variant.Outlined" OnClick="ValidateAccount" Disabled="@(!CanValidate())">Validate</MudButton>
<MudButton Color="Color.Primary" Variant="Variant.Outlined" Disabled="@(!CanSave())" OnClick="OnSave">Save</MudButton>
</MudStack>
</DialogActions>
</MudDialog>

View File

@@ -0,0 +1,117 @@
using System.Net;
using Manager.YouTube;
using Microsoft.AspNetCore.Components;
using MudBlazor;
namespace Manager.App.Components.Dialogs
{
public partial class AccountDialog : ComponentBase
{
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
[Parameter] public string DefaultUserAgent { get; set; } = "";
public YouTubeClient Client { get; set; } = new();
private bool _isLoading;
private bool _showCookieTextImport;
private string _cookieText = "";
private string _cookieDomain = ".youtube.com";
protected override void OnInitialized()
{
Client.UserAgent = DefaultUserAgent;
base.OnInitialized();
}
private void AddCookie()
{
Client.CookieContainer.Add(new Cookie { Name = "SET_NAME", Domain = ".youtube.com" });
}
private async Task RemoveCookie(Cookie? cookie)
{
if (cookie == null)
{
return;
}
cookie.Expired = true;
await InvokeAsync(StateHasChanged);
}
private void ToggleCookieTextImport()
{
_showCookieTextImport =! _showCookieTextImport;
}
private void ApplyTextCookies()
{
_showCookieTextImport = false;
var cookies = ParseCookieHeader(_cookieText, _cookieDomain);
Client.CookieContainer.Add(cookies);
_cookieText = string.Empty;
StateHasChanged();
}
public static CookieCollection ParseCookieHeader(string cookieHeader, string domain = "")
{
var collection = new CookieCollection();
if (string.IsNullOrWhiteSpace(cookieHeader))
return collection;
var cookies = cookieHeader.Split(';', StringSplitOptions.RemoveEmptyEntries);
foreach (var cookieStr in cookies)
{
var parts = cookieStr.Split('=', 2);
if (parts.Length == 2)
{
var name = parts[0].Trim();
var value = parts[1].Trim();
// Escape invalid characters
var safeName = Uri.EscapeDataString(name);
var safeValue = Uri.EscapeDataString(value);
var cookie = new Cookie(safeName, safeValue);
if (!string.IsNullOrEmpty(domain))
cookie.Domain = domain;
collection.Add(cookie);
}
}
return collection;
}
private bool CanValidate()
{
if (string.IsNullOrWhiteSpace(Client.UserAgent) || Client.CookieContainer.Count <= 0)
{
return false;
}
return true;
}
private bool CanSave()
{
return Client.ClientState is { LoggedIn: true };
}
private async Task ValidateAccount()
{
_isLoading = true;
await Client.GetStateAsync();
_isLoading = false;
}
private void OnSave()
{
MudDialog?.Close(DialogResult.Ok(Client));
}
}
}

View File

@@ -1,61 +1,18 @@
@page "/Channels"
@using Manager.App.Models.Settings
@using Microsoft.Extensions.Options
@inject ILibraryService LibraryService
@inject IDialogService DialogService
@inject IOptions<LibrarySettings> LibraryOptions
<PageTitle>Channels</PageTitle>
<MudDialog @bind-Visible="@_addAccountDialogVisible" Options="_dialogOptions">
<TitleContent>
<MudText Typo="Typo.h6">Add new account</MudText>
</TitleContent>
<DialogContent>
<MudDataGrid Items="_cookies" Dense Elevation="0" Outlined>
<Header>
<MudStack Class="ma-2">
<MudText>Cookies</MudText>
</MudStack>
<MudStack Row Spacing="2" Class="ma-1">
<MudTooltip Text="Add cookie">
<MudIconButton Icon="@Icons.Material.Filled.Add" Size="Size.Small" Color="Color.Success" OnClick="() => _cookies.Add(new HttpCookie())"/>
</MudTooltip>
<MudTooltip Text="Add from text">
<MudIconButton Icon="@Icons.Material.Filled.InsertDriveFile" Size="Size.Small" Color="Color.Primary" Disabled/>
</MudTooltip>
</MudStack>
</Header>
<Columns>
<TemplateColumn Title="Name">
<CellTemplate>
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Name" Immediate/>
</CellTemplate>
</TemplateColumn>
<TemplateColumn Title="Value">
<CellTemplate>
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Value" Immediate/>
</CellTemplate>
</TemplateColumn>
<TemplateColumn>
<CellTemplate>
<MudTooltip Text="Remove">
<MudIconButton Size="Size.Small" Icon="@Icons.Material.Filled.Remove" Color="Color.Error" OnClick="() => _cookies.Remove(context.Item)"/>
</MudTooltip>
</CellTemplate>
</TemplateColumn>
</Columns>
</MudDataGrid>
</DialogContent>
<DialogActions>
<MudStack Spacing="2" Row>
<MudButton Color="Color.Error" OnClick="() => _addAccountDialogVisible = false" Variant="Variant.Outlined">Cancel</MudButton>
<MudButton Color="Color.Primary" Variant="Variant.Outlined" Disabled>Save</MudButton>
</MudStack>
</DialogActions>
</MudDialog>
<MudStack Spacing="2">
<MudPaper Elevation="0" Outlined>
<MudStack Row Class="ma-2">
<MudButton IconSize="Size.Small" StartIcon="@Icons.Material.Filled.Add" Variant="Variant.Outlined" OnClick="() => _addAccountDialogVisible = true">Add account</MudButton>
<MudButton IconSize="Size.Small" StartIcon="@Icons.Material.Filled.Add" Variant="Variant.Outlined" OnClick="OnAddAccountDialogAsync">Add account</MudButton>
</MudStack>
</MudPaper>

View File

@@ -1,4 +1,6 @@
using Manager.App.Components.Dialogs;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
using Microsoft.AspNetCore.Components;
using MudBlazor;
@@ -6,23 +8,26 @@ namespace Manager.App.Components.Pages;
public partial class Channels : ComponentBase
{
private bool _addAccountDialogVisible;
private DialogOptions _dialogOptions = new() { BackdropClick = false, CloseButton = true, FullWidth = true };
private List<HttpCookie> _cookies = [];
private readonly DialogOptions _dialogOptions = new() { BackdropClick = false, CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.ExtraLarge };
private async Task<TableData<ChannelEntity>> ServerReload(TableState state, CancellationToken token)
{
var results = await LibraryService.GetChannelAccountsAsync(state.Page * state.PageSize, state.PageSize, token);
if (!results.IsSuccess)
{
return new TableData<ChannelEntity>();
}
return new TableData<ChannelEntity> { Items = results.Value, TotalItems = results.Total };
return !results.IsSuccess ? new TableData<ChannelEntity>() : new TableData<ChannelEntity> { Items = results.Value, TotalItems = results.Total };
}
}
private async Task OnAddAccountDialogAsync()
{
var libSettings = LibraryOptions.Value;
var parameters = new DialogParameters<AccountDialog> { { x => x.DefaultUserAgent, libSettings.DefaultUserAgent } };
var dialog = await DialogService.ShowAsync<AccountDialog>("Add account", parameters, _dialogOptions);
var result = await dialog.Result;
public record HttpCookie()
{
public string Name { get; set; }
public string Value { get; set; }
if (result == null || result.Canceled || result.Data == null)
{
return;
}
var client = (YouTubeClient)result.Data;
}
}

View File

@@ -41,7 +41,9 @@ public class ClientManager : BackgroundService
container.Add(cookieColl);
}
var ytClient = new YouTubeClient(container, accountEntity.UserAgent ?? "");
var ytClient = new YouTubeClient();
//ytClient.CookieContainer = container;
ytClient.UserAgent = accountEntity.UserAgent;
await ytClient.GetStateAsync();
return ytClient;