1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-22 08:42:44 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web/Shared/InstanceCommandInput.razor
2022-12-30 03:26:06 +01:00

56 lines
1.9 KiB
Plaintext

@using Phantom.Server.Services.Instances
@using Phantom.Server.Services.Audit
@using Phantom.Common.Data.Replies
@inherits PhantomComponent
@inject InstanceManager InstanceManager
@inject AuditLog AuditLog
<Form Model="form" OnSubmit="ExecuteCommand">
<label for="command-input" class="form-label">Instance Name</label>
<div class="input-group flex-nowrap">
<span class="input-group-text" style="padding-top: 0.3rem;">/</span>
<input id="command-input" class="form-control" type="text" placeholder="command" @bind="form.Command" @bind:event="oninput" disabled="@(Disabled || form.SubmitModel.IsSubmitting)" @ref="commandInputElement" />
<FormButtonSubmit Label="Execute" class="btn btn-primary" disabled="@(Disabled || string.IsNullOrWhiteSpace(form.Command))" />
</div>
<FormSubmitError />
</Form>
@code {
[Parameter]
public Guid InstanceGuid { get; set; }
[Parameter]
public bool Disabled { get; set; }
private readonly SendCommandFormModel form = new ();
private sealed class SendCommandFormModel : FormModel {
public string Command { get; set; } = string.Empty;
}
private ElementReference commandInputElement;
private async Task ExecuteCommand(EditContext context) {
await form.SubmitModel.StartSubmitting();
if (!await CheckPermission(Permission.ControlInstances)) {
form.SubmitModel.StopSubmitting("You do not have permission to execute commands.");
return;
}
var result = await InstanceManager.SendCommand(InstanceGuid, form.Command);
if (result.Is(SendCommandToInstanceResult.Success)) {
await AuditLog.AddInstanceCommandExecutedEvent(InstanceGuid, form.Command);
form.Command = string.Empty;
form.SubmitModel.StopSubmitting();
}
else {
form.SubmitModel.StopSubmitting(result.ToSentence(SendCommandToInstanceResultExtensions.ToSentence));
}
await commandInputElement.FocusAsync(preventScroll: true);
}
}