@namespace Heimdall.Web.Components.Settings @inject HeimdallApiClient Api @inject ISnackbar Snackbar @inject Heimdall.Web.Services.UiText Text
@Text["NotificationSettingsSection_Title"] @Text["NotificationSettingsSection_Channels"]
@Text["NotificationSettingsSection_Description"] ntfy @Text["NotificationSettingsSection_SmtpEmail"]
@(_testing ? Text["NotificationSettingsSection_Testing"] : Text["NotificationSettingsSection_TestNtfy"]) @(_testing ? Text["NotificationSettingsSection_Testing"] : Text["NotificationSettingsSection_TestEmail"])
@if (_testResult is not null) { @_testResult.Message }
@code { private List _settings = []; private bool _testing; private TestActionResult? _testResult; protected override async Task OnInitializedAsync() { try { _settings = await Api.GetSettingsAsync(); } catch { // API indisponible (ex : test) — on reste sur les défauts. _settings = []; } } private string? GetValue(string key) => _settings.FirstOrDefault(s => s.Key == key)?.Value; // Les secrets sont redactés par l'API (***REDACTED***) : afficher vide avec // un placeholder pour ne pas laisser croire que la valeur est vide. private string SecretValue(string key) { var v = GetValue(key); return v is null or "" or "***REDACTED***" ? string.Empty : v; } private Task OnBaseUrlChanged(string v) => SaveValueAsync("ntfy.baseurl", v); private Task OnTokenChanged(string v) => SaveValueAsync("ntfy.token", v); private Task OnFromAddressChanged(string v) => SaveValueAsync("email.fromaddress", v); private Task OnFromNameChanged(string v) => SaveValueAsync("email.fromname", v); private Task OnSmtpHostChanged(string v) => SaveValueAsync("email.smtphost", v); private Task OnSmtpPortChanged(int? v) => SaveValueAsync("email.smtpport", v?.ToString() ?? ""); private Task OnUsernameChanged(string v) => SaveValueAsync("email.username", v); private Task OnPasswordChanged(string v) => SaveValueAsync("email.password", v); private Task OnUseSslChanged(bool v) => SaveValueAsync("email.usessl", v ? "true" : "false"); private async Task TestNtfyAsync() { _testing = true; _testResult = null; try { _testResult = await Api.TestNtfyAsync() ?? new TestActionResult(false, Text["NotificationSettingsSection_TestUnavailable"]); } catch (Exception ex) { _testResult = new TestActionResult(false, string.Format(Text["NotificationSettingsSection_TestFailed"], ex.Message)); } finally { _testing = false; } } private async Task TestEmailAsync() { _testing = true; _testResult = null; try { _testResult = await Api.TestEmailAsync() ?? new TestActionResult(false, Text["NotificationSettingsSection_TestUnavailable"]); } catch (Exception ex) { _testResult = new TestActionResult(false, string.Format(Text["NotificationSettingsSection_TestFailed"], ex.Message)); } finally { _testing = false; } } private async Task SaveValueAsync(string key, string? value) { value ??= string.Empty; var existing = _settings.FirstOrDefault(s => s.Key == key); try { if (existing is null) { await Api.CreateSettingAsync(new CreateAppSettingRequest( Key: key, Value: value, Category: "integrations", Description: key, ValueType: "string", AllowedValues: null, AgentScoped: false)); } else { await Api.UpdateSettingAsync(key, new UpdateAppSettingRequest( Value: value, Description: existing.Description ?? key, Category: "integrations", ValueType: "string", AllowedValues: null, AgentScoped: null)); } _settings = await Api.GetSettingsAsync(); Snackbar.Add(string.Format(Text["NotificationSettingsSection_SettingUpdated"], key), Severity.Success); } catch (Exception ex) { Snackbar.Add(string.Format(Text["NotificationSettingsSection_SettingUpdateFailed"], ex.Message), Severity.Error); } } }