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

93 lines
3.0 KiB
Plaintext

@page "/instances/{InstanceGuid:guid}"
@attribute [Authorize(Permission.ViewInstancesPolicy)]
@inherits PhantomComponent
@using Phantom.Common.Data.Instance
@using Phantom.Common.Data.Replies
@using Phantom.Server.Services.Audit
@using Phantom.Server.Services.Instances
@implements IDisposable
@inject InstanceManager InstanceManager
@inject AuditLog AuditLog
@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">
<PermissionView Permission="Permission.ControlInstances">
<button type="button" class="btn btn-success" @onclick="LaunchInstance" disabled="@(isLaunchingInstance || !Instance.Status.CanLaunch())">Launch</button>
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#stop-instance" disabled="@(!Instance.Status.CanStop())">Stop...</button>
<span><!-- extra spacing --></span>
</PermissionView>
<InstanceStatusText Status="Instance.Status" />
<PermissionView Permission="Permission.CreateInstances">
<a href="instances/@InstanceGuid/edit" class="btn btn-warning ms-auto">Edit Configuration</a>
</PermissionView>
</div>
@if (lastError != null) {
<p class="text-danger mt-2">@lastError</p>
}
<PermissionView Permission="Permission.ViewInstanceLogs">
<InstanceLog InstanceGuid="InstanceGuid" />
</PermissionView>
<PermissionView Permission="Permission.ControlInstances">
<div class="mb-3">
<InstanceCommandInput InstanceGuid="InstanceGuid" Disabled="@(!Instance.Status.CanSendCommand())" />
</div>
<InstanceStopDialog InstanceGuid="InstanceGuid" ModalId="stop-instance" Disabled="@(!Instance.Status.CanStop())" />
</PermissionView>
}
@code {
[Parameter]
public Guid InstanceGuid { get; set; }
private string? lastError = null;
private bool isLaunchingInstance = 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;
try {
if (!await CheckPermission(Permission.ControlInstances)) {
lastError = "You do not have permission to launch instances.";
return;
}
var result = await InstanceManager.LaunchInstance(InstanceGuid);
if (result.Is(LaunchInstanceResult.LaunchInitiated)) {
await AuditLog.AddInstanceLaunchedEvent(InstanceGuid);
}
else {
lastError = result.ToSentence(LaunchInstanceResultExtensions.ToSentence);
}
} finally {
isLaunchingInstance = false;
}
}
public void Dispose() {
InstanceManager.InstancesChanged.Unsubscribe(this);
}
}