LANCommander/LANCommander.Server/UI/Components/ScriptEditor.razor
2025-02-08 23:02:43 -06:00

103 lines
3.4 KiB
Text

@using System.Linq.Expressions
@using LANCommander.SDK.Extensions
@using LANCommander.SDK.Enums
@inject ScriptService ScriptService
@inject ModalService ModalService
@inject IMessageService MessageService
<Flex Vertical Gap="FlexGap.Large">
<DataTable
@ref="Table"
TItem="Script"
HidePagination
Responsive
Query="GetQueryExpression()">
<BoundDataColumn Property="s => s.Type">
@context.Type.GetDisplayName()
</BoundDataColumn>
<BoundDataColumn Property="s => s.CreatedBy != null ? s.CreatedBy.UserName : String.Empty" Include="CreatedBy">
@context.CreatedBy?.UserName
</BoundDataColumn>
<BoundDataColumn Property="s => s.CreatedOn" Format="MM/dd/yyyy hh:mm tt" />
<DataActions>
<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 {
[Parameter] public Guid GameId { get; set; }
[Parameter] public Guid RedistributableId { get; set; }
[Parameter] public Guid ServerId { get; set; }
[Parameter] public Guid ArchiveId { 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;
ICollection<Script> Scripts { get; set; } = new List<Script>();
Expression<Func<Script, bool>> GetQueryExpression()
{
if (!GameId.IsNullOrEmpty())
return s => s.GameId == GameId;
else if (!RedistributableId.IsNullOrEmpty())
return s => s.RedistributableId == RedistributableId;
else if (!ServerId.IsNullOrEmpty())
return s => s.ServerId == ServerId;
else
return s => s.GameId == Guid.Empty && s.RedistributableId == Guid.Empty && 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,
ArchiveId = ArchiveId,
GameId = GameId,
RedistributableId = RedistributableId,
ServerId = ServerId,
};
var modalRef = await ModalService.CreateModalAsync<ScriptEditorDialog, ScriptEditorOptions, Script>(modalOptions, options);
modalRef.OnOk = async (script) =>
{
Table.Reload();
};
}
async void Delete(Script script = null)
{
if (script != null)
await ScriptService.DeleteAsync(script);
await MessageService.Success("Script deleted!");
}
}