1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-24 13:42:53 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web/Pages/InstanceDetail.razor

108 lines
3.6 KiB
Plaintext

@page "/instances/{InstanceGuid:guid}"
@using Phantom.Common.Data.Instance
@using Phantom.Common.Data.Replies
@using Phantom.Server.Services.Instances
@implements IDisposable
@inject InstanceManager InstanceManager;
@if (Instance == null) {
<h1>Instance Not Found</h1>
<p>Return to <a href="/instances">all instances</a>.</p>
}
else {
<h1>Instance: @Instance.Configuration.InstanceName</h1>
<div class="d-flex flex-row align-items-center gap-2">
<button type="button" class="btn btn-success" @onclick="LaunchInstance" disabled="@(isLaunchingInstance || !Instance.Status.CanLaunch())">Launch</button>
<button type="button" class="btn btn-danger" @onclick="StopInstance" disabled="@(isStoppingInstance || !Instance.Status.CanStop())">Stop</button>
<div class="ms-2">
<InstanceStatusText Status="Instance.Status" />
</div>
</div>
@if (lastError != null) {
<p class="text-danger">@lastError</p>
}
<InstanceLog InstanceGuid="InstanceGuid" />
<div class="mb-3">
<form @onsubmit="ExecuteCommand" class="@(commandError == null ? "" : "is-invalid")">
<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="commandInput" @bind:event="oninput" disabled="@(isSendingCommand || !Instance.Status.CanSendCommand())" @ref="commandInputElement" />
<button type="submit" class="btn btn-primary" disabled="@(string.IsNullOrWhiteSpace(commandInput) || isSendingCommand)">Execute</button>
</div>
</form>
<div class="invalid-feedback mt-2">
@commandError
</div>
</div>
}
@code {
[Parameter]
public Guid InstanceGuid { get; set; }
private string? lastError = null;
private bool isLaunchingInstance = false;
private bool isStoppingInstance = false;
private ElementReference commandInputElement;
private string commandInput = string.Empty;
private string? commandError = null;
private bool isSendingCommand = false;
private Instance? Instance { get; set; }
protected override void OnInitialized() {
InstanceManager.InstancesChanged.Subscribe(this, instances => {
var newInstance = instances.TryGetValue(InstanceGuid, out var instance) ? instance : null;
if (newInstance != Instance) {
Instance = newInstance;
InvokeAsync(StateHasChanged);
}
});
}
private async Task LaunchInstance() {
isLaunchingInstance = true;
lastError = null;
var result = await InstanceManager.LaunchInstance(InstanceGuid);
lastError = result == LaunchInstanceResult.LaunchInitiated ? null : result.ToSentence();
isLaunchingInstance = false;
}
private async Task StopInstance() {
isStoppingInstance = true;
lastError = null;
var result = await InstanceManager.StopInstance(InstanceGuid);
lastError = result == StopInstanceResult.StopInitiated ? null : result.ToSentence();
isStoppingInstance = false;
}
private async Task ExecuteCommand() {
isSendingCommand = true;
commandError = null;
await Task.Yield();
var result = await InstanceManager.SendCommand(InstanceGuid, commandInput);
if (result == SendCommandToInstanceResult.Success) {
commandInput = string.Empty;
}
else {
commandError = result.ToSentence();
}
isSendingCommand = false;
await commandInputElement.FocusAsync(preventScroll: true);
}
public void Dispose() {
InstanceManager.InstancesChanged.Unsubscribe(this);
}
}