fix(admin-ui): isolate UNS callers and recover confirmed-write refreshes
This commit is contained in:
@@ -118,6 +118,7 @@
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_readLifetime.Cancel();
|
||||
_readLifetime.Dispose();
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
<div><label class="form-label small mb-1" for="@_filterId">Filter loaded UNS names</label>
|
||||
<input @ref="_filterControl" aria-describedby="@(_filterId + "-hint")" id="@_filterId" type="text" class="form-control form-control-sm" style="max-width:240px"
|
||||
placeholder="Filter by name (substring)…"
|
||||
@bind="_filter" @bind:event="oninput" disabled="@(_loading || _loadError is not null)" />
|
||||
@bind="_filter" @bind:event="oninput" disabled="@(!_structureLoaded)" />
|
||||
@if (!string.IsNullOrWhiteSpace(_filter))
|
||||
{
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm mt-1" @onclick="ClearFilterAsync">Clear filter</button>
|
||||
}</div>
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm" @onclick="ExpandAll" disabled="@(_loading || _loadError is not null)">Expand all</button>
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm" @onclick="CollapseAll" disabled="@(_loading || _loadError is not null)">Collapse all</button>
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm" @onclick="ExpandAll" disabled="@(!_structureLoaded)">Expand all</button>
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm" @onclick="CollapseAll" disabled="@(!_structureLoaded)">Collapse all</button>
|
||||
</div>
|
||||
<button type="button" class="btn btn-outline-primary btn-sm" @onclick="OpenImportModal">Import equipment CSV</button>
|
||||
</div>
|
||||
@@ -38,13 +38,32 @@
|
||||
|
||||
<p id="@(_filterId + "-hint")" class="small text-muted px-3 pt-2 mb-0">Filters loaded names only. Unloaded tag contents are not searched.</p>
|
||||
|
||||
@if (_loading)
|
||||
@if (_lastLoadedAtUtc is { } loadedAt)
|
||||
{
|
||||
<p class="small text-muted px-3 pt-2 mb-0">Last loaded: <time class="mono" data-testid="uns-last-loaded" datetime="@(loadedAt.ToString("O"))">@loadedAt.ToString("yyyy-MM-dd HH:mm:ss 'UTC'")</time></p>
|
||||
}
|
||||
@if (_refreshOutcome is not null)
|
||||
{
|
||||
<div class="px-3 pt-2">
|
||||
<p role="status" aria-live="polite" data-testid="uns-refresh-outcome" class="@(_refreshFailed ? "text-warning mb-2" : "small text-muted mb-2")">@_refreshOutcome</p>
|
||||
@if (_refreshFailed && _structureLoaded)
|
||||
{
|
||||
@if (_loadError is not null) { <DiagnosticDetails Summary="Structure refresh details" Text="@_loadError" /> }
|
||||
<button type="button" class="btn btn-outline-secondary mt-2" data-testid="tree-retry" @ref="_retryControl" @onclick="RetryAsync" disabled="@_loading" aria-busy="@(_loading ? "true" : null)">@(_loading ? "Refreshing…" : "Retry structure refresh")</button>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
@if (_loading && _structureLoaded)
|
||||
{
|
||||
<p role="status" class="small text-muted p-3 mb-0">Refreshing the loaded UNS structure…</p>
|
||||
}
|
||||
@if (_loading && !_structureLoaded)
|
||||
{
|
||||
<div style="padding:1rem" class="text-muted" role="status">
|
||||
<span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span>Loading UNS tree…
|
||||
</div>
|
||||
}
|
||||
else if (_loadError is not null)
|
||||
else if (_loadError is not null && !_structureLoaded)
|
||||
{
|
||||
<div class="p-3">
|
||||
<p role="alert">The UNS tree could not be loaded. Retry to read the current structure.</p>
|
||||
@@ -58,7 +77,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
<div style="padding:.5rem 1rem;max-width:100%;overflow:auto" tabindex="0" role="region" aria-label="Loaded UNS tree">
|
||||
<div style="padding:.5rem 1rem;max-width:100%;overflow:auto" tabindex="0" role="region" aria-label="Loaded UNS tree" aria-busy="@(_loading ? "true" : null)">
|
||||
<UnsTree Roots="_roots" Filter="@_filter"
|
||||
OnToggleExpand="ToggleAsync"
|
||||
OnAddChild="HandleAddChild"
|
||||
@@ -68,59 +87,49 @@
|
||||
}
|
||||
</section>
|
||||
|
||||
<AreaModal Visible="_areaModalVisible"
|
||||
@{ var modalPresentation = _modalGeneration; var deleteNode = _confirmNode; }
|
||||
<AreaModal Visible="_areaModalVisible" Target="@modalPresentation"
|
||||
IsNew="_areaModalIsNew"
|
||||
ClusterId="@_areaModalClusterId"
|
||||
Existing="_areaModalExisting"
|
||||
Clusters="ClusterOptions"
|
||||
OnSaved="OnModalSavedAsync"
|
||||
OnCancel="CloseModals" />
|
||||
OnSaved="() => OnModalSavedAsync(modalPresentation)"
|
||||
OnCancel="() => ClosePresentation(modalPresentation)" />
|
||||
|
||||
<LineModal Visible="_lineModalVisible"
|
||||
<LineModal Visible="_lineModalVisible" Target="@modalPresentation"
|
||||
IsNew="_lineModalIsNew"
|
||||
UnsAreaId="@_lineModalAreaId"
|
||||
Existing="_lineModalExisting"
|
||||
Areas="_lineModalAreaOptions"
|
||||
OnSaved="OnModalSavedAsync"
|
||||
OnCancel="CloseModals" />
|
||||
OnSaved="() => OnModalSavedAsync(modalPresentation)"
|
||||
OnCancel="() => ClosePresentation(modalPresentation)" />
|
||||
|
||||
<ImportEquipmentModal Visible="_importModalVisible"
|
||||
OnImported="OnImportedAsync"
|
||||
OnCancel="CloseModals" />
|
||||
<ImportEquipmentModal Visible="_importModalVisible" Target="@modalPresentation"
|
||||
OnImported="() => OnImportedAsync(modalPresentation)"
|
||||
OnCancel="() => ClosePresentation(modalPresentation)" />
|
||||
|
||||
@if (_confirmNode is not null)
|
||||
@if (deleteNode is not null)
|
||||
{
|
||||
<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" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Delete @_confirmNode.Kind</h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="CloseModals"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Delete <span class="mono">@_confirmNode.DisplayName</span>? This cannot be undone.</p>
|
||||
@if (_confirmNode.Kind == UnsNodeKind.Enterprise)
|
||||
{
|
||||
<p class="text-danger">This will delete every cluster under this enterprise.</p>
|
||||
}
|
||||
@if (!string.IsNullOrWhiteSpace(_confirmError))
|
||||
{
|
||||
<div class="text-danger small mt-2">@_confirmError</div>
|
||||
}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="CloseModals" disabled="@_confirmBusy">Cancel</button>
|
||||
<button type="button" class="btn btn-danger" @onclick="ConfirmDeleteAsync" disabled="@_confirmBusy">
|
||||
@if (_confirmBusy) { <span class="spinner-border spinner-border-sm me-1"></span> }
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<ModalFrame Visible="true" Title="@($"Delete {deleteNode.Kind}")" Description="@($"Delete {deleteNode.DisplayName}? This cannot be undone.")"
|
||||
Target="@modalPresentation" Busy="@_confirmBusy" OnClose="() => CancelConfirm(modalPresentation)">
|
||||
<ChildContent>
|
||||
@if (deleteNode.Kind == UnsNodeKind.Enterprise)
|
||||
{
|
||||
<p class="text-danger">This will delete every cluster under this enterprise.</p>
|
||||
}
|
||||
@if (!string.IsNullOrWhiteSpace(_confirmError))
|
||||
{
|
||||
<p class="text-danger mb-0" role="alert">@_confirmError</p>
|
||||
}
|
||||
</ChildContent>
|
||||
<Footer>
|
||||
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" disabled="@_confirmBusy" @onclick="() => CancelConfirm(modalPresentation)">Cancel</button>
|
||||
<button type="button" class="btn btn-danger" disabled="@_confirmBusy" @onclick="() => ConfirmDeleteAsync(modalPresentation, deleteNode)">
|
||||
@if (_confirmBusy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
|
||||
Delete
|
||||
</button>
|
||||
</Footer>
|
||||
</ModalFrame>
|
||||
}
|
||||
</PageErrorBoundary>
|
||||
|
||||
@@ -167,10 +176,25 @@
|
||||
private string? _loadError;
|
||||
private ElementReference _retryControl;
|
||||
private bool _restoreRetryFocus;
|
||||
private bool _focusFilterAfterRetry;
|
||||
private bool _structureLoaded;
|
||||
private DateTime? _lastLoadedAtUtc;
|
||||
private string? _confirmedOutcome;
|
||||
private string? _refreshOutcome;
|
||||
private bool _refreshFailed;
|
||||
private long? _postRefreshPresentation;
|
||||
private string OutcomePrefix => _confirmedOutcome is null ? "" : $"Last confirmed action: {_confirmedOutcome}. ";
|
||||
private string SuccessfulReadMessage() => OutcomePrefix + "Structure refreshed.";
|
||||
private string FailedReadMessage() => OutcomePrefix + "Structure refresh failed. " + (_structureLoaded ? "Showing the last loaded structure." : "No structure has been loaded.");
|
||||
private async Task RetryAsync()
|
||||
{
|
||||
await LoadStructureAsync();
|
||||
if (!_disposed && _loadError is not null) { _restoreRetryFocus = true; }
|
||||
if (!_disposed && _loadError is not null) _restoreRetryFocus = true;
|
||||
else if (!_disposed && !_loading)
|
||||
{
|
||||
_refreshOutcome = SuccessfulReadMessage();
|
||||
_focusFilterAfterRetry = true;
|
||||
}
|
||||
}
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
@@ -179,34 +203,67 @@
|
||||
_restoreRetryFocus = false;
|
||||
await _retryControl.FocusAsync();
|
||||
}
|
||||
if (_focusFilterAfterRetry && !_disposed)
|
||||
{
|
||||
_focusFilterAfterRetry = false;
|
||||
if (!_areaModalVisible && !_lineModalVisible && !_importModalVisible && _confirmNode is null)
|
||||
await _filterControl.FocusAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly CancellationTokenSource _readLifetime = new();
|
||||
private bool _readPending;
|
||||
private long? _explicitRead;
|
||||
private bool _disposed;
|
||||
|
||||
protected override Task OnInitializedAsync() => LoadStructureAsync();
|
||||
|
||||
private async Task LoadStructureAsync()
|
||||
{
|
||||
if (_disposed || _readPending) { return; }
|
||||
_readPending = true;
|
||||
if (_disposed || _explicitRead == _structureGeneration) return;
|
||||
var request = _structureGeneration + 1;
|
||||
_explicitRead = request;
|
||||
if (!_structureLoaded) _loadError = null;
|
||||
try { await ReadStructureAsync(); }
|
||||
finally { if (_explicitRead == request) _explicitRead = null; }
|
||||
}
|
||||
|
||||
private long _structureGeneration;
|
||||
private long _modalGeneration;
|
||||
private bool CurrentModal(long generation) => !_disposed && generation == _modalGeneration;
|
||||
|
||||
private async Task<bool> ReadStructureAsync(long? presentation = null)
|
||||
{
|
||||
if (_disposed || (presentation is { } target && !CurrentModal(target))) return false;
|
||||
var request = ++_structureGeneration;
|
||||
_loading = true;
|
||||
_loadError = null;
|
||||
try
|
||||
{
|
||||
var roots = await Svc.LoadStructureAsync(_readLifetime.Token);
|
||||
if (!_disposed) { _roots = roots; }
|
||||
if (_disposed || request != _structureGeneration || (presentation is { } current && !CurrentModal(current))) return false;
|
||||
_roots = roots;
|
||||
_structureLoaded = true;
|
||||
_lastLoadedAtUtc = DateTime.UtcNow;
|
||||
_refreshFailed = false;
|
||||
_loadError = null;
|
||||
if (_refreshOutcome is not null) _refreshOutcome = SuccessfulReadMessage();
|
||||
return true;
|
||||
}
|
||||
catch (OperationCanceledException) when (_disposed) { }
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_disposed) { _loadError = ex.Message; }
|
||||
if (!_disposed && request == _structureGeneration && (presentation is null || CurrentModal(presentation.Value)))
|
||||
{
|
||||
_loadError = ex.Message;
|
||||
if (_structureLoaded || _confirmedOutcome is not null)
|
||||
{
|
||||
_refreshFailed = true;
|
||||
_refreshOutcome = FailedReadMessage();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_readPending = false;
|
||||
if (!_disposed) { _loading = false; }
|
||||
if (!_disposed && request == _structureGeneration) _loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +275,10 @@
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
++_modalGeneration;
|
||||
++_structureGeneration;
|
||||
_readLifetime.Cancel();
|
||||
_readLifetime.Dispose();
|
||||
}
|
||||
@@ -252,7 +312,7 @@
|
||||
/// </summary>
|
||||
private async Task HandleAddChild(UnsNode node)
|
||||
{
|
||||
if (_modalBusy) { return; }
|
||||
if (_disposed || _modalBusy) { return; }
|
||||
_modalBusy = true;
|
||||
try
|
||||
{
|
||||
@@ -292,15 +352,17 @@
|
||||
/// </summary>
|
||||
private async Task HandleEdit(UnsNode node)
|
||||
{
|
||||
if (_modalBusy) { return; }
|
||||
if (_disposed || _modalBusy) { return; }
|
||||
_modalBusy = true;
|
||||
CloseModals();
|
||||
var presentation = _modalGeneration;
|
||||
try
|
||||
{
|
||||
CloseModals();
|
||||
switch (node.Kind)
|
||||
{
|
||||
case UnsNodeKind.Area:
|
||||
var area = await Svc.LoadAreaAsync(node.EntityId!);
|
||||
var area = await Svc.LoadAreaAsync(node.EntityId!, _readLifetime.Token);
|
||||
if (!CurrentModal(presentation)) return;
|
||||
if (area is null) { return; }
|
||||
_areaModalIsNew = false;
|
||||
_areaModalExisting = area;
|
||||
@@ -309,7 +371,8 @@
|
||||
break;
|
||||
|
||||
case UnsNodeKind.Line:
|
||||
var line = await Svc.LoadLineAsync(node.EntityId!);
|
||||
var line = await Svc.LoadLineAsync(node.EntityId!, _readLifetime.Token);
|
||||
if (!CurrentModal(presentation)) return;
|
||||
if (line is null) { return; }
|
||||
_lineModalIsNew = false;
|
||||
_lineModalExisting = line;
|
||||
@@ -319,6 +382,7 @@
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception) when (!CurrentModal(presentation)) { }
|
||||
finally
|
||||
{
|
||||
_modalBusy = false;
|
||||
@@ -328,6 +392,7 @@
|
||||
/// <summary>Opens the bulk equipment-CSV import modal.</summary>
|
||||
private void OpenImportModal()
|
||||
{
|
||||
if (_disposed) return;
|
||||
CloseModals();
|
||||
_importModalVisible = true;
|
||||
}
|
||||
@@ -336,16 +401,12 @@
|
||||
/// Handles the import modal's close after a run. A bulk import can add equipment across many
|
||||
/// lines and clusters, so the whole structural tree is reloaded rather than refreshed in place.
|
||||
/// </summary>
|
||||
private async Task OnImportedAsync()
|
||||
{
|
||||
_roots = await Svc.LoadStructureAsync();
|
||||
CloseModals();
|
||||
StateHasChanged();
|
||||
}
|
||||
private Task OnImportedAsync(long presentation) => ReloadAndCloseAsync(presentation, "Import processed");
|
||||
|
||||
/// <summary>Opens the delete-confirm modal for a node, stashing it as the pending target.</summary>
|
||||
private void HandleDelete(UnsNode node)
|
||||
{
|
||||
if (_disposed) return;
|
||||
CloseModals();
|
||||
_confirmNode = node;
|
||||
}
|
||||
@@ -354,34 +415,36 @@
|
||||
/// Performs the pending delete. Loads the entity's RowVersion first, then dispatches on Kind.
|
||||
/// Area/Line/Equipment trigger a full structural reload on success.
|
||||
/// </summary>
|
||||
private async Task ConfirmDeleteAsync()
|
||||
private async Task ConfirmDeleteAsync(long presentation, UnsNode node)
|
||||
{
|
||||
if (_confirmNode is null) { return; }
|
||||
if (!CurrentModal(presentation) || !ReferenceEquals(_confirmNode, node) || _confirmBusy) return;
|
||||
|
||||
_confirmBusy = true;
|
||||
_confirmError = null;
|
||||
try
|
||||
{
|
||||
var node = _confirmNode;
|
||||
UnsMutationResult result;
|
||||
|
||||
switch (node.Kind)
|
||||
{
|
||||
case UnsNodeKind.Area:
|
||||
var area = await Svc.LoadAreaAsync(node.EntityId!);
|
||||
if (area is null) { await ReloadAndCloseAsync(); return; }
|
||||
var area = await Svc.LoadAreaAsync(node.EntityId!, _readLifetime.Token);
|
||||
if (!CurrentModal(presentation)) return;
|
||||
if (area is null) { await ReloadAndCloseAsync(presentation, "Target is no longer available"); return; }
|
||||
result = await Svc.DeleteAreaAsync(node.EntityId!, area.RowVersion);
|
||||
break;
|
||||
|
||||
case UnsNodeKind.Line:
|
||||
var line = await Svc.LoadLineAsync(node.EntityId!);
|
||||
if (line is null) { await ReloadAndCloseAsync(); return; }
|
||||
var line = await Svc.LoadLineAsync(node.EntityId!, _readLifetime.Token);
|
||||
if (!CurrentModal(presentation)) return;
|
||||
if (line is null) { await ReloadAndCloseAsync(presentation, "Target is no longer available"); return; }
|
||||
result = await Svc.DeleteLineAsync(node.EntityId!, line.RowVersion);
|
||||
break;
|
||||
|
||||
case UnsNodeKind.Equipment:
|
||||
var equipment = await Svc.LoadEquipmentAsync(node.EntityId!);
|
||||
if (equipment is null) { await ReloadAndCloseAsync(); return; }
|
||||
var equipment = await Svc.LoadEquipmentAsync(node.EntityId!, _readLifetime.Token);
|
||||
if (!CurrentModal(presentation)) return;
|
||||
if (equipment is null) { await ReloadAndCloseAsync(presentation, "Target is no longer available"); return; }
|
||||
result = await Svc.DeleteEquipmentAsync(node.EntityId!, equipment.RowVersion);
|
||||
break;
|
||||
|
||||
@@ -402,15 +465,17 @@
|
||||
break;
|
||||
}
|
||||
|
||||
if (!CurrentModal(presentation)) return;
|
||||
if (result.Ok)
|
||||
{
|
||||
await ReloadAndCloseAsync();
|
||||
await ReloadAndCloseAsync(presentation, "Deleted");
|
||||
}
|
||||
else
|
||||
{
|
||||
_confirmError = result.Error;
|
||||
}
|
||||
}
|
||||
catch (Exception) when (!CurrentModal(presentation)) { }
|
||||
finally
|
||||
{
|
||||
_confirmBusy = false;
|
||||
@@ -418,24 +483,49 @@
|
||||
}
|
||||
|
||||
/// <summary>Reloads the tree after a successful modal save and closes any open modal.</summary>
|
||||
private async Task OnModalSavedAsync()
|
||||
private Task OnModalSavedAsync(long presentation) => ReloadAndCloseAsync(presentation, "Saved changes");
|
||||
|
||||
/// <summary>Only a current presentation may apply its reload and close itself.</summary>
|
||||
private async Task ReloadAndCloseAsync(long presentation, string outcome)
|
||||
{
|
||||
_roots = await Svc.LoadStructureAsync();
|
||||
CloseModals();
|
||||
StateHasChanged();
|
||||
if (!CurrentModal(presentation)) return;
|
||||
_confirmedOutcome = outcome;
|
||||
_loadError = null;
|
||||
_refreshFailed = false;
|
||||
_refreshOutcome = OutcomePrefix + "Refreshing structure…";
|
||||
_postRefreshPresentation = presentation;
|
||||
try
|
||||
{
|
||||
var applied = await ReadStructureAsync(presentation);
|
||||
if (CurrentModal(presentation) && applied) _refreshOutcome = SuccessfulReadMessage();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_postRefreshPresentation == presentation) _postRefreshPresentation = null;
|
||||
}
|
||||
if (CurrentModal(presentation)) CloseModals();
|
||||
}
|
||||
|
||||
/// <summary>Reloads the tree after a successful delete and closes the confirm modal.</summary>
|
||||
private async Task ReloadAndCloseAsync()
|
||||
private void ClosePresentation(long presentation)
|
||||
{
|
||||
_roots = await Svc.LoadStructureAsync();
|
||||
CloseModals();
|
||||
StateHasChanged();
|
||||
if (CurrentModal(presentation)) CloseModals();
|
||||
}
|
||||
|
||||
private void CancelConfirm(long presentation)
|
||||
{
|
||||
if (CurrentModal(presentation) && !_confirmBusy) CloseModals();
|
||||
}
|
||||
|
||||
/// <summary>Closes every modal and clears its transient state.</summary>
|
||||
private void CloseModals()
|
||||
{
|
||||
if (_postRefreshPresentation == _modalGeneration)
|
||||
{
|
||||
_postRefreshPresentation = null;
|
||||
_refreshFailed = true;
|
||||
_refreshOutcome = OutcomePrefix + "Structure refresh was superseded. Retry refresh to update the loaded structure.";
|
||||
}
|
||||
++_modalGeneration;
|
||||
_areaModalVisible = false;
|
||||
_areaModalIsNew = false;
|
||||
_areaModalClusterId = null;
|
||||
|
||||
Reference in New Issue
Block a user