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/Pages/InstanceDetail.razor

126 lines
4.0 KiB
Plaintext

@page "/instances/{InstanceGuid:guid}"
@attribute [Authorize(Permission.ViewInstancesPolicy)]
@using Phantom.Common.Data.Replies
@using Phantom.Common.Data.Web.Instance
@using Phantom.Common.Data.Web.Users
@using Phantom.Utils.Result
@using Phantom.Common.Data.Instance
@using Phantom.Web.Services.Instances
@using Phantom.Web.Services.Authorization
@inherits PhantomComponent
@inject InstanceManager InstanceManager
@if (isLoading) {
<h1>Instance</h1>
<p>Loading...</p>
return;
}
@if (Instance == null) {
<h1>Instance Not Found</h1>
<p>Return to <a href="instances">all instances</a>.</p>
return;
}
<div class="d-flex flex-row align-items-center gap-3 mb-3">
<h1 class="mb-0">Instance: @Instance.Configuration.InstanceName</h1>
<span class="fs-4 text-muted">//</span>
<div class="mt-2">
<InstanceStatusText Status="Instance.Status" />
</div>
</div>
<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>
<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" role="alert">@lastError</p>
}
<PermissionView Permission="Permission.ViewInstanceLogs">
<InstanceLog InstanceGuid="InstanceGuid" />
</PermissionView>
<PermissionView Permission="Permission.ControlInstances">
<div class="my-3">
<InstanceCommandInput AgentGuid="Instance.Configuration.AgentGuid" InstanceGuid="InstanceGuid" Disabled="@(!Instance.Status.CanSendCommand())" />
</div>
<InstanceStopDialog AgentGuid="Instance.Configuration.AgentGuid" InstanceGuid="InstanceGuid" ModalId="stop-instance" Disabled="@(!Instance.Status.CanStop())" />
</PermissionView>
@code {
[Parameter]
public Guid InstanceGuid { get; init; }
private Instance? Instance { get; set; }
private bool isLoading = true;
private string? lastError = null;
private bool isLaunchingInstance = false;
protected override async Task OnInitializedAsync() {
Instance = InstanceManager.GetByGuid(await GetAuthenticatedUser(), InstanceGuid);
isLoading = false;
if (Instance != null) {
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 (Instance == null) {
lastError = "Instance not found.";
return;
}
var result = await InstanceManager.LaunchInstance(await GetAuthenticatedUser(), Instance.Configuration.AgentGuid, InstanceGuid, CancellationToken);
switch (result.Variant()) {
case Ok<LaunchInstanceResult>(LaunchInstanceResult.LaunchInitiated):
break;
case Ok<LaunchInstanceResult>(var launchInstanceResult):
lastError = launchInstanceResult.ToSentence();
break;
case Err<UserInstanceActionFailure>(OfInstanceActionFailure(var failure)):
lastError = failure.ToSentence();
break;
case Err<UserInstanceActionFailure>(OfUserActionFailure(UserActionFailure.NotAuthorized)):
lastError = "You do not have permission to launch this instance.";
break;
default:
lastError = "Unknown error.";
break;
}
} finally {
isLaunchingInstance = false;
}
}
protected override void OnDisposed() {
InstanceManager.InstancesChanged.Unsubscribe(this);
}
}