Files
SethDiscordBot/WebUI/Components/Pages/Home.razor
2025-05-06 16:46:51 +03:00

118 lines
3.4 KiB
Plaintext

@page "/"
@using DiscordBotCore.Bot
@using DiscordBotCore.Logging
@using DiscordBotCore.PluginManagement.Loading
@using WebUI.Models
@using WebUI.Services
@inject IDiscordBotApplication DiscordBotApplication
@inject IPluginLoader PluginLoader
@inject ILogger Logger
@inject IJSRuntime JS
@inject NotificationService NotificationService
@rendermode InteractiveServer
<h3>Console Log Viewer</h3>
<div class="container mt-5">
<div class="row">
<div class="col-md-8">
<h4>Console Log</h4>
<div id="consoleLog" class="border p-3 bg-dark text-white" style="height: 400px; overflow-y: auto; font-family: monospace;">
@foreach (var line in Logger.LogMessages)
{
<div style="@GetLogStyle(line)">@line.Message</div>
}
</div>
</div>
<div class="col-md-4">
<h4>Controls</h4>
<button class="btn btn-success mb-2" @onclick="StartApplication" disabled="@IsRunning">Start Application</button>
<button class="btn btn-danger mb-2" @onclick="StopApplication" disabled="@(!IsRunning)">Stop Application</button>
<button class="btn btn-info mb-2" @onclick="LoadPlugins" disabled="@(!IsRunning)">Load Plugins</button>
<button class="btn btn-warning mb-2" @onclick="ClearLogs">Clear Logs</button>
</div>
</div>
</div>
<script>
window.scrollToBottom = function (elementId) {
var el = document.getElementById(elementId);
if (el) {
el.scrollTop = el.scrollHeight;
}
}
</script>
@code {
private bool IsRunning { get; set; }
protected override void OnInitialized()
{
IsRunning = DiscordBotApplication.IsReady;
Logger.OnLogReceived += LoggerOnLogReceived;
}
private void LoggerOnLogReceived(ILogMessage obj)
{
InvokeAsync(async () =>
{
await JS.InvokeVoidAsync("scrollToBottom", "consoleLog");
StateHasChanged();
});
}
private async Task StartApplication()
{
if (!DiscordBotApplication.IsReady)
{
await DiscordBotApplication.StartAsync();
Logger.Log("Application started", this);
NotificationService.Notify("Bot Started !", NotificationType.Success);
}
IsRunning = DiscordBotApplication.IsReady;
}
private async Task StopApplication()
{
if (DiscordBotApplication.IsReady)
{
await DiscordBotApplication.StopAsync();
Logger.Log("Application stopped", this);
NotificationService.Notify("Bot Stopped !", NotificationType.Success);
}
IsRunning = DiscordBotApplication.IsReady;
}
private async Task LoadPlugins()
{
Logger.Log("Loading plugins", this);
await PluginLoader.LoadPlugins();
Logger.Log("Plugins loaded", this);
NotificationService.Notify("Plugins Loaded !", NotificationType.Success);
}
private string GetLogStyle(ILogMessage logMessage)
{
return logMessage.LogMessageType switch
{
LogType.Info => "color: white;",
LogType.Warning => "color: yellow;",
LogType.Error => "color: red;",
LogType.Critical => "color: purple;",
_ => ""
};
}
private void ClearLogs()
{
Logger.LogMessages.Clear();
}
}