@page "/design/shared-scripts"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.Scripts
@using ScadaLink.Commons.Interfaces.Repositories
@using ScadaLink.TemplateEngine
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)]
@inject ITemplateEngineRepository TemplateEngineRepository
@inject SharedScriptService SharedScriptService
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavigationManager
Shared Scripts
@if (_loading)
{
}
else if (_errorMessage != null)
{
@_errorMessage
}
else
{
| ID |
Name |
Code (preview) |
Parameters |
Returns |
Actions |
@if (_scripts.Count == 0)
{
| No shared scripts configured. |
}
@foreach (var script in _scripts)
{
| @script.Id |
@script.Name |
@script.Code[..Math.Min(60, script.Code.Length)]@(script.Code.Length > 60 ? "..." : "")
|
@(script.ParameterDefinitions ?? "—") |
@(script.ReturnDefinition ?? "—") |
|
}
}
@code {
private async Task GetCurrentUserAsync()
{
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
return authState.User.FindFirst("Username")?.Value ?? "unknown";
}
private List _scripts = new();
private bool _loading = true;
private string? _errorMessage;
private ToastNotification _toast = default!;
private ConfirmDialog _confirmDialog = default!;
protected override async Task OnInitializedAsync()
{
await LoadDataAsync();
}
private async Task LoadDataAsync()
{
_loading = true;
_errorMessage = null;
try
{
_scripts = (await SharedScriptService.GetAllSharedScriptsAsync()).ToList();
}
catch (Exception ex)
{
_errorMessage = $"Failed to load shared scripts: {ex.Message}";
}
_loading = false;
}
private async Task DeleteScript(SharedScript script)
{
var confirmed = await _confirmDialog.ShowAsync(
$"Delete shared script '{script.Name}'?", "Delete Shared Script");
if (!confirmed) return;
try
{
var user = await GetCurrentUserAsync();
var result = await SharedScriptService.DeleteSharedScriptAsync(script.Id, user);
if (result.IsSuccess)
{
_toast.ShowSuccess($"Script '{script.Name}' deleted.");
await LoadDataAsync();
}
else
{
_toast.ShowError(result.Error);
}
}
catch (Exception ex)
{
_toast.ShowError($"Delete failed: {ex.Message}");
}
}
}