Files
Heimdall/src/Heimdall.Web/Components/Settings/NotificationSettingsSection.razor
T
Arnaud Dartois 23571da9c3
Heimdall Gitea / Build and tests (pull_request) Failing after 2m10s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
feat(i18n): localize remaining UI and server messages
2026-10-25 05:59:18 +01:00

207 lines
9.3 KiB
Plaintext

@namespace Heimdall.Web.Components.Settings
@inject HeimdallApiClient Api
@inject ISnackbar Snackbar
@inject Heimdall.Web.Services.UiText Text
<MudPaper Elevation="1" Class="pa-4 mb-4" Style="border-radius: var(--hmd-radius-lg);">
<div class="d-flex align-center mb-3">
<MudText Typo="Typo.h6" Class="flex-grow-1">
<i class="fas fa-bell mr-2"></i> @Text["NotificationSettingsSection_Title"]
</MudText>
<MudChip T="string" Size="Size.Small" Variant="Variant.Outlined" Color="Color.Primary">@Text["NotificationSettingsSection_Channels"]</MudChip>
</div>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-3">
@Text["NotificationSettingsSection_Description"]
</MudText>
<MudText Typo="Typo.subtitle2" Class="mb-2">
<i class="fas fa-paper-plane mr-1" style="color: var(--mud-palette-primary);"></i> ntfy
</MudText>
<MudGrid>
<MudItem xs="12" md="6">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_NtfyServer"]" Variant="Variant.Outlined"
Value="@GetValue("ntfy.baseurl")"
ValueChanged="OnBaseUrlChanged"
HelperText="@Text["NotificationSettingsSection_NtfyServerHelp"]" />
</MudItem>
<MudItem xs="12" md="6">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_AccessToken"]" Variant="Variant.Outlined"
Type="InputType.Password"
Value="@SecretValue("ntfy.token")"
ValueChanged="OnTokenChanged"
Placeholder="@Text["NotificationSettingsSection_SecretConfigured"]"
HelperText="@Text["NotificationSettingsSection_AccessTokenHelp"]" />
</MudItem>
</MudGrid>
<MudText Typo="Typo.subtitle2" Class="mb-2 mt-3">
<i class="fas fa-envelope mr-1" style="color: var(--mud-palette-primary);"></i> @Text["NotificationSettingsSection_SmtpEmail"]
</MudText>
<MudGrid>
<MudItem xs="12" md="6">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_FromAddress"]" Variant="Variant.Outlined"
Value="@GetValue("email.fromaddress")"
ValueChanged="OnFromAddressChanged"
HelperText="@Text["NotificationSettingsSection_FromAddressHelp"]" />
</MudItem>
<MudItem xs="12" md="6">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_FromName"]" Variant="Variant.Outlined"
Value="@GetValue("email.fromname")"
ValueChanged="OnFromNameChanged" />
</MudItem>
<MudItem xs="12" md="4">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_SmtpHost"]" Variant="Variant.Outlined"
Value="@GetValue("email.smtphost")"
ValueChanged="OnSmtpHostChanged"
HelperText="@Text["NotificationSettingsSection_SmtpHostHelp"]" />
</MudItem>
<MudItem xs="6" md="2">
<MudNumericField T="int?" Label="@Text["NotificationSettingsSection_SmtpPort"]" Variant="Variant.Outlined"
Value="@(int.TryParse(GetValue("email.smtpport"), out var p) ? p : null)"
ValueChanged="OnSmtpPortChanged" Min="1" Max="65535" />
</MudItem>
<MudItem xs="12" md="3">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_SmtpUsername"]" Variant="Variant.Outlined"
Value="@GetValue("email.username")"
ValueChanged="OnUsernameChanged" />
</MudItem>
<MudItem xs="12" md="3">
<MudTextField T="string" Label="@Text["NotificationSettingsSection_SmtpPassword"]" Variant="Variant.Outlined"
Type="InputType.Password"
Value="@SecretValue("email.password")"
ValueChanged="OnPasswordChanged"
Placeholder="@Text["NotificationSettingsSection_SecretConfigured"]" />
</MudItem>
<MudItem xs="12">
<MudSwitch T="bool"
Value="@(GetValue("email.usessl")?.Equals("true", StringComparison.OrdinalIgnoreCase) == true)"
ValueChanged="OnUseSslChanged"
Color="Color.Primary"
Label="@Text["NotificationSettingsSection_UseTls"]"
HelperText="@Text["NotificationSettingsSection_UseTlsHelp"]" />
</MudItem>
</MudGrid>
<div class="d-flex align-center mt-3">
<MudButton Variant="Variant.Outlined" Color="Color.Info" StartIcon="@Icons.Material.Filled.Send"
OnClick="TestNtfyAsync" Disabled="@_testing">
@(_testing ? Text["NotificationSettingsSection_Testing"] : Text["NotificationSettingsSection_TestNtfy"])
</MudButton>
<MudButton Variant="Variant.Outlined" Color="Color.Info" Class="ml-2" StartIcon="@Icons.Material.Filled.Email"
OnClick="TestEmailAsync" Disabled="@_testing">
@(_testing ? Text["NotificationSettingsSection_Testing"] : Text["NotificationSettingsSection_TestEmail"])
</MudButton>
</div>
@if (_testResult is not null)
{
<MudAlert Severity="@(_testResult.Success ? Severity.Success : Severity.Error)" Class="mt-3" Variant="Variant.Outlined">
@_testResult.Message
</MudAlert>
}
</MudPaper>
@code {
private List<AppSettingItem> _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);
}
}
}