From a12aa66660b2a5c56b155aa8b02b3c055d3332c6 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Sat, 14 Dec 2024 19:11:50 +0200 Subject: [PATCH] Updated the plugin webpage --- .../PluginManagement/PluginListEndpoint.cs | 5 +- DiscordBotCore/Others/JsonManager.cs | 35 ++++++++++ .../Components/CustomTags/Marketplace.razor | 22 +++++++ .../CustomTags/SettingsComponent.razor | 66 +++++++++++++++++++ .../Components/Pages/SidebarPages/Home.razor | 2 +- .../Pages/SidebarPages/PluginMarket.razor | 19 ++++++ .../Pages/SidebarPages/Settings.razor | 66 +------------------ DiscordBotWebUI/DiscordBotWebUI.csproj | 6 +- DiscordBotWebUI/Models/PluginModel.cs | 8 +++ 9 files changed, 159 insertions(+), 70 deletions(-) create mode 100644 DiscordBotWebUI/Components/CustomTags/Marketplace.razor create mode 100644 DiscordBotWebUI/Components/CustomTags/SettingsComponent.razor create mode 100644 DiscordBotWebUI/Models/PluginModel.cs diff --git a/DiscordBotCore/API/Endpoints/PluginManagement/PluginListEndpoint.cs b/DiscordBotCore/API/Endpoints/PluginManagement/PluginListEndpoint.cs index ac81653..7258b42 100644 --- a/DiscordBotCore/API/Endpoints/PluginManagement/PluginListEndpoint.cs +++ b/DiscordBotCore/API/Endpoints/PluginManagement/PluginListEndpoint.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using DiscordBotCore.Interfaces.API; using DiscordBotCore.Others; +using DiscordBotCore.Plugin; namespace DiscordBotCore.API.Endpoints.PluginManagement; @@ -11,7 +12,9 @@ public class PluginListEndpoint : IEndpoint public async Task HandleRequest(string? jsonRequest) { var onlineInfos = await Application.CurrentApplication.PluginManager.GetPluginsList(); - var response = await JsonManager.ConvertToJsonString(onlineInfos); + + var response = await JsonManager.ConvertToJson(onlineInfos, [nameof(PluginOnlineInfo.Name), nameof(PluginOnlineInfo.Author), nameof(PluginOnlineInfo.Version)]); + return ApiResponse.From(response, true); } } diff --git a/DiscordBotCore/Others/JsonManager.cs b/DiscordBotCore/Others/JsonManager.cs index 347de0a..335092c 100644 --- a/DiscordBotCore/Others/JsonManager.cs +++ b/DiscordBotCore/Others/JsonManager.cs @@ -1,13 +1,48 @@ using System; +using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Reflection; using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace DiscordBotCore.Others; public static class JsonManager { + + public static async Task ConvertToJson(List data, string[] propertyNamesToUse) + { + if (data == null) throw new ArgumentNullException(nameof(data)); + if (propertyNamesToUse == null) throw new ArgumentNullException(nameof(propertyNamesToUse)); + + // Use reflection to filter properties dynamically + var filteredData = data.Select(item => + { + if (item == null) return null; + + var type = typeof(T); + var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + // Create a dictionary with specified properties and their values + var selectedProperties = propertyInfos + .Where(p => propertyNamesToUse.Contains(p.Name)) + .ToDictionary(p => p.Name, p => p.GetValue(item)); + + return selectedProperties; + }).ToList(); + + // Serialize the filtered data to JSON + var options = new JsonSerializerOptions + { + WriteIndented = true, // For pretty-print JSON; remove if not needed + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + + return await Task.FromResult(JsonSerializer.Serialize(filteredData, options)); + } public static async Task ConvertToJsonString(T Data) { diff --git a/DiscordBotWebUI/Components/CustomTags/Marketplace.razor b/DiscordBotWebUI/Components/CustomTags/Marketplace.razor new file mode 100644 index 0000000..2d3b798 --- /dev/null +++ b/DiscordBotWebUI/Components/CustomTags/Marketplace.razor @@ -0,0 +1,22 @@ +@using DiscordBotWebUI.Models + + + + + + + + + + + + +@code { + [Parameter] + public required List PluginModels { get; set; } + + [Parameter] + public required Action OnPluginDownloadClick { get; set; } +} \ No newline at end of file diff --git a/DiscordBotWebUI/Components/CustomTags/SettingsComponent.razor b/DiscordBotWebUI/Components/CustomTags/SettingsComponent.razor new file mode 100644 index 0000000..b1cdf49 --- /dev/null +++ b/DiscordBotWebUI/Components/CustomTags/SettingsComponent.razor @@ -0,0 +1,66 @@ +@inject DialogService DialogService + +
+ + + + + + + + + + + + +
+ + +@code { + private string TokenString { get; set; } + private string PrefixString { get; set; } + private string ServerIdsString { get; set; } + + [Parameter] + public required Action SaveSettings { get; set; } + + private async void SaveButtonClick() + { + var response = await DialogService.Confirm("Saving this requires a bot restart.\nRestart now?", "Save Settings", new ConfirmOptions() + { + CloseDialogOnEsc = false, + ShowClose = false, + CloseDialogOnOverlayClick = false, + OkButtonText = "Restart Now", + CancelButtonText = "Don't Save" + }); + + if (!response.HasValue) + { + return; + } + + if (!response.Value) + { + return; + } + + SaveSettings.Invoke(); + } + +} \ No newline at end of file diff --git a/DiscordBotWebUI/Components/Pages/SidebarPages/Home.razor b/DiscordBotWebUI/Components/Pages/SidebarPages/Home.razor index 311a4e0..dea44e9 100644 --- a/DiscordBotWebUI/Components/Pages/SidebarPages/Home.razor +++ b/DiscordBotWebUI/Components/Pages/SidebarPages/Home.razor @@ -1,5 +1,5 @@ @page "/" - +Home page @code { } \ No newline at end of file diff --git a/DiscordBotWebUI/Components/Pages/SidebarPages/PluginMarket.razor b/DiscordBotWebUI/Components/Pages/SidebarPages/PluginMarket.razor index 0a965f2..7a1b2d4 100644 --- a/DiscordBotWebUI/Components/Pages/SidebarPages/PluginMarket.razor +++ b/DiscordBotWebUI/Components/Pages/SidebarPages/PluginMarket.razor @@ -1,5 +1,24 @@ @page "/plugins" +@using DiscordBotWebUI.Components.CustomTags +@using DiscordBotWebUI.Models + + + @code { + private List _PluginModels; + private async void OnModelSelected(string itemName) + { + Console.WriteLine(itemName); + } + + + protected override async Task OnInitializedAsync() + { + _PluginModels = new List() + { + new PluginModel() {PluginName = "Test", PluginAuthor = "Andrei", PluginDescription = "Interesting plugin"} + }; + } } \ No newline at end of file diff --git a/DiscordBotWebUI/Components/Pages/SidebarPages/Settings.razor b/DiscordBotWebUI/Components/Pages/SidebarPages/Settings.razor index 2ec4a2f..9feaba1 100644 --- a/DiscordBotWebUI/Components/Pages/SidebarPages/Settings.razor +++ b/DiscordBotWebUI/Components/Pages/SidebarPages/Settings.razor @@ -1,65 +1,5 @@ @page "/settings" -@inject DialogService DialogService; +@using DiscordBotWebUI.Components.CustomTags +Settings -
- - - - - - - - - - - - -
- - -@code { - private string TokenString { get; set; } - private string PrefixString { get; set; } - private string ServerIdsString { get; set; } - - private async void SaveButtonClick() - { - var response = await DialogService.Confirm("Saving this requires a bot restart.\nRestart now?", "Save Settings", new ConfirmOptions() - { - CloseDialogOnEsc = false, - ShowClose = false, - CloseDialogOnOverlayClick = false, - OkButtonText = "Restart Now", - CancelButtonText = "Don't Save" - }); - - if (!response.HasValue) - { - return; - } - - if (!response.Value) - { - return; - } - - //TODO: Restart bot request - - } - -} \ No newline at end of file + \ No newline at end of file diff --git a/DiscordBotWebUI/DiscordBotWebUI.csproj b/DiscordBotWebUI/DiscordBotWebUI.csproj index 13dccca..a7ce3da 100644 --- a/DiscordBotWebUI/DiscordBotWebUI.csproj +++ b/DiscordBotWebUI/DiscordBotWebUI.csproj @@ -7,10 +7,6 @@ Linux - - - - @@ -43,7 +39,7 @@ - + diff --git a/DiscordBotWebUI/Models/PluginModel.cs b/DiscordBotWebUI/Models/PluginModel.cs new file mode 100644 index 0000000..6b79daa --- /dev/null +++ b/DiscordBotWebUI/Models/PluginModel.cs @@ -0,0 +1,8 @@ +namespace DiscordBotWebUI.Models; + +public class PluginModel +{ + public string PluginName { get; set; } + public string PluginAuthor { get; set; } + public string PluginDescription { get; set; } +}