refactor(caddy): centralize reverse proxy editor dialogs
All checks were successful
Caddy Manager CI build / docker (push) Successful in 5m28s

This commit is contained in:
2026-07-26 13:29:46 +07:00
parent 6f6a37d5a6
commit b5cbad3664
6 changed files with 18 additions and 51 deletions

View File

@@ -35,8 +35,8 @@
@bind-SelectedValues="_selectedCaddyConfigurations"> @bind-SelectedValues="_selectedCaddyConfigurations">
@foreach (var (index, caddyConfig) in _availableCaddyConfigurations.Index()) @foreach (var (index, caddyConfig) in _availableCaddyConfigurations.Index())
{ {
<CaddyReverseProxyItem ConfigurationInfo="@caddyConfig" OnCaddyRestartRequired="@ReloadCaddy" <CaddyReverseProxyItem ConfigurationInfo="@caddyConfig"
OnCaddyfileDuplicateRequested="@HandleDuplicateRequest" /> OnEditRequested="@(fileName => ShowCaddyfileEditorDialog(fileName))" />
@if (index < _availableCaddyConfigurations.Count - 1) @if (index < _availableCaddyConfigurations.Count - 1)
{ {

View File

@@ -63,7 +63,8 @@ public partial class CaddyReverseProxiesPage : ComponentBase
/// <returns></returns> /// <returns></returns>
private async Task ShowCaddyfileEditorDialog(string fileName, string initialContent = "") private async Task ShowCaddyfileEditorDialog(string fileName, string initialContent = "")
{ {
var dialog = await DialogService.ShowAsync<CaddyfileEditorComponent>("New configuration", var dialog = await DialogService.ShowAsync<CaddyfileEditorComponent>(
string.IsNullOrWhiteSpace(fileName) ? "New configuration" : "Caddy file",
options: new DialogOptions options: new DialogOptions
{ {
FullWidth = true, FullWidth = true,
@@ -71,7 +72,8 @@ public partial class CaddyReverseProxiesPage : ComponentBase
}, parameters: new DialogParameters<CaddyfileEditorComponent> }, parameters: new DialogParameters<CaddyfileEditorComponent>
{ {
{ p => p.FileName, fileName }, { p => p.FileName, fileName },
{ p => p.InitialContent, initialContent } { p => p.InitialContent, initialContent },
{ p => p.OnDuplicate, EventCallback.Factory.Create<string>(this, HandleDuplicateRequest) }
}); });
var result = await dialog.Result; var result = await dialog.Result;
@@ -81,6 +83,7 @@ public partial class CaddyReverseProxiesPage : ComponentBase
await ReloadCaddy(); await ReloadCaddy();
} }
// Always rebuild the list, the configuration may have been renamed or had its hostnames and ports changed
Refresh(); Refresh();
} }

View File

@@ -1,7 +1,5 @@
using CaddyManager.Contracts.Caddy;
using CaddyManager.Contracts.Models.Caddy; using CaddyManager.Contracts.Models.Caddy;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using MudBlazor;
namespace CaddyManager.Components.Pages.Caddy.CaddyReverseProxies; namespace CaddyManager.Components.Pages.Caddy.CaddyReverseProxies;
@@ -12,52 +10,18 @@ namespace CaddyManager.Components.Pages.Caddy.CaddyReverseProxies;
public partial class CaddyReverseProxyItem : ComponentBase public partial class CaddyReverseProxyItem : ComponentBase
{ {
/// <summary> /// <summary>
/// Callback to refresh the Caddy reverse proxies on the main page /// Callback asking the main page to open the editor for this configuration. The page owns the dialog so that it
/// can refresh the whole list afterwards, which matters when the configuration is renamed or deleted.
/// </summary> /// </summary>
[Parameter] [Parameter]
public EventCallback OnCaddyRestartRequired { get; set; } public EventCallback<string> OnEditRequested { get; set; }
[Parameter]
public EventCallback<string> OnCaddyfileDuplicateRequested { get; set; }
[Parameter] [Parameter]
public CaddyConfigurationInfo ConfigurationInfo { get; set; } = null!; public CaddyConfigurationInfo ConfigurationInfo { get; set; } = null!;
/// <summary> /// <summary>
/// Dialog service for showing the Caddy file editor dialog /// Request the Caddy file editor dialog for this configuration
/// </summary>
[Inject]
private IDialogService DialogService { get; set; } = null!;
/// <summary>
/// Caddy service for ops on the Caddy configuration
/// </summary>
[Inject] private ICaddyService CaddyService { get; set; } = null!;
/// <summary>
/// Show the Caddy file editor dialog
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private async Task Edit() private Task Edit() => OnEditRequested.InvokeAsync(ConfigurationInfo.FileName);
{
var dialog = await DialogService.ShowAsync<CaddyfileEditor.CaddyfileEditor>("Caddy file", options: new DialogOptions
{
FullWidth = true,
MaxWidth = MaxWidth.Medium,
}, parameters: new DialogParameters<CaddyfileEditor.CaddyfileEditor>
{
{ p => p.FileName, ConfigurationInfo.FileName },
{ p => p.OnDuplicate, EventCallback.Factory.Create(this, OnCaddyfileDuplicateRequested) }
});
var result = await dialog.Result;
ConfigurationInfo = CaddyService.GetCaddyConfigurationInfo(ConfigurationInfo.FileName);
await InvokeAsync(StateHasChanged);
if (result is { Data: bool, Canceled: false } && (bool)result.Data)
{
await OnCaddyRestartRequired.InvokeAsync();
}
}
} }

View File

@@ -19,6 +19,6 @@
<MudButton OnClick="Duplicate">Duplicate</MudButton> <MudButton OnClick="Duplicate">Duplicate</MudButton>
} }
<MudButton Color="Color.Primary" OnClick="Submit">Save</MudButton> <MudButton Color="Color.Primary" OnClick="Submit">Save</MudButton>
<MudButton Color="Color.Secondary" OnClick="SaveAndRestart">Save & Restart</MudButton> <MudButton Color="Color.Secondary" OnClick="SaveAndReload">Save &amp; Reload</MudButton>
</DialogActions> </DialogActions>
</MudDialog> </MudDialog>

View File

@@ -144,13 +144,13 @@ public partial class CaddyfileEditor : ComponentBase
} }
/// <summary> /// <summary>
/// Saves the Caddy configuration file and restarts the Caddy container /// Saves the Caddy configuration file and reloads the Caddy configuration
/// </summary> /// </summary>
private async Task SaveAndRestart() private async Task SaveAndReload()
{ {
if (await Save()) if (await Save())
{ {
// Indicate successful save and that a restart is required by the calling component // Indicate successful save and that a reload is required by the calling component
MudDialog.Close(DialogResult.Ok(true)); MudDialog.Close(DialogResult.Ok(true));
} }
} }

View File

@@ -8,7 +8,7 @@ public class ApplicationInfo
/// <summary> /// <summary>
/// The version of the application, to be defined and tagged /// The version of the application, to be defined and tagged
/// </summary> /// </summary>
public static readonly string Version = "1.1.0"; public static readonly string Version = "1.1.1";
/// <summary> /// <summary>
/// The commit hash of the application /// The commit hash of the application