LANCommander/LANCommander.Server/UI/Components/ScriptEditor.razor
Pat Hartl 20731f355a Fix certain taxonomies (tag, genre, platform) being removed from game on save
Fixes the action, custom field, multiplayer mode, and save path editors from clearing out taxonomies when saving. Also passes the Game/Server/Redistributable ID as a cascading parameter.
2025-12-08 00:51:32 -06:00

124 lines
4 KiB
Text

@using System.Linq.Expressions
@using LANCommander.SDK.Extensions
@using LANCommander.SDK.Enums
@inject ScriptService ScriptService
@inject ModalService ModalService
@inject IMessageService MessageService
@inject ILogger<ScriptEditor> Logger
<Flex Direction="FlexDirection.Vertical" Gap="FlexGap.Large">
<DataTable
@ref="Table"
TItem="Script"
HidePagination
Responsive
Size="TableSize.Small"
Query="GetQueryExpression()">
<BoundDataColumn Property="s => s.Name" />
<BoundDataColumn Property="s => s.Type">
@context.Type.GetDisplayName()
</BoundDataColumn>
<BoundDataColumn Title="Created By" Property="s => s.CreatedBy != null ? s.CreatedBy.UserName : String.Empty" Include="CreatedBy">
@context.CreatedBy?.UserName
</BoundDataColumn>
<BoundDataColumn Property="s => s.CreatedOn">
<LocalTime Value="context.CreatedOn" />
</BoundDataColumn>
<DataActions TData="string">
<Button OnClick="() => Edit(context.Id)" Icon="@IconType.Outline.Edit" Type="ButtonType.Text"/>
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this script?">
<Button Icon="@IconType.Outline.Close" Type="ButtonType.Text" Danger/>
</Popconfirm>
</DataActions>
</DataTable>
<Flex Gap="FlexGap.Small" Justify="FlexJustify.End">
<Button OnClick="() => Edit()" Type="ButtonType.Primary">Add Script</Button>
</Flex>
</Flex>
<style>
.monaco-editor-container {
height: 600px;
}
</style>
@code {
[CascadingParameter(Name = "GameId")] public Guid? GameId { get; set; }
[CascadingParameter(Name = "RedistributableId")] public Guid? RedistributableId { get; set; }
[CascadingParameter(Name = "ServerId")] public Guid? ServerId { get; set; }
[Parameter] public IEnumerable<ScriptType> AllowedTypes { get; set; }
Expression<Func<Script, bool>> Query = s => s.GameId == Guid.Empty && s.RedistributableId == Guid.Empty && s.ServerId == Guid.Empty;
DataTable<Script> Table;
Archive _archive;
Expression<Func<Script, bool>> GetQueryExpression()
{
if (GameId.HasValue)
return s => s.GameId == GameId;
if (RedistributableId.HasValue)
return s => s.RedistributableId == RedistributableId;
if (ServerId.HasValue)
return s => s.ServerId == ServerId;
return s =>
(s.GameId == null || s.GameId == Guid.Empty)
&&
(s.RedistributableId == null || s.RedistributableId == Guid.Empty)
&&
(s.ServerId == null || s.ServerId == Guid.Empty);
}
async void Edit(Guid? scriptId = null)
{
var modalOptions = new ModalOptions()
{
Title = scriptId == null ? "Add Script" : "Edit Script",
Maximizable = false,
DefaultMaximized = true,
Closable = true,
OkText = "Save"
};
var options = new ScriptEditorOptions()
{
ScriptId = scriptId ?? default,
AllowedTypes = AllowedTypes,
GameId = GameId,
RedistributableId = RedistributableId,
ServerId = ServerId,
};
var modalRef = await ModalService.CreateModalAsync<ScriptEditorDialog, ScriptEditorOptions, Script>(modalOptions, options);
modalRef.OnOk = async (script) =>
{
await Table.ReloadAsync();
};
}
async Task Delete(Script script = null)
{
try
{
if (script != null)
await ScriptService.DeleteAsync(script);
await Table.ReloadAsync();
MessageService.Success("Script deleted!");
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not delete script");
MessageService.Error("Could not delete script!");
}
}
}