Files
Servers/MoonlightServers.Frontend/UI/Views/Client/Manage.razor

251 lines
9.0 KiB
Plaintext

@page "/servers/{ServerId:int}"
@using Microsoft.AspNetCore.SignalR.Client
@using MoonlightServers.Shared.Http.Responses.Users.Servers
@using MoonCore.Blazor.Tailwind.Components
@using MoonCore.Exceptions
@using MoonCore.Helpers
@using MoonlightServers.Frontend.Interfaces
@using MoonlightServers.Frontend.Models
@using MoonlightServers.Shared.Enums
@using MoonlightServers.Frontend.UI.Components
@using MoonlightServers.Frontend.UI.Components.Servers.ServerTabs
@inject HttpApiClient ApiClient
@inject IEnumerable<IServerTabProvider> TabProviders
@implements IAsyncDisposable
<LazyLoader Load="Load">
@if (NotFound)
{
<div class="flex flex-col justify-center text-center">
<img class="h-48 mt-5 mb-3" src="/svg/notfound.svg" alt="Not found illustration">
<h3 class="mt-2 font-semibold text-white text-lg">
Server not found
</h3>
<p class="mt-1 text-gray-300">
The server you requested does not exist
</p>
</div>
}
else
{
<div class="card card-body justify-between py-2.5 px-5 flex-row">
<div class="flex flex-row items-center">
@{
var bgColor = State switch
{
ServerState.Installing => "bg-primary-500",
ServerState.Offline => "bg-danger-500",
ServerState.Starting => "bg-warning-500",
ServerState.Stopping => "bg-warning-500",
ServerState.Online => "bg-success-500",
_ => "bg-gray-500"
};
}
<div class="p-2.5 rounded-full @bgColor me-3"></div>
<div class="flex flex-col">
<div class="hidden sm:flex text-lg font-semibold">@Server.Name</div>
<div class="hidden text-sm text-gray-400 md:flex gap-x-3">
<span>
<i class="icon-sparkles me-0.5 align-middle"></i>
<span class="align-middle">@Server.StarName</span>
</span>
<span>
<i class="icon-database me-0.5 align-middle"></i>
<span class="align-middle">@Server.NodeName</span>
</span>
</div>
</div>
</div>
<div class="flex flex-row items-center">
<div class="flex gap-x-1.5">
@if (State == ServerState.Offline)
{
<WButton CssClasses="btn btn-success" OnClick="_ => Start()">
<i class="icon-play me-1 align-middle"></i>
<span class="align-middle">Start</span>
</WButton>
}
else
{
<button type="button" class="btn btn-success" disabled="disabled">
<i class="icon-play me-1 align-middle"></i>
<span class="align-middle">Start</span>
</button>
}
@if (State == ServerState.Online)
{
<button type="button" class="btn btn-primary">
<i class="icon-rotate-ccw me-1 align-middle"></i>
<span class="align-middle">Restart</span>
</button>
}
else
{
<button type="button" class="btn btn-primary" disabled="disabled">
<i class="icon-rotate-ccw me-1 align-middle"></i>
<span class="align-middle">Restart</span>
</button>
}
@if (State == ServerState.Starting || State == ServerState.Online || State == ServerState.Stopping)
{
if (State == ServerState.Stopping)
{
<WButton CssClasses="btn btn-danger" OnClick="_ => Kill()">
<i class="icon-bomb me-1 align-middle"></i>
<span class="align-middle">Kill</span>
</WButton>
}
else
{
<WButton CssClasses="btn btn-danger" OnClick="_ => Stop()">
<i class="icon-squircle me-1 align-middle"></i>
<span class="align-middle">Stop</span>
</WButton>
}
}
else
{
<button type="button" class="btn btn-danger" disabled="disabled">
<i class="icon-squircle me-1 align-middle"></i>
<span class="align-middle">Stop</span>
</button>
}
</div>
</div>
</div>
<div class="mt-3">
<Tabs>
@foreach (var tab in Tabs)
{
<Tab Name="@tab.Name">
@{
var rf = ComponentHelper.FromType(tab.ComponentType, parameters =>
{
parameters.Add("Server", Server);
parameters.Add("State", State);
parameters.Add("InitialConsoleMessage", InitialConsoleMessage);
parameters.Add("HubConnection", HubConnection);
parameters.Add("Parent", this);
});
}
@rf
</Tab>
}
</Tabs>
</div>
}
</LazyLoader>
@code
{
[Parameter] public int ServerId { get; set; }
private List<ServerTab> Tabs = new();
private ServerDetailResponse Server;
private bool NotFound = false;
private ServerState State;
private string InitialConsoleMessage;
private HubConnection HubConnection;
private async Task Load(LazyLoader _)
{
try
{
// Load meta data
Server = await ApiClient.GetJson<ServerDetailResponse>(
$"api/client/servers/{ServerId}"
);
// Load server tabs
foreach (var serverTabProvider in TabProviders)
Tabs.AddRange(await serverTabProvider.GetTabs(Server));
// Load initial status for first render
var status = await ApiClient.GetJson<ServerStatusResponse>(
$"api/client/servers/{ServerId}/status"
);
State = status.State;
// Load initial messages
var initialLogs = await ApiClient.GetJson<ServerLogsResponse>(
$"api/client/servers/{ServerId}/logs"
);
InitialConsoleMessage = "";
foreach (var message in initialLogs.Messages)
InitialConsoleMessage += message;
// Load websocket meta
var websocketDetails = await ApiClient.GetJson<ServerWebSocketResponse>(
$"api/client/servers/{ServerId}/ws"
);
// Build signal r
HubConnection = new HubConnectionBuilder()
.WithUrl(websocketDetails.Target)
.Build();
// Define handlers
HubConnection.On<string>("StateChanged", async stateStr =>
{
if (!Enum.TryParse(stateStr, out ServerState receivedState))
return;
State = receivedState;
await InvokeAsync(StateHasChanged);
});
HubConnection.On<string>("ConsoleOutput", async content =>
{
// Update initial message
InitialConsoleMessage += content;
await InvokeAsync(StateHasChanged);
});
// Connect
await HubConnection.StartAsync();
// Authenticate
await HubConnection.SendAsync("Authenticate", websocketDetails.AccessToken);
}
catch (HttpApiException e)
{
if (e.Status == 404)
NotFound = true;
else
throw;
}
}
private async Task Start()
=> await ApiClient.Post($"api/client/servers/{Server.Id}/start");
private async Task Stop()
=> await ApiClient.Post($"api/client/servers/{Server.Id}/stop");
private async Task Kill()
=> await ApiClient.Post($"api/client/servers/{Server.Id}/kill");
public async ValueTask DisposeAsync()
{
if (HubConnection.State == HubConnectionState.Connected)
await HubConnection.StopAsync();
await HubConnection.DisposeAsync();
}
}