LANCommander/LANCommander.Launcher/UI/Windows/MainWindow.razor
2026-01-16 22:43:10 -06:00

196 lines
No EOL
7.1 KiB
Text

@using LANCommander.Launcher.Models
@using Photino.Blazor.CustomWindow.Components
@namespace LANCommander.Launcher.UI
@inherits LayoutComponentBase
@inject ImportService ImportService
@inject ProfileService ProfileService
@inject AuthenticationService AuthenticationService
@inject AuthenticationClient AuthenticationClient
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject ModalService ModalService
@inject IJSRuntime JS
@inject ImportManagerService ImportManagerService
@inject LocalizationService LocalizationService
@inject IConnectionClient ConnectionClient
<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 (ConnectionClient.IsOfflineMode())
{
<Tooltip Title="@LocalizationService.GetString("ReconnectToServer")" MouseEnterDelay="0.5">
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.CloudSync" OnClick="Connect" Loading="@Connecting" Danger/>
</Tooltip>
}
else if (ConnectionClient.IsConnected())
{
<Tooltip Title="@LocalizationService.GetString("ReimportLibrary")" MouseEnterDelay="0.5">
<Button Type="@ButtonType.Text" Icon="@IconType.Outline.Sync" OnClick="@ImportHandler.Import"/>
</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="@LocalizationService.GetString("LauncherCrashed")">
<Body>
<Layout>
@Body
</Layout>
<div class="logo">
<img src="assets/logo-cut.svg" />
</div>
<LANCommander.Launcher.UI.Footer />
<PowerShellConsole />
<ImportHandler @ref="ImportHandler" Progress="OnImportProcess"/>
<UpdateChecker/>
<AntContainer/>
<KeepAliveContainer/>
</Body>
<Extra>
<Button Type="ButtonType.Primary" OnClick="@(() => NavigationManager.NavigateTo("/", true))">@LocalizationService.GetString("ViewLibrary")</Button>
</Extra>
</ErrorHandler>
</WindowContent>
</CustomWindow>
@code {
public bool Importing;
public bool Connecting;
public IMessageService Messages { get; set; }
ImportHandler ImportHandler;
ImportHandler.ImportProgressEventArgs ImportStatus = new();
private ImportProgress _importProgress => ImportService.Progress;
protected override async Task OnInitializedAsync()
{
ConnectionClient.OnOfflineModeEnabled += OnOfflineModeChanged;
ConnectionClient.OnConnect += OnConnect;
if (await AuthenticationClient.ValidateTokenAsync())
{
await AuthenticationService.Login();
await ImportManagerService.RequestImport();
await InvokeAsync(StateHasChanged);
}
}
private async void OnConnect(object? sender, EventArgs e)
{
await ImportManagerService.RequestImport();
}
public void Dispose()
{
if (ConnectionClient != null)
ConnectionClient.OnOfflineModeEnabled -= OnOfflineModeChanged;
}
async void OnOfflineModeChanged(object? sender, EventArgs eventArgs)
{
await InvokeAsync(StateHasChanged);
}
async void OnImportProcess(ImportHandler.ImportProgressEventArgs importProgress)
{
ImportStatus = importProgress;
await InvokeAsync(StateHasChanged);
}
async Task Connect()
{
Connecting = true;
if (await ConnectionClient.ConnectAsync())
{
await ProfileService.DownloadProfileInfoAsync();
MessageService.Success(LocalizationService.GetString("BackOnline"));
await InvokeAsync(StateHasChanged);
}
else
{
await ModalService.ConfirmAsync(new ConfirmOptions()
{
Title = LocalizationService.GetString("CouldNotReconnect"),
Icon = @<Icon Type="@IconType.Outline.ExclamationCircle"></Icon>,
Content = LocalizationService.GetString("CouldNotReconnectMessage"),
OkText = LocalizationService.GetString("Logout"),
CancelText = LocalizationService.GetString("StayOffline"),
Centered = true,
OnOk = async (e) =>
{
await Logout();
}
});
}
Connecting = false;
await InvokeAsync(StateHasChanged);
}
async Task Logout()
{
await ConnectionClient.DisconnectAsync();
await AuthenticationService.Logout();
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>