feat: in-app help, notifications panel and global search (#32)

F1 and "?" now open the wiki page of the screen inside the application
(/aiuto, mockup 011) from a copy of the wiki shipped next to the program,
so help works offline and always matches the installed version. Markdown
is converted in-house and fully encoded; images of the repository are
served from the copy. KHAEMHAT_HELP_URL sets the online wiki behind
"Apri nel wiki"; "Segnala un problema" opens a new issue of that repo.
A test checks that every page and menu entry has an existing wiki page.

The bell opens a notifications panel (mockup 010) with all/unread
filters and mark read. Broadcast notifications are now read per user
through sys_notification_reads / notification_reads instead of one
shared read_at.

The global search asks ISearchProvider implementations through
GlobalSearchService after a short pause; the first provider finds help
pages and sections, # and @ say which modules will fill them.

Escape also closes a side panel when the focus fell out of it.
Decisions in ADR 0023.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 19:39:17 +01:00
1 parent acb7e598d1
commit f415dc37a8
39 files changed
+1976 -107

No files matched your search

@@ -217,10 +217,16 @@
}
}
private async Task OpenHelpAsync()
// F1 and the "?" open the help of the screen in a tab of its own (mockup 011); on a help page they stay there.
private Task OpenHelpAsync()
{
var path = TabService.PathOf(Navigation.ToBaseRelativePath(Navigation.Uri));
await Js.InvokeVoidAsync("khaemhat.openWindow", HelpLinks.UrlFor(HelpLinks.DefaultBaseUrl, path));
var path = TabService.PathOf("/" + Navigation.ToBaseRelativePath(Navigation.Uri));
if (!path.StartsWith(Khaemhat.Web.Help.WikiLibrary.BasePath, StringComparison.OrdinalIgnoreCase))
{
Navigation.NavigateTo(HelpLinks.LocalFor(path));
}
return Task.CompletedTask;
}
private async Task OnShortcutAsync(string command)
@@ -48,7 +48,7 @@ else
}
<div class="kh-actions">
<a class="btn" href="./">@T["Common.BackHome"]</a>
<a class="btn ghost" href="@(HelpLinks.DefaultBaseUrl + "Roadmap")" target="_blank" rel="noopener">@T["Pages.ComingSoon.Roadmap"]</a>
<a class="btn ghost" href="@Khaemhat.Web.Help.WikiLibrary.Href("Roadmap")">@T["Pages.ComingSoon.Roadmap"]</a>
</div>
</div>
}
@@ -0,0 +1,73 @@
@page "/aiuto"
@page "/aiuto/{Name}"
@attribute [Authorize]
@using Khaemhat.Application.Configuration
@using Khaemhat.Web.Help
@inject WikiLibrary Wiki
@inject KhaemhatOptions Options
@* The help of SPEC §17 (mockup 011): the wiki page of the screen, from the copy shipped with the application, with
the wiki index at the side, "Apri nel wiki" for the online version and "Segnala un problema". F1 and the "?" of the
top bar open it on the page of the current screen. *@
<KhPageTitle Text="@T.Format("Pages.Help.TabTitle", Current?.Title ?? T["Pages.Help.Title"])" Icon="help" />
<div class="phead">
<div>
<div class="crumbs">@T["Pages.Help.Crumb"]@if (Section is { } section){<text> › @section.Title</text>} › <b>@(Current?.Title ?? Name)</b></div>
<h1>@T.Format("Pages.Help.Heading", Current?.Title ?? T["Pages.Help.Title"])</h1>
</div>
<div class="grow"></div>
<a class="btn" href="@OnlineUrl" target="_blank" rel="noopener"><KhIcon Name="external" /> @T["Pages.Help.OpenWiki"]</a>
@if (IssuesUrl is { } issues)
{
<a class="btn" href="@issues" target="_blank" rel="noopener"><KhIcon Name="alert" /> @T["Pages.Help.Report"]</a>
}
</div>
<div class="kh-help">
<nav class="panel kh-help-index" aria-label="@T["Pages.Help.Index"]">
@foreach (var group in Wiki.Sidebar)
{
<div class="kh-help-group">@group.Title</div>
@foreach (var (page, label) in group.Pages)
{
<a class="kh-help-link @(string.Equals(page, PageName, StringComparison.OrdinalIgnoreCase) ? "on" : null)" href="@WikiLibrary.Href(page)"
aria-current="@(string.Equals(page, PageName, StringComparison.OrdinalIgnoreCase) ? "page" : null)">
<KhIcon Name="file" Class="sm" /> @label
</a>
}
}
</nav>
<article class="panel kh-help-body">
@if (!Wiki.IsAvailable)
{
<div class="callout warn"><KhIcon Name="alert" /> <span>@T["Pages.Help.Missing"]</span></div>
}
else if (Current is null)
{
<div class="callout warn"><KhIcon Name="alert" /> <span>@T.Format("Pages.Help.NotFound", Name ?? string.Empty)</span></div>
}
else
{
@((MarkupString)Current.Html)
}
</article>
</div>
@code {
/// <summary>The wiki page, such as <c>Interfaccia-e-scorciatoie</c>; empty for the wiki home.</summary>
[Parameter]
public string? Name { get; set; }
private string PageName => string.IsNullOrWhiteSpace(Name) ? "Home" : Name;
private WikiPage? Current => Wiki.Get(PageName);
private WikiSection? Section => Wiki.Sidebar.FirstOrDefault(s => s.Pages.Any(p => string.Equals(p.Page, PageName, StringComparison.OrdinalIgnoreCase)));
private string BaseUrl => HelpLinks.BaseUrl(Options.HelpUrl);
private string OnlineUrl => BaseUrl + (Current?.Name ?? PageName);
private string? IssuesUrl => HelpLinks.IssuesUrl(BaseUrl);
}
@@ -1,10 +1,14 @@
@inject ShellSession Session
@inject NavigationManager Navigation
@inject ThemeService Theme
@inject GlobalSearchService Search
@implements IDisposable
@using Khaemhat.Application.Search
@* The global search of SPEC §10.2 (Ctrl+K, mockup 009). Today it finds pages of the menu and commands (prefix >);
records (parties, items, documents with #, people with @) join as their modules arrive, through
GlobalSearchService. Arrows move, Enter opens, Escape closes. *@
@* The global search of SPEC §10.2 (Ctrl+K, mockup 009). Pages of the menu and commands (prefix >) are found at once;
records come from the providers of GlobalSearchService (today the help pages; parties, items, documents with #
and people with @ join as their modules arrive), a moment after the user stops typing. Arrows move, Enter opens,
Escape closes. *@
@if (Open)
{
<div class="overlay" @onclick="CloseAsync">
@@ -17,7 +21,11 @@
<span class="kbdhint"><kbd>@T["Shell.Keys.Escape"]</kbd></span>
</div>
<div id="kh-palette-hits" role="listbox" class="hits">
@if (Hits.Count == 0)
@if (Soon is { } soon)
{
<div class="grp">@T[soon]</div>
}
else if (Hits.Count == 0 && !_searching)
{
<div class="grp">@T["Shell.Search.NoResults"]</div>
}
@@ -38,6 +46,10 @@
@onclick="() => RunAsync(hit)" @onmouseover="() => _selected = i">
<KhIcon Name="@hit.Icon" />
<span>@hit.Title</span>
@if (hit.Detail is not null)
{
<span class="d">@hit.Detail</span>
}
@if (hit.Hint is not null)
{
<span class="k">@hit.Hint</span>
@@ -49,18 +61,24 @@
<span class="kbdhint"><kbd>↑</kbd><kbd>↓</kbd> @T["Shell.Search.Move"]</span>
<span class="kbdhint"><kbd>↵</kbd> @T["Shell.Search.Open"]</span>
<span class="kbdhint"><kbd>&gt;</kbd> @T["Shell.Search.Commands"]</span>
<span class="kbdhint"><kbd>#</kbd> @T["Shell.Search.Documents"]</span>
<span class="kbdhint"><kbd>@@</kbd> @T["Shell.Search.People"]</span>
</div>
</div>
</div>
}
@code {
private sealed record Hit(string Group, string Icon, string Title, string? Hint, Func<Task> Run);
private sealed record Hit(string Group, string Icon, string Title, string? Hint, Func<Task> Run, string? Detail = null);
private static readonly TimeSpan Pause = TimeSpan.FromMilliseconds(200);
private ElementReference _input;
private string _query = string.Empty;
private int _selected;
private bool _focus;
private bool _searching;
private CancellationTokenSource? _pending;
/// <summary>Whether the search is open.</summary>
[Parameter]
@@ -76,6 +94,14 @@
private List<Hit> Hits { get; set; } = [];
// # and @ before their modules: say when they will find something instead of "no results".
private string? Soon => SearchQuery.Parse(_query) switch
{
{ Scope: SearchScope.Documents } when !Search.Covers(SearchScope.Documents) => "Shell.Search.DocumentsSoon",
{ Scope: SearchScope.People } when !Search.Covers(SearchScope.People) => "Shell.Search.PeopleSoon",
_ => null,
};
/// <inheritdoc />
protected override void OnParametersSet()
{
@@ -84,10 +110,11 @@
_query = string.Empty;
_selected = 0;
_focus = true;
Hits = Search(_query);
Hits = Local(_query);
}
else if (!Open)
{
Cancel();
Hits = [];
}
}
@@ -102,13 +129,52 @@
}
}
private void OnInput(ChangeEventArgs e)
private async Task OnInput(ChangeEventArgs e)
{
_query = e.Value?.ToString() ?? string.Empty;
_selected = 0;
Hits = Search(_query);
Hits = Local(_query);
Cancel();
var query = SearchQuery.Parse(_query);
if (Session.Actor is not { } actor || query.Scope == SearchScope.Commands || query.Text.Length < SearchQuery.MinimumLength)
{
_searching = false;
return;
}
// Wait for a pause in the typing, then ask the providers; a newer input cancels this one.
var pending = _pending = new CancellationTokenSource();
_searching = true;
try
{
await Task.Delay(Pause, pending.Token);
var found = await Search.SearchAsync(actor, query, pending.Token);
if (pending.IsCancellationRequested || !Open)
{
return;
}
var local = Local(_query);
Hits = [.. (query.Scope == SearchScope.All ? local : []).Concat(found.Select(h => new Hit(h.Group, h.Icon, h.Title, null, () => Go(h.Href), h.Detail)))];
_selected = Math.Min(_selected, Math.Max(0, Hits.Count - 1));
_searching = false;
}
catch (OperationCanceledException)
{
// The user typed something else.
}
}
private void Cancel()
{
_pending?.Cancel();
_pending?.Dispose();
_pending = null;
}
/// <inheritdoc />
public void Dispose() => Cancel();
private async Task OnKeyDownAsync(KeyboardEventArgs e)
{
switch (e.Key)
@@ -136,21 +202,23 @@
private Task CloseAsync()
{
Cancel();
_searching = false;
Hits = [];
return OpenChanged.InvokeAsync(false);
}
private List<Hit> Search(string query)
private List<Hit> Local(string input)
{
var text = query.Trim();
var commandsOnly = text.StartsWith('>');
if (commandsOnly)
var query = SearchQuery.Parse(input);
if (query.Scope is SearchScope.Documents or SearchScope.People)
{
text = text[1..].Trim();
return [];
}
var text = query.Text;
var hits = new List<Hit>();
if (!commandsOnly)
if (query.Scope == SearchScope.All)
{
foreach (var item in MenuCatalog.All.Where(Session.CanSee))
{
@@ -0,0 +1,167 @@
@using Khaemhat.Application.Notifications
@using Khaemhat.Application.Preferences
@using Khaemhat.Core.Instance
@inject ShellSession Session
@inject NotificationDispatcher Notifications
@inject NavigationManager Navigation
@inject TimeProvider Clock
@* The notifications of SPEC §9 (mockup 010): a side panel from the bell with all or only the unread ones, newest
first. A click marks one as read and opens its page; "Segna tutte come lette" clears the bell. A notification for
everyone is read by each user for themselves. *@
<KhDrawer Open="Open" Title="@T["Shell.Notifications.Title"]" Icon="bell" OnClose="CloseAsync">
<ChildContent>
<div class="kh-seg kh-notif-filter" role="group" aria-label="@T["Shell.Notifications.Filter"]">
<button type="button" class="@(_unreadOnly ? null : "on")" aria-pressed="@(_unreadOnly ? "false" : "true")" @onclick="() => _unreadOnly = false">
@T.Format("Shell.Notifications.All", _items.Count)
</button>
<button type="button" class="@(_unreadOnly ? "on" : null)" aria-pressed="@(_unreadOnly ? "true" : "false")" @onclick="() => _unreadOnly = true">
@T.Format("Shell.Notifications.Unread", Unread)
</button>
</div>
@if (!_loaded)
{
<p class="muted">@T["Common.Loading"]</p>
}
else if (!Shown.Any())
{
<p class="kh-wempty"><KhIcon Name="check-circle" /> @T[_unreadOnly ? "Shell.Notifications.NoneUnread" : "Shell.Notifications.None"]</p>
}
else
{
<ul class="kh-notifs">
@foreach (var item in Shown)
{
<li class="kh-notif @(item.Read ? null : "unread")" @key="item.Id">
<span class="ico-box @ToneOf(item.Severity)" aria-hidden="true"><KhIcon Name="@IconOf(item.Severity)" /></span>
<button type="button" class="kh-notif-main" @onclick="() => OpenAsync(item)">
<span class="t">@item.Title</span>
@if (item.Body is not null)
{
<span class="s">@item.Body</span>
}
<span class="m">
<span class="badge gray">@CategoryOf(item.Category)</span>
<span>@When(item.CreatedAt)</span>
</span>
</button>
@if (!item.Read)
{
<button type="button" class="ib" title="@T["Shell.Notifications.MarkRead"]" aria-label="@T.Format("Shell.Notifications.MarkReadOf", item.Title)"
@onclick="() => MarkReadAsync(item)"><KhIcon Name="check" /></button>
}
</li>
}
</ul>
}
</ChildContent>
<Footer>
<button type="button" class="btn" disabled="@(Unread == 0)" @onclick="MarkAllAsync"><KhIcon Name="check" /> @T["Shell.Notifications.MarkAll"]</button>
</Footer>
</KhDrawer>
@code {
private IReadOnlyList<InboxItem> _items = [];
private bool _unreadOnly;
private bool _loaded;
private bool _wasOpen;
/// <summary>Whether the panel is shown.</summary>
[Parameter]
public bool Open { get; set; }
/// <summary>Opens or closes it.</summary>
[Parameter]
public EventCallback<bool> OpenChanged { get; set; }
/// <summary>Told the unread count after every change.</summary>
[Parameter]
public EventCallback<int> UnreadChanged { get; set; }
private int Unread => _items.Count(i => !i.Read);
private IEnumerable<InboxItem> Shown => _unreadOnly ? _items.Where(i => !i.Read) : _items;
/// <inheritdoc />
protected override async Task OnParametersSetAsync()
{
if (Open && !_wasOpen)
{
_loaded = false;
await LoadAsync();
}
_wasOpen = Open;
}
private async Task LoadAsync()
{
_items = Session.Actor is { } actor ? await Notifications.InboxAsync(actor) : [];
_loaded = true;
await UnreadChanged.InvokeAsync(Unread);
}
private Task CloseAsync() => OpenChanged.InvokeAsync(false);
private async Task MarkReadAsync(InboxItem item)
{
if (Session.Actor is { } actor && !item.Read)
{
await Notifications.MarkReadAsync(actor, item.Id);
await LoadAsync();
}
}
private async Task OpenAsync(InboxItem item)
{
await MarkReadAsync(item);
if (UserPreferences.IsLocalPath(item.Link))
{
await CloseAsync();
Navigation.NavigateTo(item.Link!.TrimStart('/'));
}
}
private async Task MarkAllAsync()
{
if (Session.Actor is { } actor)
{
await Notifications.MarkAllReadAsync(actor);
await LoadAsync();
}
}
private string CategoryOf(string category) =>
T.Contains("Shell.Notifications.Categories." + category) ? T["Shell.Notifications.Categories." + category] : category;
private string When(DateTime createdAt)
{
var at = new DateTimeOffset(DateTime.SpecifyKind(createdAt, DateTimeKind.Utc)).ToOffset(Clock.GetLocalNow().Offset);
var now = Clock.GetLocalNow();
var minutes = (int)(now - at).TotalMinutes;
return minutes switch
{
< 1 => T["Shell.Notifications.Now"],
< 60 => T.Format("Shell.Notifications.MinutesAgo", minutes),
_ when at.Date == now.Date => at.ToString("HH:mm", T.Culture),
_ when at.Date == now.Date.AddDays(-1) => T.Format("Shell.Notifications.Yesterday", at.ToString("HH:mm", T.Culture)),
_ => at.ToString("d MMM HH:mm", T.Culture),
};
}
private static string IconOf(NotificationSeverity severity) => severity switch
{
NotificationSeverity.Error => "x-circle",
NotificationSeverity.Warning => "alert",
NotificationSeverity.Success => "check-circle",
_ => "info",
};
private static string? ToneOf(NotificationSeverity severity) => severity switch
{
NotificationSeverity.Error => "err",
NotificationSeverity.Warning => "warn",
NotificationSeverity.Success => "ok",
_ => null,
};
}
+40 -49
View File
@@ -54,34 +54,15 @@
}
</div>
<div class="pop">
<button type="button" class="tbtn" aria-haspopup="menu" aria-expanded="@(_open == Popup.Notifications)"
aria-label="@T.Format("Shell.TopBar.NotificationsCount", _unread.Count)" @onclick="ToggleNotificationsAsync">
<KhIcon Name="bell" />
@if (_unread.Count > 0)
{
<span class="dot"></span>
}
<span class="tip">@T["Shell.TopBar.Notifications"]</span>
</button>
@if (_open == Popup.Notifications)
<button type="button" class="tbtn" aria-haspopup="dialog" aria-expanded="@(_notifications ? "true" : "false")"
aria-label="@T.Format("Shell.TopBar.NotificationsCount", _unread)" @onclick="ToggleNotifications">
<KhIcon Name="bell" />
@if (_unread > 0)
{
<div class="popmenu notif" role="menu">
<div class="fh">@T["Shell.TopBar.Notifications"]</div>
@if (_unread.Count == 0)
{
<div class="mi muted">@T["Shell.TopBar.NoNotifications"]</div>
}
@foreach (var n in _unread)
{
<a class="mi" role="menuitem" href="@(n.Link is { } link && UserPreferences.IsLocalPath(link) ? link.TrimStart('/') : "#")" @onclick="Close">
<KhIcon Name="@SeverityIcon(n.Severity)" />
<span class="grow"><b>@n.Title</b>@if (n.Body is not null) {<br /><span class="muted small">@n.Body</span>}</span>
</a>
}
</div>
<span class="dot"></span>
}
</div>
<span class="tip">@T["Shell.TopBar.Notifications"]</span>
</button>
<button type="button" class="tbtn" aria-label="@ThemeLabel" @onclick="Theme.ToggleDarkAsync">
<KhIcon Name="@(Session.Preferences.Theme == ThemeMode.Dark ? "sun" : "moon")" /><span class="tip">@ThemeLabel</span>
@@ -98,18 +79,20 @@
{
<div class="scrim" @onclick="Close"></div>
}
<NotificationPanel Open="_notifications" OpenChanged="open => _notifications = open" UnreadChanged="count => _unread = count" />
@code {
private enum Popup
{
Company,
Recent,
Notifications,
User,
}
private Popup? _open;
private IReadOnlyList<(string Title, string? Body, string? Link, Khaemhat.Core.Instance.NotificationSeverity Severity)> _unread = [];
private readonly CancellationTokenSource _stop = new();
private int _unread;
private bool _notifications;
/// <summary>Opens the global search.</summary>
[Parameter]
@@ -126,7 +109,8 @@
{
Session.Changed += OnChanged;
Tabs.Changed += OnChanged;
await LoadNotificationsAsync();
await CountUnreadAsync();
_ = RefreshAsync(_stop.Token);
}
/// <inheritdoc />
@@ -134,6 +118,8 @@
{
Session.Changed -= OnChanged;
Tabs.Changed -= OnChanged;
_stop.Cancel();
_stop.Dispose();
}
private void OnChanged() => _ = InvokeAsync(StateHasChanged);
@@ -142,39 +128,44 @@
private void Close() => _open = null;
private async Task ToggleNotificationsAsync()
private void ToggleNotifications()
{
Toggle(Popup.Notifications);
if (_open == Popup.Notifications)
_open = null;
_notifications = !_notifications;
}
private async Task CountUnreadAsync()
{
if (Session.Actor is { } actor)
{
await LoadNotificationsAsync();
_unread = (await Notifications.InboxAsync(actor, unreadOnly: true)).Count;
}
}
private async Task LoadNotificationsAsync()
// The bell lights up for notifications that arrive while the page is open: a look every minute.
private async Task RefreshAsync(CancellationToken cancellationToken)
{
if (Session.Actor is not { } actor)
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(1));
try
{
return;
while (await timer.WaitForNextTickAsync(cancellationToken))
{
await InvokeAsync(async () =>
{
await CountUnreadAsync();
StateHasChanged();
});
}
}
if (actor.SystemUserId is { } systemUser)
catch (OperationCanceledException)
{
_unread = [.. (await Notifications.UnreadForInstanceAsync(systemUser)).Select(n => (n.Title, n.Body, n.Link, n.Severity))];
// The page closed.
}
else if (actor is { CompanyCode: { } company, CompanyUserId: { } user })
catch (ObjectDisposedException)
{
_unread = [.. (await Notifications.UnreadForUserAsync(company, user)).Select(n => (n.Title, n.Body, n.Link, n.Severity))];
// The circuit went away.
}
}
private static string Relative(string url) => url.TrimStart('/');
private static string SeverityIcon(Khaemhat.Core.Instance.NotificationSeverity severity) => severity switch
{
Khaemhat.Core.Instance.NotificationSeverity.Error => "x-circle",
Khaemhat.Core.Instance.NotificationSeverity.Warning => "alert",
Khaemhat.Core.Instance.NotificationSeverity.Success => "check-circle",
_ => "info",
};
}