2024-12-03 14:32:38 +00:00
|
|
|
using AIStudio.Settings;
|
2025-04-12 19:13:33 +00:00
|
|
|
using AIStudio.Tools.PluginSystem;
|
2024-12-03 14:32:38 +00:00
|
|
|
|
2024-06-30 13:26:28 +00:00
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
2025-04-14 17:29:56 +00:00
|
|
|
using SharedTools;
|
|
|
|
|
2024-07-14 19:46:17 +00:00
|
|
|
namespace AIStudio.Components;
|
2024-06-30 13:26:28 +00:00
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
public abstract class MSGComponentBase : ComponentBase, IDisposable, IMessageBusReceiver, ILang
|
2024-06-30 13:26:28 +00:00
|
|
|
{
|
2024-12-03 14:32:38 +00:00
|
|
|
[Inject]
|
|
|
|
protected SettingsManager SettingsManager { get; init; } = null!;
|
|
|
|
|
2024-06-30 13:26:28 +00:00
|
|
|
[Inject]
|
|
|
|
protected MessageBus MessageBus { get; init; } = null!;
|
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
[Inject]
|
|
|
|
private ILogger<PluginLanguage> Logger { get; init; } = null!;
|
|
|
|
|
|
|
|
private ILanguagePlugin Lang { get; set; } = PluginFactory.BaseLanguage;
|
|
|
|
|
2024-06-30 13:26:28 +00:00
|
|
|
#region Overrides of ComponentBase
|
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
protected override async Task OnInitializedAsync()
|
2024-06-30 13:26:28 +00:00
|
|
|
{
|
2025-04-12 19:13:33 +00:00
|
|
|
this.Lang = await this.SettingsManager.GetActiveLanguagePlugin();
|
|
|
|
|
2024-06-30 13:26:28 +00:00
|
|
|
this.MessageBus.RegisterComponent(this);
|
2025-04-12 19:13:33 +00:00
|
|
|
await base.OnInitializedAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Implementation of ILang
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string T(string fallbackEN)
|
|
|
|
{
|
|
|
|
var type = this.GetType();
|
|
|
|
var ns = $"{type.Namespace!}::{type.Name}".ToUpperInvariant().Replace(".", "::");
|
|
|
|
var key = $"root::{ns}::T{fallbackEN.ToFNV32()}";
|
|
|
|
|
|
|
|
if(this.Lang.TryGetText(key, out var text, logWarning: false))
|
|
|
|
return text;
|
|
|
|
|
|
|
|
this.Logger.LogWarning($"Missing translation key '{key}' for content '{fallbackEN}'.");
|
|
|
|
return fallbackEN;
|
2024-06-30 13:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Implementation of IMessageBusReceiver
|
|
|
|
|
2025-01-21 14:36:22 +00:00
|
|
|
public abstract string ComponentName { get; }
|
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
public async Task ProcessMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data)
|
2024-09-15 10:30:07 +00:00
|
|
|
{
|
|
|
|
switch (triggeredEvent)
|
|
|
|
{
|
|
|
|
case Event.COLOR_THEME_CHANGED:
|
|
|
|
this.StateHasChanged();
|
|
|
|
break;
|
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
case Event.PLUGINS_RELOADED:
|
|
|
|
this.Lang = await this.SettingsManager.GetActiveLanguagePlugin();
|
|
|
|
await this.InvokeAsync(this.StateHasChanged);
|
|
|
|
break;
|
|
|
|
|
2024-09-15 10:30:07 +00:00
|
|
|
default:
|
2025-04-12 19:13:33 +00:00
|
|
|
await this.ProcessIncomingMessage(sendingComponent, triggeredEvent, data);
|
|
|
|
break;
|
2024-09-15 10:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
2025-04-12 19:13:33 +00:00
|
|
|
|
|
|
|
public async Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
|
|
|
|
{
|
|
|
|
return await this.ProcessIncomingMessageWithResult<TPayload, TResult>(sendingComponent, triggeredEvent, data);
|
|
|
|
}
|
2024-06-30 13:26:28 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
protected virtual Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data)
|
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual Task<TResult?> ProcessIncomingMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
|
|
|
|
{
|
|
|
|
return Task.FromResult<TResult?>(default);
|
|
|
|
}
|
|
|
|
|
2024-06-30 13:26:28 +00:00
|
|
|
#region Implementation of IDisposable
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
this.MessageBus.Unregister(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
protected async Task SendMessage<T>(Event triggeredEvent, T? data = default)
|
|
|
|
{
|
|
|
|
await this.MessageBus.SendMessage(this, triggeredEvent, data);
|
|
|
|
}
|
|
|
|
|
2024-07-13 08:37:57 +00:00
|
|
|
protected async Task<TResult?> SendMessageWithResult<TPayload, TResult>(Event triggeredEvent, TPayload? data)
|
|
|
|
{
|
|
|
|
return await this.MessageBus.SendMessageUseFirstResult<TPayload, TResult>(this, triggeredEvent, data);
|
|
|
|
}
|
|
|
|
|
2024-06-30 13:26:28 +00:00
|
|
|
protected void ApplyFilters(ComponentBase[] components, Event[] events)
|
|
|
|
{
|
2024-09-15 10:30:07 +00:00
|
|
|
// Append the color theme changed event to the list of events:
|
|
|
|
var eventsList = new List<Event>(events)
|
|
|
|
{
|
2025-04-12 19:13:33 +00:00
|
|
|
Event.COLOR_THEME_CHANGED,
|
|
|
|
Event.PLUGINS_RELOADED,
|
2024-09-15 10:30:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.MessageBus.ApplyFilters(this, components, eventsList.ToArray());
|
2024-06-30 13:26:28 +00:00
|
|
|
}
|
|
|
|
}
|