Files
ersatztv/ErsatzTV/Pages/Templates.razor
T
Jason DoveandGitHub 21f4439aa4 block ui improvements (#2646)
* template editor improvements

* more keyboard navigation

* replace template tree view with template table
2025-11-13 19:25:44 -06:00

310 lines
12 KiB
Plaintext

@page "/templates"
@using ErsatzTV.Application.Scheduling
@implements IDisposable
@inject ILogger<Templates> Logger
@inject ISnackbar Snackbar
@inject IMediator Mediator
@inject IDialogService Dialog
@inject NavigationManager NavigationManager
<MudForm Style="max-height: 100%">
<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">Template Groups</MudText>
<MudDivider Class="mb-6"/>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex">
<MudText>Template Group Name</MudText>
</div>
<MudTextField @bind-Value="_templateGroupName" For="@(() => _templateGroupName)"/>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex"></div>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(_ => AddTemplateGroup())" StartIcon="@Icons.Material.Filled.Add">
Add Template Group
</MudButton>
</MudStack>
<MudText Typo="Typo.h5" Class="mt-10 mb-2">Templates</MudText>
<MudDivider Class="mb-6"/>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex">
<MudText>Template Group</MudText>
</div>
<MudSelect T="string" ValueChanged="@UpdateSelectedTemplateGroup">
@foreach (TemplateGroupViewModel templateGroup in _templateGroups)
{
<MudSelectItem Value="@templateGroup.Name">@templateGroup.Name</MudSelectItem>
}
</MudSelect>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex">
<MudText>Template Name</MudText>
</div>
<MudTextField @bind-Value="_templateName" For="@(() => _templateName)"/>
</MudStack>
<MudStack Row="true" Breakpoint="Breakpoint.SmAndDown" Class="form-field-stack gap-md-8 mb-5">
<div class="d-flex"></div>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(_ => AddTemplate())" StartIcon="@Icons.Material.Filled.Add">
Add Template
</MudButton>
</MudStack>
<MudTable Hover="true"
Dense="true"
Class="mt-8"
Items="@_templates"
GroupBy="@_groupDefinition"
GroupHeaderStyle="background-color:var(--mud-palette-appbarbackground)"
RowStyle="background-color:var(--mud-palette-background-gray)"
Filter="new Func<TemplateViewModel,bool>(FilterTemplates)">
<ColGroup>
<MudHidden Breakpoint="Breakpoint.Xs">
<col style="width: 60px;"/>
<col/>
<col style="width: 180px;"/>
</MudHidden>
</ColGroup>
<ToolBarContent>
<MudTextField T="string"
ValueChanged="@(s => OnSearch(s))"
Placeholder="Search for templates"
Adornment="Adornment.Start"
AdornmentIcon="@Icons.Material.Filled.FilterList"
Clearable="true">
</MudTextField>
</ToolBarContent>
<GroupHeaderTemplate>
<MudTd Class="mud-table-cell-custom-group">
@($"{context.Key}")
</MudTd>
<MudTd>
<div style="align-items: center; display: flex;">
<div style="width: 48px;"></div>
<div style="width: 48px;"></div>
<MudTooltip Text="Delete Template Group">
<MudIconButton Icon="@Icons.Material.Filled.Delete"
OnClick="@(_ => DeleteTemplateGroup(context.Items?.FirstOrDefault()))">
</MudIconButton>
</MudTooltip>
</div>
</MudTd>
</GroupHeaderTemplate>
<RowTemplate>
<MudTd>@context.Name</MudTd>
<MudTd>
<div class="d-flex">
@if (context.Id >= 0)
{
<MudTooltip Text="Edit Template">
<MudIconButton Icon="@Icons.Material.Filled.Edit"
Href="@($"templates/{context.Id}")">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Copy Template">
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy"
OnClick="@(_ => CopyTemplate(context))">
</MudIconButton>
</MudTooltip>
<MudTooltip Text="Delete Template">
<MudIconButton Icon="@Icons.Material.Filled.Delete"
OnClick="@(_ => DeleteTemplate(context))">
</MudIconButton>
</MudTooltip>
}
else
{
<div style="height: 48px; width: 48px"></div>
}
</div>
</MudTd>
</RowTemplate>
</MudTable>
</MudContainer>
</div>
</MudForm>
@code {
private CancellationTokenSource _cts;
private readonly List<TemplateViewModel> _templates = [];
private List<TemplateGroupViewModel> _templateGroups = [];
private TemplateGroupViewModel _selectedTemplateGroup;
private string _templateGroupName;
private string _templateName;
private string _searchString;
public void Dispose()
{
_cts?.Cancel();
_cts?.Dispose();
}
protected override async Task OnParametersSetAsync()
{
_cts?.Cancel();
_cts?.Dispose();
_cts = new CancellationTokenSource();
var token = _cts.Token;
try
{
_templateGroups = await Mediator.Send(new GetAllTemplateGroups(), token);
_templates.Clear();
_templates.AddRange(await Mediator.Send(new GetAllTemplates(), token));
}
catch (OperationCanceledException)
{
// do nothing
}
}
private readonly TableGroupDefinition<TemplateViewModel> _groupDefinition = new()
{
GroupName = "Group",
Indentation = false,
Expandable = true,
Selector = (e) => e.GroupName
};
private async Task AddTemplateGroup()
{
if (!string.IsNullOrWhiteSpace(_templateGroupName))
{
Either<BaseError, TemplateGroupViewModel> result = await Mediator.Send(new CreateTemplateGroup(_templateGroupName), _cts.Token);
foreach (BaseError error in result.LeftToSeq())
{
Snackbar.Add(error.Value, Severity.Error);
Logger.LogError("Unexpected error adding template group: {Error}", error.Value);
}
foreach (TemplateGroupViewModel templateGroup in result.RightToSeq())
{
_templateGroupName = null;
_templateGroups = await Mediator.Send(new GetAllTemplateGroups(), _cts.Token);
_selectedTemplateGroup = _templateGroups.Find(tg => tg.Id == templateGroup.Id);
_templates.Clear();
_templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token));
await InvokeAsync(StateHasChanged);
}
}
}
private void UpdateSelectedTemplateGroup(string templateGroupName)
{
_selectedTemplateGroup = _templateGroups.Find(tg => tg.Name == templateGroupName);
InvokeAsync(StateHasChanged);
}
private async Task AddTemplate()
{
if (_selectedTemplateGroup is not null && !string.IsNullOrWhiteSpace(_templateName))
{
Either<BaseError, TemplateViewModel> result = await Mediator.Send(new CreateTemplate(_selectedTemplateGroup.Id, _templateName), _cts.Token);
foreach (BaseError error in result.LeftToSeq())
{
Snackbar.Add(error.Value, Severity.Error);
Logger.LogError("Unexpected error adding template: {Error}", error.Value);
}
if (result.IsRight)
{
_templates.Clear();
_templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token));
_templateName = null;
await InvokeAsync(StateHasChanged);
}
}
}
private void OnSearch(string query)
{
_searchString = query;
}
private bool FilterTemplates(TemplateViewModel template) => FilterTemplates(template, _searchString);
private bool FilterTemplates(TemplateViewModel template, string searchString)
{
if (string.IsNullOrWhiteSpace(searchString))
{
return true;
}
if (template.Name.Contains(searchString, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
private async Task DeleteTemplateGroup(TemplateViewModel template)
{
if (template is null)
{
return;
}
var parameters = new DialogParameters { { "EntityType", "template group" }, { "EntityName", template.GroupName } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<DeleteDialog>("Delete Template Group", parameters, options);
DialogResult result = await dialog.Result;
if (result is not null && !result.Canceled)
{
await Mediator.Send(new DeleteTemplateGroup(template.TemplateGroupId), _cts.Token);
if (_selectedTemplateGroup?.Id == template.TemplateGroupId)
{
_selectedTemplateGroup = null;
}
_templateGroups = await Mediator.Send(new GetAllTemplateGroups(), _cts.Token);
_templates.Clear();
_templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token));
await InvokeAsync(StateHasChanged);
}
}
private async Task CopyTemplate(TemplateViewModel template)
{
var parameters = new DialogParameters { { "TemplateId", template.Id } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<CopyTemplateDialog>("Copy Template", parameters, options);
DialogResult dialogResult = await dialog.Result;
if (dialogResult is { Canceled: false, Data: TemplateViewModel data })
{
NavigationManager.NavigateTo($"templates/{data.Id}");
}
}
private async Task DeleteTemplate(TemplateViewModel template)
{
if (template is null)
{
return;
}
var parameters = new DialogParameters { { "EntityType", "template" }, { "EntityName", template.Name } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
IDialogReference dialog = await Dialog.ShowAsync<DeleteDialog>("Delete Template", parameters, options);
DialogResult result = await dialog.Result;
if (result is not null && !result.Canceled)
{
await Mediator.Send(new DeleteTemplate(template.Id), _cts.Token);
_templates.Clear();
_templates.AddRange(await Mediator.Send(new GetAllTemplates(), _cts.Token));
await InvokeAsync(StateHasChanged);
}
}
}