Improve admin field names and generation-safe dialog lifecycle

This commit is contained in:
Joseph Doherty
2026-11-13 21:15:58 -05:00
parent 06b1743051
commit 169149f1eb
82 changed files with 15839 additions and 73 deletions
@@ -0,0 +1,151 @@
@inject IJSRuntime JS
@implements IAsyncDisposable
@if (Visible)
{
var closeGeneration = _generation;
<div class="modal-backdrop fade show" style="display:block"></div>
<div id="@_id" class="modal fade show" tabindex="-1" role="dialog" aria-modal="true"
aria-labelledby="@(_id + "-title")" aria-describedby="@(string.IsNullOrWhiteSpace(Description) ? null : _id + "-description")"
aria-busy="@(Busy ? "true" : "false")" style="display:block">
<div class="modal-dialog @DialogClass" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 id="@(_id + "-title")" class="modal-title h5">@if (TitleContent is not null) { @TitleContent } else { @Title }</h2>
<button type="button" class="btn-close" aria-label="Close @Title" disabled="@Busy" @onclick="() => RequestCloseFromDom(closeGeneration)"></button>
</div>
<div class="modal-body">
@if (!string.IsNullOrWhiteSpace(Description))
{
<p id="@(_id + "-description")" class="mb-0">@Description</p>
}
@ChildContent
</div>
@if (Footer is not null)
{
<div class="modal-footer">@Footer</div>
}
</div>
</div>
</div>
}
@code {
/// <summary>Whether this overlay is presented.</summary>
[Parameter] public bool Visible { get; set; }
/// <summary>The accessible dialog title.</summary>
[Parameter] public string Title { get; set; } = "Dialog";
/// <summary>Optional title markup; its text must retain the dialog's domain name.</summary>
[Parameter] public RenderFragment? TitleContent { get; set; }
/// <summary>Optional plain description linked to the modal container.</summary>
[Parameter] public string? Description { get; set; }
/// <summary>Optional existing Bootstrap dialog size/scroll modifiers.</summary>
[Parameter] public string? DialogClass { get; set; }
/// <summary>The body content.</summary>
[Parameter] public RenderFragment? ChildContent { get; set; }
/// <summary>The caller's action controls.</summary>
[Parameter] public RenderFragment? Footer { get; set; }
/// <summary>Prevents close while an operation is still in flight.</summary>
[Parameter] public bool Busy { get; set; }
/// <summary>The caller's existing close/cancel path; also rejects a second simultaneous overlay.</summary>
[Parameter] public EventCallback OnClose { get; set; }
/// <summary>Optional nonconfidential editor/confirmation identity; changing it starts a fresh presentation.</summary>
[Parameter] public object? Target { get; set; }
private readonly SemaphoreSlim _lifecycle = new(1, 1);
private readonly string _id = "ot-modal-" + Guid.NewGuid().ToString("N");
private DotNetObjectReference<ModalFrame>? _reference;
private bool _active;
private long _activeGeneration;
private bool _disposed;
private bool _lastVisible;
private object? _lastTarget;
private bool _needsOpen;
private bool _closePending;
private long _generation;
private Task? _disposeTask;
protected override void OnParametersSet()
{
if (_lastVisible == Visible && Equals(_lastTarget, Target)) return;
_lastVisible = Visible;
_lastTarget = Target;
++_generation;
_needsOpen = Visible;
_closePending = false;
}
/// <summary>Closes only the current idle presentation, never a reopened target via a stale native event.</summary>
[JSInvokable]
public Task RequestCloseFromDom(long generation) => InvokeAsync(async () =>
{
if (_disposed || !Visible || Busy || _closePending || generation != _generation) return;
_closePending = true;
try { await OnClose.InvokeAsync(); }
finally { if (generation == _generation) _closePending = false; }
});
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await _lifecycle.WaitAsync();
long rejectedGeneration = -1;
try
{
if (_disposed) return;
var generation = _generation;
if (!Visible)
{
if (_reference is not null) await JS.InvokeVoidAsync("otDialog.deactivate", _id);
_active = false;
return;
}
if (_active && _activeGeneration != generation)
{
await JS.InvokeVoidAsync("otDialog.deactivate", _id, _activeGeneration);
_active = false;
}
if (_disposed || !Visible || generation != _generation) return;
if (!_active || _needsOpen)
{
_needsOpen = false;
_reference ??= DotNetObjectReference.Create(this);
var accepted = await JS.InvokeAsync<bool>("otDialog.activate", _id, _reference, generation, Busy);
// A hide/target switch/dispose can occur while the browser reply is pending.
// Clean only that old native generation before a queued render opens the next.
if (_disposed || !Visible || generation != _generation)
{
if (accepted) await JS.InvokeVoidAsync("otDialog.deactivate", _id, generation);
return;
}
_active = accepted;
_activeGeneration = generation;
if (!accepted) rejectedGeneration = generation;
}
else await JS.InvokeVoidAsync("otDialog.update", _id, generation, Busy);
}
catch (JSDisconnectedException) { }
catch (JSException) { }
catch (InvalidOperationException) { } // Static SSR cannot invoke JavaScript.
finally { _lifecycle.Release(); }
// Never hold the lifecycle gate while invoking a caller that can render/dispose us.
if (rejectedGeneration == _generation) await RequestCloseFromDom(rejectedGeneration);
}
public ValueTask DisposeAsync() => new(_disposeTask ??= DisposeCoreAsync());
private async Task DisposeCoreAsync()
{
_disposed = true;
++_generation;
await _lifecycle.WaitAsync();
try
{
if (_reference is not null) await JS.InvokeVoidAsync("otDialog.deactivate", _id);
}
catch (JSDisconnectedException) { }
catch (JSException) { }
catch (InvalidOperationException) { }
finally { _active = false; _reference?.Dispose(); _lifecycle.Release(); }
}
}