Files

51 lines
1.8 KiB
C#

using Elternbeirat.PocketBase;
using Elternbeirat.Web.Features.Layout;
using Microsoft.AspNetCore.Components;
namespace Elternbeirat.Web.Components.Layout;
public partial class MainLayout
{
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Could not load navigation pages from PocketBase; the menu is empty.")]
private static partial void LogSourceUnavailable(ILogger logger, Exception exception);
[Inject]
private PocketBaseClient PocketBase { get; set; } = null!;
[Inject]
private ILogger<MainLayout> Logger { get; set; } = null!;
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private IReadOnlyList<NavLinkItem> _header = [];
private IReadOnlyList<NavLinkItem> _footer = [];
// The skip link's target on this very page: path and query of the request plus
// the fragment. See the comment on the skip link in the markup for why it cannot
// be a bare "#main".
private string SkipLinkHref =>
$"{HttpContext?.Request.PathBase}{HttpContext?.Request.Path}{HttpContext?.Request.QueryString}#main";
// Berlin's year, not the server's (UTC) -- see FooterYear.
private readonly int _year = FooterYear.For(TimeProvider.System.GetUtcNow());
protected override async Task OnInitializedAsync()
{
try
{
var pages = await PocketBase.GetPagesAsync(HttpContext?.RequestAborted ?? default);
_header = NavBuilder.Header(pages);
_footer = NavBuilder.Footer(pages);
}
catch (PocketBaseUnavailableException ex)
{
// Only the menu fails: render the chrome with an empty menu instead of a
// 500, so every page stays reachable. The brand link still leads home.
LogSourceUnavailable(Logger, ex);
}
}
}