@inject ShellSession Session @inject NavigationManager Navigation @inject ThemeService Theme @* 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. *@ @if (Open) {
} @code { private sealed record Hit(string Group, string Icon, string Title, string? Hint, Func Run); private ElementReference _input; private string _query = string.Empty; private int _selected; private bool _focus; /// Whether the search is open. [Parameter] public bool Open { get; set; } /// Opens or closes it. [Parameter] public EventCallback OpenChanged { get; set; } /// Collapses or expands the menu (command). [Parameter] public EventCallback OnToggleMenu { get; set; } private List Hits { get; set; } = []; /// protected override void OnParametersSet() { if (Open && !_focus && Hits.Count == 0) { _query = string.Empty; _selected = 0; _focus = true; Hits = Search(_query); } else if (!Open) { Hits = []; } } /// protected override async Task OnAfterRenderAsync(bool firstRender) { if (_focus && Open) { _focus = false; await _input.FocusAsync(); } } private void OnInput(ChangeEventArgs e) { _query = e.Value?.ToString() ?? string.Empty; _selected = 0; Hits = Search(_query); } private async Task OnKeyDownAsync(KeyboardEventArgs e) { switch (e.Key) { case "ArrowDown": _selected = Hits.Count == 0 ? 0 : (_selected + 1) % Hits.Count; break; case "ArrowUp": _selected = Hits.Count == 0 ? 0 : (_selected - 1 + Hits.Count) % Hits.Count; break; case "Enter" when Hits.Count > 0: await RunAsync(Hits[Math.Min(_selected, Hits.Count - 1)]); break; case "Escape": await CloseAsync(); break; } } private async Task RunAsync(Hit hit) { await CloseAsync(); await hit.Run(); } private Task CloseAsync() { Hits = []; return OpenChanged.InvokeAsync(false); } private List Search(string query) { var text = query.Trim(); var commandsOnly = text.StartsWith('>'); if (commandsOnly) { text = text[1..].Trim(); } var hits = new List(); if (!commandsOnly) { foreach (var item in MenuCatalog.All.Where(Session.CanSee)) { var title = T[item.Key]; var area = MenuCatalog.FindArea(item.Href); var label = area is not null && area != item ? T[area.Key] + " · " + title : title; if (Matches(label, text)) { hits.Add(new Hit("Shell.Search.Pages", item.Icon, label, null, () => Go(item.Href))); } } } foreach (var command in Commands()) { if (Matches(command.Title, text)) { hits.Add(command); } } return [.. hits.Take(30)]; } private IEnumerable Commands() { yield return new Hit("Shell.Search.CommandsGroup", "moon", T["Shell.Commands.ToggleTheme"], null, () => Theme.ToggleDarkAsync()); yield return new Hit("Shell.Search.CommandsGroup", "sidebar", T["Shell.Commands.ToggleMenu"], KeyboardShortcuts.KeysOf(KeyboardShortcuts.Commands.ToggleMenu), () => OnToggleMenu.InvokeAsync()); yield return new Hit("Shell.Search.CommandsGroup", "palette", T["Shell.Commands.Appearance"], null, () => Go("/profilo?scheda=aspetto")); yield return new Hit("Shell.Search.CommandsGroup", "user", T["Shell.Commands.Profile"], null, () => Go("/profilo")); yield return new Hit("Shell.Search.CommandsGroup", "monitor", T["Shell.User.Sessions"], null, () => Go("/profilo?scheda=sessioni")); yield return new Hit("Shell.Search.CommandsGroup", "keyboard", T["Shell.Commands.Shortcuts"], null, () => Go("/profilo?scheda=scorciatoie")); } private Task Go(string href) { Navigation.NavigateTo(href.TrimStart('/')); return Task.CompletedTask; } private static bool Matches(string title, string text) => text.Length == 0 || text.Split(' ', StringSplitOptions.RemoveEmptyEntries).All(word => title.Contains(word, StringComparison.CurrentCultureIgnoreCase)); }