LANCommander/LANCommander.Launcher/UI/MainLayout.razor
2025-01-15 00:32:42 -06:00

230 lines
No EOL
7.5 KiB
Text

@using LANCommander.Launcher.Models
@using Photino.Blazor.CustomWindow.Components
@inherits LayoutComponentBase
@inject ImportService ImportService
@inject ProfileService ProfileService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject ModalService ModalService
@inject LANCommander.SDK.Client LANCommander
@inject IJSRuntime JS
<CustomWindow HeaderHeight="37">
<HeaderExtraControlsLayout>
<Space Direction="SpaceDirection.Horizontal">
@if (Settings != null && Settings.Profile != null && ProfileService.IsAuthenticated())
{
@if (!Settings.Authentication.OfflineMode)
{
<SpaceItem>
<Popover Placement="Placement.BottomRight" IsButton OnClick="Import" Trigger="new [] { Trigger.Hover }">
<ChildContent>
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.Sync" Loading="@Importing"/>
</ChildContent>
<ContentTemplate>
<Progress Percent="@((ImportStatusIndex / (float)ImportStatusTotal) * 100)" Steps="@ImportStatusTotal" />
</ContentTemplate>
</Popover>
</SpaceItem>
}
else
{
<SpaceItem>
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.CloudSync" OnClick="Connect" Loading="@Connecting" Danger />
</SpaceItem>
}
<SpaceItem>
<ProfileButton />
</SpaceItem>
}
</Space>
</HeaderExtraControlsLayout>
<WindowContent>
<ErrorBoundary>
<ChildContent>
@Body
@if (Settings.Debug.EnableScriptDebugging)
{
<PowerShellConsole/>
}
<UpdateChecker/>
</ChildContent>
<ErrorContent>
<Result Status="ResultStatus.Error"
Title="Launcher Crashed"
SubTitle="@RandomQuip"
Class="crash-error">
<Extra>
<Button Type="ButtonType.Primary" OnClick="@(() => NavigationManager.NavigateTo("/", true))">View Library</Button>
<Button OnClick="() => CopyError(context)">Copy Error</Button>
</Extra>
<ChildContent>
<code>
@context.StackTrace
</code>
</ChildContent>
</Result>
</ErrorContent>
</ErrorBoundary>
<AntContainer/>
<RedirectToLogin/>
</WindowContent>
</CustomWindow>
@code {
Models.Settings Settings = null;
public bool Importing = false;
public bool Connecting = false;
public IMessageService Messages { get; set; }
public static MainLayout _MainLayout { get; set; }
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"
};
int ImportStatusIndex = 0;
int ImportStatusTotal = 0;
protected override async Task OnInitializedAsync()
{
_MainLayout = this;
Settings = SettingService.GetSettings();
Messages = MessageService;
ImportService.OnImportComplete += OnImportComplete;
ImportService.OnImportUpdated += OnImportUpdated;
var randIndex = new Random().Next(0, CrashQuips.Length - 1);
RandomQuip = CrashQuips[randIndex];
if (!(await LANCommander.ValidateTokenAsync()) && !Settings.Authentication.OfflineMode)
NavigationManager.NavigateTo("/Authenticate");
}
async Task OnImportComplete()
{
MessageService.Success("Import Complete", 3);
}
async Task OnImportUpdated(ImportStatusUpdate status)
{
MessageService.Info($"Importing {status.CurrentItem.Name}");
}
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))
{
ProfileService.SetOfflineMode(false);
MessageService.Success("Back Online!");
}
else
{
if (await LANCommander.PingAsync())
{
ProfileService.SetOfflineMode(false);
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 fix your credentials.",
OkText = "Logout",
CancelText = "Stay Offline",
Centered = true,
OnOk = async (e) =>
{
await Logout();
}
});
}
}
Connecting = false;
await InvokeAsync(StateHasChanged);
}
async Task Logout()
{
await ProfileService.Logout();
NavigationManager.NavigateTo("/Authenticate");
}
public static async void Import()
{
if (!_MainLayout.Importing)
{
await _MainLayout.JS.InvokeVoidAsync("window.external.sendMessage", "import");
_MainLayout.Importing = true;
_MainLayout.StateHasChanged();
_MainLayout.MessageService.Info("Import Started", 2.5);
}
}
[JSInvokable("ImportComplete")]
public static void ImportComplete()
{
_MainLayout.Importing = false;
_MainLayout.StateHasChanged();
_MainLayout.ImportService.ImportHasCompleted();
}
}