using Elternbeirat.Contracts; using Elternbeirat.PocketBase; using Elternbeirat.Web.Shared; using Microsoft.AspNetCore.Components; namespace Elternbeirat.Web.Features.Faqs; public partial class FaqList { [LoggerMessage( Level = LogLevel.Error, Message = "Could not load FAQs 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 Logger { get; set; } = null!; [CascadingParameter] private HttpContext? HttpContext { get; set; } private IReadOnlyList> _groups = []; // Set when the content source could not be reached or read. The markup then // shows a "temporarily unavailable" note instead of "no questions", matching // the 503 this sets on the response. private bool _unavailable; protected override async Task OnInitializedAsync() { try { var faqs = await PocketBase.GetFaqsAsync(HttpContext?.RequestAborted ?? CancellationToken.None); // GetFaqsAsync sorts by topic.order first, so grouping in encounter order // keeps the topic groups in the order an editor gave the topics. Entries // without an expanded topic have no group and are skipped rather than // grouped under a null heading. _groups = [.. faqs.Where(faq => faq.Topic is not null).GroupBy(faq => faq.Topic!)]; } catch (PocketBaseUnavailableException exception) { // An empty page would claim there are no questions. The site works, its // data store is down: 503 and an honest note. LogSourceUnavailable(Logger, exception); _unavailable = true; SourceUnavailable.SetStatus(HttpContext); } } }