fix(admin-ui): harden read-only script source dialog lifecycle

This commit is contained in:
Joseph Doherty
2026-11-14 00:39:03 -05:00
parent ea5bb6a0bc
commit f953d230f8
34 changed files with 2125 additions and 48 deletions
@@ -5,6 +5,7 @@
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptList
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@implements IDisposable
<PageErrorBoundary>
<PageTitle>Scripts · OtOpcUa</PageTitle>
@@ -71,52 +72,44 @@ else
replacing the list) would unmount the subtree and dispose the Close handler's binding id — a
click already in flight then throws GetRequiredEventBindingEntry. Latent today (_rows is written
once, in OnInitializedAsync) but the shape bit ScadaBridge live the moment a refresh landed. *@
@if (_sourceScriptId is not null)
{
@{
var sourceId = _sourceScriptId;
var sourceGeneration = _sourceGeneration;
var src = FindSourceScript();
<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 modal-dialog-scrollable" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
@if (src is not null)
{
@(string.IsNullOrWhiteSpace(src.Name) ? src.ScriptId : src.Name)
<span class="mono small text-muted ms-2">@src.ScriptId</span>
}
else
{
<span class="mono">@_sourceScriptId</span>
}
</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="CloseSource"></button>
</div>
<div class="modal-body">
@if (_sourceError is not null)
{
<div class="panel notice notice-alert mb-0">@_sourceError</div>
}
else if (src is null || (_sourceLoaded && _sourceCode is null))
{
<p class="text-muted mb-0">This script is no longer loaded.</p>
}
else if (!_sourceLoaded)
{
<p class="text-muted mb-0">Loading source…</p>
}
else
{
<pre class="mono small mb-0" style="background:var(--zebra-bg);padding:1rem;border-radius:4px;overflow:auto">@_sourceCode</pre>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" @onclick="CloseSource">Close</button>
</div>
</div>
</div>
</div>
var sourceTitle = src is null ? sourceId ?? "Script source" : string.IsNullOrWhiteSpace(src.Name) ? src.ScriptId : src.Name;
}
<ModalFrame Visible="@(sourceId is not null)" Target="@sourceGeneration" Title="@sourceTitle"
DialogClass="modal-lg modal-dialog-scrollable" OnClose="() => CloseSource(sourceGeneration, sourceId)">
<TitleContent>
<span class="text-break">@sourceTitle</span>
@if (src is not null)
{
<span class="mono small text-muted text-break ms-2">@src.ScriptId</span>
}
</TitleContent>
<ChildContent>
@if (_sourceError is not null)
{
<div class="notice notice-alert mb-0" role="alert">@_sourceError</div>
}
else if (src is null || (_sourceLoaded && _sourceCode is null))
{
<p class="text-muted mb-0" role="status">This script is no longer loaded.</p>
}
else if (!_sourceLoaded)
{
<p class="text-muted mb-0" role="status">Loading source…</p>
}
else
{
<pre class="mono small mb-0" role="region" aria-label="@($"Source for {sourceTitle}")" tabindex="0"
style="background:var(--zebra-bg);padding:1rem;border-radius:4px;overflow:auto">@_sourceCode</pre>
}
</ChildContent>
<Footer>
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => CloseSource(sourceGeneration, sourceId)">Close</button>
</Footer>
</ModalFrame>
</PageErrorBoundary>
@code {
@@ -133,11 +126,15 @@ else
private bool _sourceLoaded;
private string? _sourceError;
private int _sourceGeneration;
private bool _disposed;
private bool CurrentSource(int generation, string? scriptId)
=> !_disposed && scriptId is not null && generation == _sourceGeneration && scriptId == _sourceScriptId;
protected override async Task OnInitializedAsync()
{
await using var db = await DbFactory.CreateDbContextAsync();
_rows = await ScriptListQueries.LoadRowsAsync(db);
var rows = await ScriptListQueries.LoadRowsAsync(db);
if (!_disposed) _rows = rows;
}
/// <summary>Re-resolves the open dialog's script from the current list on every render. Drives
@@ -151,6 +148,7 @@ else
// One dialog at a time: opening another row replaces the id.
private async Task ShowSource(string scriptId)
{
if (_disposed) return;
var generation = ++_sourceGeneration;
_sourceScriptId = scriptId;
_sourceCode = null;
@@ -166,18 +164,24 @@ else
catch (Exception ex)
{
// A ConfigDb hiccup must not escape the click handler into the page's error boundary (review r0 M7).
if (generation != _sourceGeneration) return;
if (!CurrentSource(generation, scriptId)) return;
_sourceError = $"The script source could not be loaded: {ex.Message}";
_sourceLoaded = true;
return;
}
if (generation != _sourceGeneration) return;
if (!CurrentSource(generation, scriptId)) return;
_sourceCode = source;
_sourceLoaded = true;
}
private void CloseSource()
private void CloseSource(int generation, string? scriptId)
{
if (!CurrentSource(generation, scriptId)) return;
ClearSource();
}
private void ClearSource()
{
_sourceGeneration++;
_sourceScriptId = null;
@@ -185,4 +189,11 @@ else
_sourceLoaded = false;
_sourceError = null;
}
/// <summary>Retires pending read results and captured close handlers without changing the database query.</summary>
public void Dispose()
{
_disposed = true;
ClearSource();
}
}