61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using Elternbeirat.Contracts;
|
|
using Elternbeirat.PocketBase;
|
|
using Elternbeirat.Web.Shared;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace Elternbeirat.Web.Features.Posts;
|
|
|
|
public partial class PostList
|
|
{
|
|
[LoggerMessage(
|
|
Level = LogLevel.Error,
|
|
Message = "Could not load posts from PocketBase; content source is unavailable.")]
|
|
private static partial void LogSourceUnavailable(
|
|
ILogger logger,
|
|
Exception exception);
|
|
|
|
[Inject]
|
|
private PocketBaseClient PocketBase { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private ILogger<PostList> Logger { get; set; } = null!;
|
|
|
|
[CascadingParameter]
|
|
private HttpContext? HttpContext { get; set; }
|
|
|
|
// The "posts" page record supplies the heading and intro text, so an editor owns
|
|
// them in PocketBase like every other page, instead of them being hard-coded here.
|
|
// Null until loaded, or when no such page exists.
|
|
private Page? _page;
|
|
|
|
private IReadOnlyList<Post> _posts = [];
|
|
|
|
// Set when the content source could not be reached or read. The markup then
|
|
// shows a "temporarily unavailable" note instead of "no posts", matching the
|
|
// 503 this sets on the response.
|
|
private bool _unavailable;
|
|
|
|
// The page body rendered from Markdown to HTML, ready to emit as a MarkupString.
|
|
private MarkupString Body => (MarkupString)Markdown.ToHtml(_page?.Body);
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
var token = HttpContext?.RequestAborted ?? CancellationToken.None;
|
|
var pages = await PocketBase.GetPagesAsync(token);
|
|
_page = pages.FirstOrDefault(
|
|
page => string.Equals(page.Slug, "posts", StringComparison.OrdinalIgnoreCase));
|
|
_posts = await PocketBase.GetPostsAsync(token);
|
|
}
|
|
catch (PocketBaseUnavailableException exception)
|
|
{
|
|
// An empty list would claim there are no posts. The site works, its
|
|
// data store is down: 503 and an honest note.
|
|
LogSourceUnavailable(Logger, exception);
|
|
_unavailable = true;
|
|
SourceUnavailable.SetStatus(HttpContext);
|
|
}
|
|
}
|
|
}
|