Centralize the home slug check on Page, case-insensitively

This commit is contained in:
tleininger committed 2026-09-24 10:23:30 +02:00
1 parent 66bc76af1c
commit d06af2891a
3 files changed
+36 -10

No files matched your search

+12 -8
View File
@@ -1,3 +1,4 @@
using Elternbeirat.Contracts;
using Elternbeirat.PocketBase;
using Microsoft.AspNetCore.Components;
@@ -5,14 +6,18 @@ namespace Elternbeirat.Web.Features.Home;
public partial class Home
{
[LoggerMessage(Level = LogLevel.Warning, Message = "Could not load the home page from PocketBase.")]
private static partial void LogLoadFailed(ILogger logger, Exception exception);
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Could not load the home page from PocketBase.")]
private static partial void LogLoadFailed(
ILogger logger,
Exception exception);
[Inject]
private PocketBaseClient PocketBase { get; set; } = default!;
private PocketBaseClient PocketBase { get; set; } = null!;
[Inject]
private ILogger<Home> Logger { get; set; } = default!;
private ILogger<Home> Logger { get; set; } = null!;
// Which dynamic blocks the home page shows is editorial: it comes from the
// "home" page's embed field in PocketBase, not from hard-coded markup.
@@ -23,14 +28,13 @@ public partial class Home
try
{
var pages = await PocketBase.GetPagesAsync();
var home = pages.FirstOrDefault(
p => string.Equals(p.Slug, "home", StringComparison.Ordinal));
var home = pages.FirstOrDefault(page => Page.IsHomeSlug(page.Slug));
_embed = home?.Embed ?? [];
}
catch (HttpRequestException ex)
catch (HttpRequestException exception)
{
// Source unreachable -> render the intro without embeds instead of a 500.
LogLoadFailed(Logger, ex);
LogLoadFailed(Logger, exception);
}
}
}
@@ -17,7 +17,9 @@ public static class NavBuilder
public static IReadOnlyList<NavLinkItem> Footer(IReadOnlyList<Page> pages) =>
Build(pages, "footer");
private static List<NavLinkItem> Build(IReadOnlyList<Page> pages, string location) =>
private static List<NavLinkItem> Build(
IReadOnlyList<Page> pages,
string location) =>
[.. pages
.Where(page => string.Equals(page.Location, location, StringComparison.Ordinal))
.OrderBy(page => page.Order)
@@ -27,5 +29,5 @@ public static class NavBuilder
// reached at "/{slug}", where either a literal route or the catch-all content
// page renders it.
private static string HrefFor(string slug) =>
string.Equals(slug, "home", StringComparison.Ordinal) ? "/" : $"/{slug}";
Page.IsHomeSlug(slug) ? "/" : $"/{slug}";
}