feat(adminui): create-new-script from the inline virtual-tag panel

This commit is contained in:
Joseph Doherty
2026-06-16 16:44:14 -04:00
parent 526eebb3bb
commit c98625fd9f
4 changed files with 192 additions and 1 deletions
@@ -418,6 +418,19 @@ public interface IUnsTreeService
/// <returns>The scripts projected to <c>(ScriptId, Display)</c> pairs.</returns>
Task<IReadOnlyList<(string ScriptId, string Display)>> LoadScriptsAsync(CancellationToken ct = default);
/// <summary>
/// Creates a new, empty C# script row so the inline virtual-tag panel can bind a brand-new script
/// without leaving the modal. The <c>ScriptId</c> is system-generated (<c>SC-</c> + the first 12 hex
/// chars of a fresh <see cref="Guid"/>, mirroring the Script-edit page's convention); the source is
/// blank, the <c>SourceHash</c> is the SHA-256 of that blank source (so it stays consistent with the
/// inline save), and the language is <c>CSharp</c>. A whitespace-only name falls back to a default.
/// The new id is returned via <see cref="UnsMutationResult.CreatedId"/> so the caller can bind it.
/// </summary>
/// <param name="name">The operator-friendly script name; whitespace falls back to a default.</param>
/// <param name="ct">A token to cancel the operation.</param>
/// <returns>Success carrying the generated <c>SC-…</c> id in <see cref="UnsMutationResult.CreatedId"/>.</returns>
Task<UnsMutationResult> CreateScriptAsync(string name, CancellationToken ct = default);
/// <summary>
/// Creates a new equipment-bound virtual tag (plan decision #2 — virtual tags are always scoped
/// to an equipment). Fails if the equipment does not exist, if no script is chosen, if neither a
@@ -1011,6 +1011,30 @@ public sealed class UnsTreeService(IDbContextFactory<OtOpcUaConfigDbContext> dbF
.ToList();
}
/// <inheritdoc />
public async Task<UnsMutationResult> CreateScriptAsync(string name, CancellationToken ct = default)
{
await using var db = await dbFactory.CreateDbContextAsync(ct);
// System-generated id, mirroring the Script-edit page's "SC-{12 hex}" convention.
var scriptId = $"SC-{Guid.NewGuid().ToString("N")[..12]}";
var resolvedName = string.IsNullOrWhiteSpace(name) ? "New script" : name.Trim();
// Blank source; SourceHash is the SHA-256 of that blank body so it stays consistent with the
// inline save (UpdateScriptSourceAsync recomputes it the same way).
db.Scripts.Add(new Script
{
ScriptId = scriptId,
Name = resolvedName,
Language = "CSharp",
SourceCode = "",
SourceHash = HashSource(""),
});
await db.SaveChangesAsync(ct);
return new UnsMutationResult(true, null, scriptId);
}
/// <summary>
/// When the bound script uses the reserved <c>{{equip}}</c> token, the owning equipment must have
/// a derivable tag base (≥1 driver tag, all sharing one object prefix). Returns a rejection result