Implemented theme import and export

This commit is contained in:
2026-02-12 11:09:38 +01:00
parent dd44e5bb86
commit 6f941a220c
5 changed files with 146 additions and 11 deletions

View File

@@ -18,6 +18,8 @@
@inject IAuthorizationService AuthorizationService
@inject HttpClient HttpClient
<InputFile OnChange="OnFileSelectedAsync" id="import-theme" class="hidden" multiple accept=".yml"/>
<div class="flex flex-row justify-between mt-5">
<div class="flex flex-col">
<h1 class="text-xl font-semibold">Themes</h1>
@@ -26,7 +28,15 @@
</div>
</div>
<div class="flex flex-row gap-x-1.5">
<Button @onclick="CreateAsync" disabled="@(!CreateAccess.Succeeded)">
<Button Variant="ButtonVariant.Outline">
<Slot>
<label for="import-theme" @attributes="context">
<HardDriveUploadIcon/>
Import
</label>
</Slot>
</Button>
<Button @onclick="Create" disabled="@(!CreateAccess.Succeeded)">
<PlusIcon/>
Create
</Button>
@@ -39,13 +49,14 @@
<TemplateColumn Identifier="@nameof(ThemeDto.Name)" IsFilterable="true" Title="Name">
<CellTemplate>
<TableCell>
<a class="text-primary flex flex-row items-center" href="#" @onclick="() => EditAsync(context)" @onclick:preventDefault>
<a class="text-primary flex flex-row items-center" href="#" @onclick="() => Edit(context)"
@onclick:preventDefault>
@context.Name
@if (context.IsEnabled)
{
<span class="ms-2">
<CheckIcon ClassName="size-4 text-green-400" />
<CheckIcon ClassName="size-4 text-green-400"/>
</span>
}
</a>
@@ -70,7 +81,13 @@
</Slot>
</DropdownMenuTrigger>
<DropdownMenuContent SideOffset="2">
<DropdownMenuItem OnClick="() => EditAsync(context)" Disabled="@(!EditAccess.Succeeded)">
<DropdownMenuItem OnClick="() => Download(context)">
Download
<DropdownMenuShortcut>
<HardDriveDownloadIcon/>
</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem OnClick="() => Edit(context)" Disabled="@(!EditAccess.Succeeded)">
Edit
<DropdownMenuShortcut>
<PenIcon/>
@@ -96,9 +113,9 @@
@code
{
[CascadingParameter] public Task<AuthenticationState> AuthState { get; set; }
private DataGrid<ThemeDto> Grid;
private AuthorizationResult EditAccess;
private AuthorizationResult DeleteAccess;
private AuthorizationResult CreateAccess;
@@ -106,7 +123,7 @@
protected override async Task OnInitializedAsync()
{
var authState = await AuthState;
EditAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Themes.Edit);
DeleteAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Themes.Delete);
CreateAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Themes.Create);
@@ -125,9 +142,11 @@
return new DataGridResponse<ThemeDto>(response!.Data, response.TotalLength);
}
private void CreateAsync() => Navigation.NavigateTo("/admin/system/themes/create");
private void Create() => Navigation.NavigateTo("/admin/system/themes/create");
private void EditAsync(ThemeDto theme) => Navigation.NavigateTo($"/admin/system/themes/{theme.Id}");
private void Edit(ThemeDto theme) => Navigation.NavigateTo($"/admin/system/themes/{theme.Id}");
private void Download(ThemeDto theme) => Navigation.NavigateTo($"api/admin/themes/{theme.Id}/export", true);
private async Task DeleteAsync(ThemeDto theme)
{
@@ -145,4 +164,31 @@
}
);
}
private async Task OnFileSelectedAsync(InputFileChangeEventArgs eventArgs)
{
var files = eventArgs.GetMultipleFiles();
foreach (var browserFile in files)
{
await using var contentStream = browserFile.OpenReadStream(browserFile.Size);
var response = await HttpClient.PostAsync(
"api/admin/themes/import",
new StreamContent(contentStream)
);
response.EnsureSuccessStatusCode();
var importedTheme = await response
.Content
.ReadFromJsonAsync<ThemeDto>(Constants.SerializerOptions);
if (importedTheme == null)
continue;
await Grid.RefreshAsync();
await ToastService.SuccessAsync("Theme Import", $"Successfully imported theme {importedTheme.Name}");
}
}
}