using AIStudio.Tools; using Microsoft.AspNetCore.Components; namespace AIStudio.Components.Blocks; /// /// Configuration component for any boolean option. /// public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceiver { /// /// Text to display when the option is true. /// [Parameter] public string LabelOn { get; set; } = string.Empty; /// /// Text to display when the option is false. /// [Parameter] public string LabelOff { get; set; } = string.Empty; /// /// The boolean state of the option. /// [Parameter] public Func State { get; set; } = () => false; /// /// An action which is called when the option is changed. /// [Parameter] public Action StateUpdate { get; set; } = _ => { }; /// /// Is the option disabled? /// [Parameter] public Func Disabled { get; set; } = () => false; #region Overrides of ComponentBase protected override async Task OnInitializedAsync() { // Register this component with the message bus: this.MessageBus.RegisterComponent(this); this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]); await base.OnInitializedAsync(); } #endregion private async Task OptionChanged(bool updatedState) { this.StateUpdate(updatedState); await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } #region Implementation of IMessageBusReceiver public Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data) { switch (triggeredEvent) { case Event.CONFIGURATION_CHANGED: this.StateHasChanged(); break; } return Task.CompletedTask; } public Task ProcessMessageWithResult(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) { return Task.FromResult(default); } #endregion }