Files
gpw_next/GPW.CORE.Shared/Components/Layout/PageModeIndicator.razor
T

91 lines
2.5 KiB
Plaintext

@using GPW.CORE.Services
@using Microsoft.JSInterop
@implements IDisposable
@inject NavigationManager Nav
@inject RouteModeService RouteModeSvc
@inject IJSRuntime JS
<span class="page-mode-indicator" title="@Title">
@if (displayMode == RouteMode.ServerOnly)
{
<span class="text-danger" title="Server">🖥️</span>
}
else if (displayMode == RouteMode.ClientOnly)
{
@if (!isClientActive)
{
<span class="text-warning" title="Client (prerender)">🟡</span>
}
else
{
<span class="text-success" title="WASM">🟢</span>
}
}
else
{
<span class="text-info" title="Hybrid">🔀</span>
}
</span>
@code {
private RouteMode displayMode = RouteMode.Hybrid;
private bool isClientActive;
private string? Title;
private IDisposable? _subscription;
protected override void OnInitialized()
{
Nav.LocationChanged += OnLocationChanged;
// Subscribe passing a callback that marshals to the component dispatcher
_subscription = RouteModeSvc.Subscribe(() => InvokeAsync(StateHasChanged));
UpdateForCurrentUri();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender || true)
{
// JS check: se fallisce siamo in prerender
try
{
await JS.InvokeVoidAsync("console.debug", "client active check");
isClientActive = true;
}
catch
{
isClientActive = false;
}
await InvokeAsync(StateHasChanged);
}
}
private void OnLocationChanged(object? s, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e)
{
UpdateForCurrentUri();
// StateHasChanged will be triggered by the subscription callback when SetMode is called,
// but we also want to update immediately for location changes:
_ = InvokeAsync(StateHasChanged);
}
private void UpdateForCurrentUri()
{
var uri = Nav.ToBaseRelativePath(Nav.Uri).Split('?')[0];
var mode = RouteModeSvc.GetModeFor(uri);
displayMode = mode ?? RouteMode.Hybrid;
Title = displayMode switch
{
RouteMode.ServerOnly => "Pagina Server Only",
RouteMode.ClientOnly => "Pagina Client Only",
_ => "Pagina Hybrid"
};
}
public void Dispose()
{
Nav.LocationChanged -= OnLocationChanged;
_subscription?.Dispose();
}
}