| | | 1 | | namespace Chronicis.Client.Services; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Pure discriminated union returned by <see cref="AutocompleteCommitDecision.Decide"/>. |
| | | 5 | | /// Hosts pattern-match on this to choose the correct commit path. |
| | | 6 | | /// </summary> |
| | | 7 | | public abstract record AutocompleteCommitDecision |
| | | 8 | | { |
| | | 9 | | /// <summary>Select the existing suggestion at <see cref="Index"/>.</summary> |
| | 4 | 10 | | public sealed record SelectExisting(int Index) : AutocompleteCommitDecision; |
| | | 11 | | |
| | | 12 | | /// <summary>Create a new article with the resolved <see cref="Name"/>.</summary> |
| | 7 | 13 | | public sealed record CreateNew(string Name) : AutocompleteCommitDecision; |
| | | 14 | | |
| | | 15 | | /// <summary>No action — guard conditions were not met.</summary> |
| | 10 | 16 | | public sealed record DoNothing() : AutocompleteCommitDecision; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Determines what the host should do when Enter is pressed in the autocomplete. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="query">Current autocomplete query (internal remainder for external queries).</param> |
| | | 22 | | /// <param name="suggestionsCount">Number of suggestions currently visible.</param> |
| | | 23 | | /// <param name="selectedIndex">Currently highlighted suggestion index.</param> |
| | | 24 | | /// <param name="isExternalQuery">True when the query targets an external provider (e.g. srd/).</param> |
| | | 25 | | /// <param name="isMapQuery">True when the query targets the maps namespace.</param> |
| | | 26 | | public static AutocompleteCommitDecision Decide( |
| | | 27 | | string query, |
| | | 28 | | int suggestionsCount, |
| | | 29 | | int selectedIndex, |
| | | 30 | | bool isExternalQuery, |
| | | 31 | | bool isMapQuery) |
| | | 32 | | { |
| | 19 | 33 | | if (suggestionsCount > 0 && selectedIndex >= 0 && selectedIndex < suggestionsCount) |
| | 3 | 34 | | return new SelectExisting(selectedIndex); |
| | | 35 | | |
| | 16 | 36 | | if (isExternalQuery || isMapQuery) |
| | 2 | 37 | | return new DoNothing(); |
| | | 38 | | |
| | 14 | 39 | | var trimmed = (query ?? string.Empty).Trim(); |
| | 14 | 40 | | if (trimmed.Length < 3) |
| | 6 | 41 | | return new DoNothing(); |
| | | 42 | | |
| | 8 | 43 | | var name = ExtractArticleName(trimmed); |
| | 8 | 44 | | return string.IsNullOrEmpty(name) ? new DoNothing() : new CreateNew(name); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Extracts a display name from the query, taking the last path segment and |
| | | 49 | | /// capitalising the first letter — parity with ArticleDetailWikiLinkAutocomplete.GetArticleName. |
| | | 50 | | /// </summary> |
| | | 51 | | private static string ExtractArticleName(string query) |
| | | 52 | | { |
| | 8 | 53 | | var segments = query.Split('/'); |
| | 8 | 54 | | var name = segments[^1].Trim(); |
| | 8 | 55 | | if (string.IsNullOrEmpty(name)) |
| | 1 | 56 | | return string.Empty; |
| | 7 | 57 | | return char.ToUpper(name[0]) + name[1..]; |
| | | 58 | | } |
| | | 59 | | } |