LANCommander/LANCommander.Server/UI/Pages/Games/Components/KeysEditor.razor
2025-05-23 02:17:45 -05:00

135 lines
4.4 KiB
Text

@inject KeyService KeyService
@inject IMessageService MessageService
@if (Keys != null)
{
<Flex Vertical Gap="FlexGap.Small">
<GridRow>
<GridCol Span="8">
<Statistic Title="Available" Value="Keys.Count - AllocatedKeys" Style="text-align: center;" />
</GridCol>
<GridCol Span="8">
<Statistic Title="Allocated" Value="AllocatedKeys" Style="text-align: center;" />
</GridCol>
<GridCol Span="8">
<Statistic Title="Total" Value="Keys.Count" Style="text-align: center;" />
</GridCol>
</GridRow>
<DataTable
@ref="Table"
TItem="Key"
Responsive
Query="k => k.GameId == GameId">
<RightToolbar>
<Button Type="@ButtonType.Default" OnClick="() => Edit()">Edit</Button>
<Button Type="@ButtonType.Primary" OnClick="() => Save()">Save</Button>
</RightToolbar>
<Columns>
<BoundDataColumn Property="k => k.Value">
<InputPassword @bind-Value="@context.Value"/>
</BoundDataColumn>
<BoundDataColumn Property="k => k.AllocationMethod" Title="Allocation Method"/>
<DataColumn TData="string" Title="Claimed By" Include="ClaimedByUser">
@switch (context.AllocationMethod)
{
case KeyAllocationMethod.MacAddress:
<text>@context.ClaimedByMacAddress</text>
break;
case KeyAllocationMethod.UserAccount:
<text>@context.ClaimedByUser?.UserName</text>
break;
}
</DataColumn>
<BoundDataColumn Property="s => s.ClaimedOn" Title="Claimed" Sortable>
<LocalTime Value="context.CreatedOn" Relative />
</BoundDataColumn>
<DataActions TData="string">
@if (context.IsAllocated())
{
<Button OnClick="() => Release(context)">Release</Button>
}
</DataActions>
</Columns>
</DataTable>
</Flex>
<Modal Title="Edit Keys" Visible="EditModalVisible" Maximizable="false" DefaultMaximized="true" OnCancel="() => EditModalVisible = false" OnOk="Save">
<StandaloneCodeEditor @ref="Editor" Id="editor" ConstructionOptions="EditorConstructionOptions" />
</Modal>
}
<style>
.monaco-editor-container {
height: 600px;
}
</style>
@code {
[Parameter] public Guid GameId { get; set; }
ICollection<Key> Keys = new List<Key>();
int AllocatedKeys;
bool EditModalVisible = false;
DataTable<Key> Table;
StandaloneCodeEditor? Editor;
StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
{
return new StandaloneEditorConstructionOptions
{
AutomaticLayout = true,
Language = "text",
Value = String.Join('\n', Keys.Select(k => k.Value)),
Theme = "vs-dark",
};
}
protected override async Task OnInitializedAsync()
{
if (Keys == null)
Keys = new List<Key>();
Keys = await KeyService.GetAsync(k => k.GameId == GameId);
AllocatedKeys = Keys.Count(k => k.IsAllocated());
}
void Edit()
{
EditModalVisible = true;
}
async Task Release(Key key)
{
await KeyService.ReleaseAsync(key);
await MessageService.SuccessAsync("Key was unallocated!");
}
async Task Save()
{
var value = await Editor.GetValue();
var keys = value.Split("\n").Select(k => k.Trim()).Where(k => !String.IsNullOrWhiteSpace(k)).ToList();
var keysDeleted = Keys.Where(k => !keys.Contains(k.Value)).ToList();
var keysAdded = keys.Where(k => !Keys.Any(gk => gk.Value == k)).ToList();
foreach (var key in keysDeleted)
KeyService.DeleteAsync(key);
foreach (var key in keysAdded)
await KeyService.AddAsync(new Key()
{
GameId = GameId,
Value = key
});
EditModalVisible = false;
MessageService.Success("Keys updated!");
}
}