LANCommander/LANCommander.Launcher/UI/MainLayout.razor

280 lines
No EOL
9.9 KiB
Text

@using System.Management.Automation.Remoting
@using LANCommander.Launcher.Models
@using LANCommander.SDK
@using Photino.Blazor.CustomWindow.Components
@using ConnectionState = LANCommander.Launcher.Models.ConnectionState
@inherits LayoutComponentBase
@inject ImportService ImportService
@inject ProfileService ProfileService
@inject AuthenticationService AuthenticationService
@inject KeepAliveService KeepAliveService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject ModalService ModalService
@inject LANCommander.SDK.Client LANCommander
@inject IJSRuntime JS
@inject ILogger<MainLayout> Logger
@inject ImportManagerService ImportManagerService
<CustomWindow HeaderHeight="37">
<HeaderExtraControlsLayout>
<Space Direction="SpaceDirection.Horizontal">
<AuthenticatedView NoAutoValidate="true">
<Authenticated>
<SpaceItem>
@if (ImportStatus.IsActive)
{
<Popover Placement="Placement.BottomRight" IsButton Trigger="new[] { Trigger.Hover }">
<ChildContent>
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.Sync" Loading="true"/>
</ChildContent>
<ContentTemplate>
<ImportProgressDisplay
Index="@ImportStatus.Index"
Total="@ImportStatus.Total"
CurrentItem="@(_importProgress?.CurrentItem)"
/>
</ContentTemplate>
</Popover>
}
else if (ConnectionState.IsConnected)
{
<Tooltip Title="Re-import library" MouseEnterDelay="0.5">
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.Sync" OnClick="@ImportHandler.Import"/>
</Tooltip>
}
else if (!string.IsNullOrEmpty(Settings.Authentication.AccessToken))
{
<Tooltip Title="Reconnect to server" MouseEnterDelay="0.5">
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.CloudSync" OnClick="Connect" Loading="@Connecting" Danger/>
</Tooltip>
}
</SpaceItem>
<SpaceItem>
<ProfileButton/>
</SpaceItem>
</Authenticated>
<NotAuthenticated>
<SpaceItem>
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.CloudSync" OnClick="Connect" Loading="@Connecting" Danger/>
</SpaceItem>
</NotAuthenticated>
</AuthenticatedView>
</Space>
</HeaderExtraControlsLayout>
<WindowContent>
<ErrorHandler Title="Launcher Crashed">
<Body>
<AuthenticatedView NoAutoValidate="false">
<Authenticated>
@Body
@if (Settings.Debug.EnableScriptDebugging)
{
<PowerShellConsole/>
}
<KeepAliveContainer/>
</Authenticated>
<NotAuthenticated>
<AuthenticationForm/>
</NotAuthenticated>
</AuthenticatedView>
<ImportHandler @ref="ImportHandler" Progress="OnImportProcess" />
<UpdateChecker/>
<AntContainer/>
</Body>
<Extra>
<Button Type="ButtonType.Primary" OnClick="@(() => NavigationManager.NavigateTo("/", true))">View Library</Button>
</Extra>
</ErrorHandler>
</WindowContent>
</CustomWindow>
@code {
Models.Settings Settings = null;
public bool Importing;
public bool Connecting;
public ConnectionState ConnectionState = default!;
public IMessageService Messages { get; set; }
ImportHandler ImportHandler;
ImportHandler.ImportProgressEventArgs ImportStatus = new();
private ImportProgress _importProgress => ImportService.Progress;
string RandomQuip = "";
string[] CrashQuips = new[]
{
"You Died.",
"Snake? SNAAAAAAKE!",
"WASTED",
"Major fracture detected",
"The past is a gaping hole. You try to run from it, but the more you run, the deeper, the darker, the bigger it gets.",
"Your town center has been destroyed",
"Your forces are under attack!",
"You have lost the lead",
"Terrorists Win",
"War... War never changes.",
"You have died of dysentery",
"You have failed to restore the books. The Ages are lost.",
"Player was splattered by a demon",
"Sure, blame it on your ISP",
"Baba is no more",
"Guests are complaining they are lost",
"The darkness has overcome you",
"Subject: Gordon Freeman. Status: Terminated",
"Mission failed: You were spotted.",
"Critical damage! Eject, eject!",
"Your minions are unhappy. They are leaving.",
"The Empire has triumphed",
"Your quest has ended in failure",
"You have been eaten by a grue",
"You no mess with Lo Wang!",
"Sam was killed. Serious carnage ensues.",
"Damn, those alien bastards are gonna pay for shooting up my ride"
};
protected override async Task OnInitializedAsync()
{
Settings = SettingService.GetSettings();
ConnectionState = KeepAliveService.GetConnectionState();
Messages = MessageService;
AuthenticationService.OnOfflineModeChanged += OnOfflineModeChanged;
var token = new SDK.Models.AuthToken
{
AccessToken = Settings.Authentication.AccessToken,
RefreshToken = Settings.Authentication.RefreshToken
};
if (await LANCommander.ValidateTokenAsync(token))
{
ConnectionState.IsStartup = true;
await AuthenticationService.Login();
await ProfileService.DownloadProfileInfoAsync();
ConnectionState.IsConnected = true;
ConnectionState.OfflineModeEnabled = false;
ConnectionState.IsStartup = false;
await ImportManagerService.RequestImport();
await InvokeAsync(StateHasChanged);
}
var randIndex = new Random().Next(0, CrashQuips.Length - 1);
RandomQuip = CrashQuips[randIndex];
}
public void Dispose()
{
if (AuthenticationService != null)
{
AuthenticationService.OnOfflineModeChanged -= OnOfflineModeChanged;
}
}
async void OnOfflineModeChanged(bool state)
{
ConnectionState.OfflineModeEnabled = state;
ConnectionState.IsConnected = LANCommander.IsConnected();
await InvokeAsync(StateHasChanged);
}
async void OnImportProcess(ImportHandler.ImportProgressEventArgs importProgress)
{
ImportStatus = importProgress;
await InvokeAsync(StateHasChanged);
}
async Task CopyError(Exception ex)
{
await JS.InvokeVoidAsync("navigator.clipboard.writeText", ex.Message + "\n" + ex.StackTrace);
MessageService.Info("Error copied to clipboard!");
}
async Task Connect()
{
Connecting = true;
Settings = SettingService.GetSettings();
var token = new SDK.Models.AuthToken
{
AccessToken = Settings.Authentication.AccessToken,
RefreshToken = Settings.Authentication.RefreshToken
};
if (await LANCommander.ValidateTokenAsync(token))
{
await AuthenticationService.Login();
await ProfileService.DownloadProfileInfoAsync();
MessageService.Success("Back Online!");
ConnectionState.IsConnected = true;
ConnectionState.OfflineModeEnabled = false;
await InvokeAsync(StateHasChanged);
}
else
{
if (await LANCommander.PingAsync())
{
await Logout();
}
else
{
await ModalService.ConfirmAsync(new ConfirmOptions()
{
Title = "Could Not Reconnect!",
Icon = @<Icon Type="@IconType.Outline.ExclamationCircle"></Icon>,
Content = "The LANCommander server could not be reached. Click stay offline and try later, or logout and enter your credentials.",
OkText = "Logout",
CancelText = "Stay Offline",
Centered = true,
OnOk = async (e) =>
{
await Logout();
}
});
}
}
Connecting = false;
await InvokeAsync(StateHasChanged);
}
async Task Logout()
{
await AuthenticationService.Logout();
ConnectionState.IsConnected = false;
ConnectionState.OfflineModeEnabled = false;
NavigationManager.NavigateTo("/Authenticate");
}
}
<style>
.import-progress {
width: 300px; /* Fixed width */
}
.import-progress .downloader-current-upper-status {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
}
</style>