fix(admin-ui): scope UNS modal lifecycles and script identity

This commit is contained in:
Joseph Doherty
2026-11-14 00:14:30 -05:00
parent 071148e9b8
commit a0aa4f023c
69 changed files with 8332 additions and 437 deletions
@@ -7,17 +7,15 @@
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw
@inject IUnsTreeService Svc
@if (Visible)
{
<div class="modal-backdrop fade show" style="display:block"></div>
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add tag references</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="CancelAsync"></button>
</div>
<div class="modal-body">
@implements IDisposable
@{
var presentation = _generation;
var completedAction = OnCommitted;
var cancelAction = OnCancel;
}
<ModalFrame DialogClass="modal-lg modal-dialog-scrollable" Visible="@Visible" Title='Add tag references' Target="@presentation" Busy="@Pending" OnClose="() => CancelAsync(presentation, cancelAction)">
<ChildContent>
<fieldset disabled="@Pending">
<p class="text-muted small mb-2">
Pick raw tags to reference under this equipment. The tree is scoped to the
equipment's cluster. Tick individual tags, or use a device / tag-group's
@@ -26,7 +24,7 @@
@if (_loading)
{
<p class="text-muted"><span class="spinner-border spinner-border-sm me-1"></span>Loading…</p>
<p class="text-muted"><span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span>Loading…</p>
}
else if (_roots.Count == 0)
{
@@ -36,31 +34,41 @@
{
<div class="border rounded p-2" style="max-height:50vh; overflow:auto">
<RawTree Roots="_roots" PickerMode="true" SelectedTagIds="_selected"
OnSelectionChanged="OnSelectionChanged"
ResolveDescendantTagIds="ResolveDescendantsAsync" />
@key="presentation" OnSelectionChanged="() => OnSelectionChanged(presentation)"
ResolveDescendantTagIds="node => ResolveDescendantsAsync(presentation, node)" />
</div>
}
@if (!string.IsNullOrWhiteSpace(_error))
{
<div class="text-danger small mt-2">@_error</div>
}
</div>
<div class="modal-footer">
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" @onclick="CancelAsync" disabled="@_busy">Cancel</button>
<button type="button" class="btn btn-primary" @onclick="CommitAsync" disabled="@(_busy || _selected.Count == 0)">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
</fieldset>
</ChildContent>
<Footer>
<div id='@FieldId("service-error")' role="alert" hidden="@string.IsNullOrWhiteSpace(_error)" class="text-danger small w-100">@_error</div>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => CancelAsync(presentation, cancelAction)" disabled="@Pending">Cancel</button>
<button type="button" class="btn btn-primary" @onclick="() => CommitAsync(presentation, completedAction)" disabled="@(Pending || _loading || _selected.Count == 0)">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
Add @_selected.Count reference@(_selected.Count == 1 ? "" : "s")
</button>
</div>
</div>
</div>
</div>
</div>
}
</Footer>
</ModalFrame>
@code {
/// <summary>Optional nonconfidential identity for successive presentations.</summary>
[Parameter] public object? Target { get; set; }
private readonly string _fieldScope = "ot-uns-" + Guid.NewGuid().ToString("N");
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
private bool _disposed, _closing;
private long _generation;
private object? _lastTarget;
private bool _lastVisible;
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
private bool Pending => _busy || _closing;
/// <summary>Retires UI continuations without cancelling a dispatched operation.</summary>
public void Dispose() { _disposed = true; ++_generation; }
/// <summary>Whether the modal is shown. The host owns this flag.</summary>
[Parameter] public bool Visible { get; set; }
@@ -74,64 +82,66 @@
[Parameter] public EventCallback OnCancel { get; set; }
private IReadOnlyList<RawNode> _roots = Array.Empty<RawNode>();
private readonly HashSet<string> _selected = new(StringComparer.Ordinal);
private HashSet<string> _selected = new(StringComparer.Ordinal);
private bool _busy;
private bool _loading;
private string? _error;
private bool _wasVisible;
// Bumped on every open and close so a superseded picker-root load can't install its tree under a
// different equipment's (or a re-opened) modal (RawBrowseModal idiom).
private int _openGeneration;
private bool IsStale(int generation) => generation != _openGeneration || !Visible;
protected override async Task OnParametersSetAsync()
{
// Reset + reload only on the false→true transition so re-renders while open don't wipe state.
if (!Visible && _wasVisible) { _openGeneration++; }
if (Visible && !_wasVisible)
object identity = (Target, EquipmentId);
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
_lastVisible = Visible; _lastTarget = identity;
var generation = ++_generation;
if (!Visible) return;
_selected = new(StringComparer.Ordinal);
_error = null; _roots = Array.Empty<RawNode>(); _loading = true;
var equipmentId = EquipmentId;
try
{
_wasVisible = true;
var generation = ++_openGeneration;
_selected.Clear();
_error = null;
_roots = Array.Empty<RawNode>();
_loading = true;
StateHasChanged();
var root = string.IsNullOrEmpty(EquipmentId)
? null
: await Svc.LoadReferencePickerRootAsync(EquipmentId);
if (IsStale(generation)) { return; }
var root = string.IsNullOrEmpty(equipmentId) ? null : await Svc.LoadReferencePickerRootAsync(equipmentId);
if (!Current(generation)) return;
_roots = root is null ? Array.Empty<RawNode>() : new[] { root };
_loading = false;
}
_wasVisible = Visible;
catch when (!Current(generation)) { /* A superseded root load cannot fault the replacement. */ }
}
private Task<IReadOnlyList<string>> ResolveDescendantsAsync(RawNode node) =>
Svc.LoadDescendantTagIdsAsync(node);
// RawTree mutates _selected in place; this just re-renders the footer count.
private void OnSelectionChanged() => StateHasChanged();
private async Task CommitAsync()
private async Task<IReadOnlyList<string>> ResolveDescendantsAsync(long generation, RawNode node)
{
if (_selected.Count == 0) { return; }
_busy = true;
_error = null;
if (!Current(generation) || Pending) return Array.Empty<string>();
try
{
var res = await Svc.AddReferencesAsync(EquipmentId!, _selected.ToList());
if (res.Ok) { await OnCommitted.InvokeAsync(); }
else { _error = res.Error; }
}
finally
{
_busy = false;
var ids = await Svc.LoadDescendantTagIdsAsync(node);
return Current(generation) && !Pending ? ids : Array.Empty<string>();
}
catch when (!Current(generation)) { return Array.Empty<string>(); }
}
private Task CancelAsync() => OnCancel.InvokeAsync();
// Each presentation owns its set: a retired child can never mutate its replacement's selection.
private void OnSelectionChanged(long generation) { if (Current(generation)) StateHasChanged(); }
private async Task CommitAsync(long generation, EventCallback completedAction)
{
if (!Current(generation) || Pending || _loading || _selected.Count == 0) return;
var equipmentId = EquipmentId!;
var selected = _selected.ToList();
_busy = true; _error = null;
try
{
var result = await Svc.AddReferencesAsync(equipmentId, selected);
if (!Current(generation)) return;
if (result.Ok) await completedAction.InvokeAsync();
else _error = result.Error;
}
catch when (!Current(generation)) { /* A retired operation cannot surface an error in its replacement. */ }
finally { _busy = false; }
}
private async Task CancelAsync(long generation, EventCallback cancelAction)
{
if (!Current(generation) || Pending) return;
_closing = true;
try { await cancelAction.InvokeAsync(); }
finally { _closing = false; }
}
}