1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-10-17 03:42:50 +02:00
Minecraft-Phantom-Panel/Web/Phantom.Web/Shared/InstanceCommandInput.razor

70 lines
2.4 KiB
Plaintext

@using Phantom.Common.Data.Replies
@using Phantom.Common.Data.Web.Users
@using Phantom.Utils.Result
@using Phantom.Web.Services.Instances
@inherits Phantom.Web.Components.PhantomComponent
@inject InstanceManager InstanceManager
<Form Model="form" OnSubmit="ExecuteCommand">
<label for="command-input" class="form-label">Execute Command</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, EditorRequired]
public Guid AgentGuid { get; set; }
[Parameter, EditorRequired]
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();
var result = await InstanceManager.SendCommandToInstance(await GetAuthenticatedUser(), AgentGuid, InstanceGuid, form.Command, CancellationToken);
switch (result.Variant()) {
case Ok<SendCommandToInstanceResult>(SendCommandToInstanceResult.Success):
form.Command = string.Empty;
form.SubmitModel.StopSubmitting();
break;
case Ok<SendCommandToInstanceResult>(var sendCommandToInstanceResult):
form.SubmitModel.StopSubmitting(sendCommandToInstanceResult.ToSentence());
break;
case Err<UserInstanceActionFailure>(OfInstanceActionFailure(var failure)):
form.SubmitModel.StopSubmitting(failure.ToSentence());
break;
case Err<UserInstanceActionFailure>(OfUserActionFailure(UserActionFailure.NotAuthorized)):
form.SubmitModel.StopSubmitting("You do not have permission to send commands to this instance.");
break;
default:
form.SubmitModel.StopSubmitting("Unknown error.");
break;
}
StateHasChanged();
await commandInputElement.FocusAsync(preventScroll: true);
}
}