From e9716b415786951b3a62f780ee159b51ff72f9a1 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Thu, 20 Jun 2024 14:25:48 +1000 Subject: [PATCH] Finished the directory --- Components/Pages/Directory.razor | 62 ++++++++++++++++++++++++++------ Models/State.cs | 7 +++- wwwroot/css/style.css | 16 +++++++++ wwwroot/js/csharp.js | 11 ++++++ 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/Components/Pages/Directory.razor b/Components/Pages/Directory.razor index 6296be5..ea69c72 100644 --- a/Components/Pages/Directory.razor +++ b/Components/Pages/Directory.razor @@ -1,4 +1,6 @@ @page "/directory" +@using System.Text.RegularExpressions +@using System.Globalization @implements IDisposable @inject IJSRuntime JS @inject State State @@ -9,34 +11,74 @@ Welcome to the omg.lol member directory! Everyone here is awesome. -
- - @if (addresses != null) { - @* TODO: Display avatar and address name, grouped by starting letter? *@ - } - +@if (groupedAddresses != null) { + IdnMapping idn = new IdnMapping(); +
+ +
+
+ @foreach(var group in groupedAddresses) { +

— @group.Key —

+
    + @foreach(string address in group) { + string displayAddress = address; + string linkAddress = address; + if (group.Key == "😀") { + try { + linkAddress = idn.GetAscii(address); + displayAddress = $"{address} {linkAddress}"; + } + catch (Exception) { } + } +
  • + + + @displayAddress + +
  • + } +
+ } +
+} +else { -
- +} @code { private List? addresses; + private IOrderedEnumerable>? groupedAddresses; protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - if (addresses == null || addresses.Count == 0) addresses = await State.GetDirectory(); + if (addresses == null || addresses.Count == 0){ + addresses = await State.GetDirectory(); + GroupAddresses(); + } State.PropertyChanged += StateChanged; State.CanRefresh = true; await InvokeAsync(StateHasChanged); - await JS.InvokeVoidAsync("removeElementById", "address-loading"); } private async void StateChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { addresses = await State.GetDirectory(true); + GroupAddresses(); State.IsRefreshing = false; } } + private void GroupAddresses() { + groupedAddresses = addresses?.GroupBy(s => { + if (Regex.IsMatch(s, "^[0-9]", RegexOptions.IgnoreCase)) return "#"; + else if (Regex.IsMatch(s, "^[a-z]", RegexOptions.IgnoreCase)) return s.First().ToString().ToUpper(); + else return "😀"; + }).OrderBy(g => g.Key); + } + public void Dispose() { State.PropertyChanged -= StateChanged; State.CanRefresh = false; diff --git a/Models/State.cs b/Models/State.cs index 689776c..a94d992 100644 --- a/Models/State.cs +++ b/Models/State.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Components; using System.ComponentModel; +using System.Globalization; using System.Text.Json; namespace Neighbourhood.omg.lol.Models { @@ -146,7 +147,11 @@ namespace Neighbourhood.omg.lol.Models { public async Task?> GetDirectory(bool forceRefresh = false) { if (forceRefresh || this.AddressDirectory == null || this.AddressDirectory.Count == 0) { - this.AddressDirectory = await api.Directory(); + IdnMapping idn = new IdnMapping(); + this.AddressDirectory = (await api.Directory()).Select(s => { + if (s.StartsWith("xn--")) return idn.GetUnicode(s); + else return s; + }).ToList(); } return this.AddressDirectory; } diff --git a/wwwroot/css/style.css b/wwwroot/css/style.css index 33116a0..0dbba39 100644 --- a/wwwroot/css/style.css +++ b/wwwroot/css/style.css @@ -400,4 +400,20 @@ a.row.indent { :is(button,.button,.chip).large.small > .responsive { inline-size: 3rem; } +} + +#directory{ + column-width: 14rem; +} +#directory a { + font-size: 1.1rem; + text-decoration: none; +} + +article#directory ul{ + list-style:none; +} + +#directoryIndex a { + font-size: 1.5rem; } \ No newline at end of file diff --git a/wwwroot/js/csharp.js b/wwwroot/js/csharp.js index 544181e..2985c9f 100644 --- a/wwwroot/js/csharp.js +++ b/wwwroot/js/csharp.js @@ -11,4 +11,15 @@ async function delay(t) { async function removeElementById(id) { document.getElementById(id)?.remove() +} + +function scrollToId(id) { + const element = document.getElementById(id); + if (element instanceof HTMLElement) { + element.scrollIntoView({ + behavior: "smooth", + block: "start", + inline: "nearest" + }); + } } \ No newline at end of file