From 3270039a6adf4064d3fd8267a90fe60a6a130ee5 Mon Sep 17 00:00:00 2001 From: Moritz <101179677+Moritz-Deiaco@users.noreply.github.com> Date: Tue, 14 May 2024 14:43:54 +0200 Subject: [PATCH] improved admin page Added the logic for a more modular admin page, where every feature can add cards and components with just a call of a function --- Moonlight/Core/CoreFeature.cs | 3 + .../Abstractions/Feature/AdminComponent.cs | 12 +++ .../Abstractions/Feature/UiInitContext.cs | 43 +++++++++- .../UI/Components/Cards/AdminUserCard.razor | 35 ++++++++ Moonlight/Core/UI/Views/Admin/Index.razor | 79 ++++++------------- Moonlight/Features/Servers/ServersFeature.cs | 3 + .../Components/Cards/AdminServersCard.razor | 35 ++++++++ 7 files changed, 155 insertions(+), 55 deletions(-) create mode 100644 Moonlight/Core/Models/Abstractions/Feature/AdminComponent.cs create mode 100644 Moonlight/Core/UI/Components/Cards/AdminUserCard.razor create mode 100644 Moonlight/Features/Servers/UI/Components/Cards/AdminServersCard.razor diff --git a/Moonlight/Core/CoreFeature.cs b/Moonlight/Core/CoreFeature.cs index df698bf..3316b1e 100644 --- a/Moonlight/Core/CoreFeature.cs +++ b/Moonlight/Core/CoreFeature.cs @@ -15,6 +15,7 @@ using Moonlight.Core.Models.Abstractions.Feature; using Moonlight.Core.Models.Enums; using Moonlight.Core.Repositories; using Moonlight.Core.Services; +using Moonlight.Core.UI.Components.Cards; namespace Moonlight.Core; @@ -207,6 +208,8 @@ public class CoreFeature : MoonlightFeature context.AddSidebarItem("Users", "bxs-group", "/admin/users", needsExactMatch: false, isAdmin: true); context.AddSidebarItem("System", "bxs-component", "/admin/sys", needsExactMatch: false, isAdmin: true); + context.AddAdminCard(index: int.MinValue); + return Task.CompletedTask; } diff --git a/Moonlight/Core/Models/Abstractions/Feature/AdminComponent.cs b/Moonlight/Core/Models/Abstractions/Feature/AdminComponent.cs new file mode 100644 index 0000000..b17e9db --- /dev/null +++ b/Moonlight/Core/Models/Abstractions/Feature/AdminComponent.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Components; + +namespace Moonlight.Core.Models.Abstractions.Feature; + +public class AdminComponent +{ + public RenderFragment Component { get; set; } + + public int Index { get; set; } + + public int RequiredPermissionLevel { get; set; } +} \ No newline at end of file diff --git a/Moonlight/Core/Models/Abstractions/Feature/UiInitContext.cs b/Moonlight/Core/Models/Abstractions/Feature/UiInitContext.cs index c63fbc1..172885e 100644 --- a/Moonlight/Core/Models/Abstractions/Feature/UiInitContext.cs +++ b/Moonlight/Core/Models/Abstractions/Feature/UiInitContext.cs @@ -1,4 +1,9 @@ -using System.Reflection; +using System.ComponentModel; +using System.Reflection; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Options; +using Moonlight.Core.UI.Components.Partials; +using IComponent = Microsoft.AspNetCore.Components.IComponent; namespace Moonlight.Core.Models.Abstractions.Feature; @@ -6,7 +11,9 @@ public class UiInitContext { public List SidebarItems { get; set; } = new(); public List RouteAssemblies { get; set; } = new(); - + public List AdminPageComponents { get; set; } = new(); + public List AdminPageCards { get; set; } = new(); + public void EnablePages() { var assembly = typeof(T).Assembly; @@ -15,6 +22,38 @@ public class UiInitContext RouteAssemblies.Add(assembly); } + public void AddAdminComponent(int index = 0, int requiredPermissionLevel = 0) where T : IComponent + { + AdminPageComponents.Add( + new AdminComponent() + { + Component = builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }, + Index = index, + RequiredPermissionLevel = requiredPermissionLevel + } + ); + } + + public void AddAdminCard(int index = 0, int requiredPermissionLevel = 0) where T : IComponent + { + AdminPageCards.Add( + new AdminComponent() + { + Component = builder => + { + builder.OpenComponent(0); + builder.CloseComponent(); + }, + Index = index, + RequiredPermissionLevel = requiredPermissionLevel + } + ); + } + public void AddSidebarItem(string name, string icon, string target, bool isAdmin = false, bool needsExactMatch = false, int index = 0) { SidebarItems.Add(new SidebarItem() diff --git a/Moonlight/Core/UI/Components/Cards/AdminUserCard.razor b/Moonlight/Core/UI/Components/Cards/AdminUserCard.razor new file mode 100644 index 0000000..7194197 --- /dev/null +++ b/Moonlight/Core/UI/Components/Cards/AdminUserCard.razor @@ -0,0 +1,35 @@ +@using MoonCore.Abstractions +@using Moonlight.Core.Database.Entities + +@inject Repository UserRepository + + +
+
+
+
+ Users +
+ + @UserCount + +
+
+ + + +
+
+
+
+ +@code { + + private int UserCount; + + protected override async Task OnInitializedAsync() + { + UserCount = UserRepository.Get().Count(); + } + +} \ No newline at end of file diff --git a/Moonlight/Core/UI/Views/Admin/Index.razor b/Moonlight/Core/UI/Views/Admin/Index.razor index 2cb5718..8d46947 100644 --- a/Moonlight/Core/UI/Views/Admin/Index.razor +++ b/Moonlight/Core/UI/Views/Admin/Index.razor @@ -1,73 +1,46 @@ @page "/admin" @using MoonCore.Abstractions @using Moonlight.Core.Database.Entities +@using Moonlight.Core.Services @using Moonlight.Features.Servers.Entities @inject Repository ServerRepository -@inject Repository UserRepository + +@inject FeatureService FeatureService +@inject IdentityService IdentityService @attribute [RequirePermission(999)] -
-
- - + + @foreach (var adminComponent in FeatureService.UiContext.AdminPageComponents.OrderBy(x => x.Index).ToArray()) + { + if (IdentityService.CurrentUser.Permissions >= adminComponent.RequiredPermissionLevel) + { +
+ @adminComponent +
+ } + } -@* This is just the start of this admin page, there will still be added more during the developement process *@ @code { - private int ServerCount; - private int UserCount; - + private async Task Load(LazyLoader arg) { - await Task.Run(() => - { - arg.SetText("Loading Information..."); - ServerCount = ServerRepository.Get().Count(); - UserCount = UserRepository.Get().Count(); - }); + await arg.SetText("Loading Information..."); + } } \ No newline at end of file diff --git a/Moonlight/Features/Servers/ServersFeature.cs b/Moonlight/Features/Servers/ServersFeature.cs index acd1445..30422cb 100644 --- a/Moonlight/Features/Servers/ServersFeature.cs +++ b/Moonlight/Features/Servers/ServersFeature.cs @@ -10,6 +10,7 @@ using Moonlight.Features.Servers.Http.Middleware; using Moonlight.Features.Servers.Implementations.Diagnose; using Moonlight.Features.Servers.Models.Enums; using Moonlight.Features.Servers.Services; +using Moonlight.Features.Servers.UI.Components.Cards; namespace Moonlight.Features.Servers; @@ -104,6 +105,8 @@ public class ServersFeature : MoonlightFeature context.AddSidebarItem("Servers", "bx-server", "/servers", isAdmin: false, needsExactMatch: false); context.AddSidebarItem("Servers", "bx-server", "/admin/servers", isAdmin: true, needsExactMatch: false); + context.AddAdminCard(); + return Task.CompletedTask; } } \ No newline at end of file diff --git a/Moonlight/Features/Servers/UI/Components/Cards/AdminServersCard.razor b/Moonlight/Features/Servers/UI/Components/Cards/AdminServersCard.razor new file mode 100644 index 0000000..8c8ce49 --- /dev/null +++ b/Moonlight/Features/Servers/UI/Components/Cards/AdminServersCard.razor @@ -0,0 +1,35 @@ +@using MoonCore.Abstractions +@using Moonlight.Features.Servers.Entities + +@inject Repository ServerRepository + + +
+
+
+
+ Servers +
+ + @ServerCount + +
+
+ + + +
+
+
+
+ +@code { + + private int ServerCount; + + protected override async Task OnInitializedAsync() + { + ServerCount = ServerRepository.Get().Count(); + } + +} \ No newline at end of file