From 0b8e3a8f2b75bcf0a841a4bc0f474613962c1cfd Mon Sep 17 00:00:00 2001 From: pancakes Date: Fri, 28 Mar 2025 16:22:23 +1000 Subject: [PATCH] [frontend/pages] Add user debug page --- .../Components/JsonViewer.razor | 62 ++++++++++++++++ .../Components/JsonViewer.razor.css | 16 +++++ Iceshrimp.Frontend/Pages/About.razor | 72 +++++++++++++++++++ Iceshrimp.Frontend/Pages/About.razor.css | 14 ++++ 4 files changed, 164 insertions(+) create mode 100644 Iceshrimp.Frontend/Components/JsonViewer.razor create mode 100644 Iceshrimp.Frontend/Components/JsonViewer.razor.css create mode 100644 Iceshrimp.Frontend/Pages/About.razor create mode 100644 Iceshrimp.Frontend/Pages/About.razor.css diff --git a/Iceshrimp.Frontend/Components/JsonViewer.razor b/Iceshrimp.Frontend/Components/JsonViewer.razor new file mode 100644 index 00000000..20b47a2c --- /dev/null +++ b/Iceshrimp.Frontend/Components/JsonViewer.razor @@ -0,0 +1,62 @@ +@using System.Text.Json + +
+ @if (Element.ValueKind == JsonValueKind.Array) + { + @if (Element.GetArrayLength() == 0) + { + Empty + } + else + { + var i = 0; + @foreach (var item in Element.EnumerateArray()) + { +
@i:
+ @RenderElement(item) + + i++; + } + } + } + else if (Element.ValueKind == JsonValueKind.Object) + { + @foreach (var prop in Element.EnumerateObject()) + { + @switch (prop.Value.ValueKind) + { + case JsonValueKind.Array: +
@prop.Name []
+ break; + case JsonValueKind.Object: +
@prop.Name {}
+ break; + default: +
@prop.Name:
+ break; + } + @RenderElement(prop.Value) + } + } +
+ +@code { + [Parameter, EditorRequired] public required JsonElement Element { get; set; } + + RenderFragment RenderElement(JsonElement element) + { + return element.ValueKind switch + { + JsonValueKind.True or JsonValueKind.False or JsonValueKind.Null or JsonValueKind.Undefined => (@
+ @element.ValueKind.ToString()
), + JsonValueKind.Array or JsonValueKind.Object => (@
+ +
), + JsonValueKind.Number => (@
@element.GetDouble()
), + JsonValueKind.String => (element.GetString() is { } s && Uri.TryCreate(s, UriKind.Absolute, out var _) + ? @
@s
+ : @
"@element.GetString()?.ReplaceLineEndings("\\n")"
), + _ => throw new ArgumentOutOfRangeException() + }; + } +} \ No newline at end of file diff --git a/Iceshrimp.Frontend/Components/JsonViewer.razor.css b/Iceshrimp.Frontend/Components/JsonViewer.razor.css new file mode 100644 index 00000000..d1f15593 --- /dev/null +++ b/Iceshrimp.Frontend/Components/JsonViewer.razor.css @@ -0,0 +1,16 @@ +.json { + dt { + color: #66C3CC; + font-weight: bold; + } + .keyword { + color: #6C95EB; + font-family: monospace; + } + .number { + color: #ED94C0; + } + .string { + color: #C9A26D; + } +} \ No newline at end of file diff --git a/Iceshrimp.Frontend/Pages/About.razor b/Iceshrimp.Frontend/Pages/About.razor new file mode 100644 index 00000000..98ddfc9f --- /dev/null +++ b/Iceshrimp.Frontend/Pages/About.razor @@ -0,0 +1,72 @@ +@page "/notes/{NoteId}/about" +@page "/{User}/about" +@using System.Text.Json +@using System.Text.RegularExpressions +@using Iceshrimp.Assets.PhosphorIcons +@using Iceshrimp.Frontend.Components +@using Iceshrimp.Frontend.Core.Miscellaneous +@using Iceshrimp.Frontend.Core.Services +@using Iceshrimp.Frontend.Localization +@using Microsoft.AspNetCore.Authorization +@using Microsoft.AspNetCore.Components.Sections +@using Microsoft.Extensions.Localization +@attribute [Authorize] +@inject ApiService Api; +@inject IStringLocalizer Loc; + + + + + + @Loc["About"] + + +@if (State == State.Loaded) +{ + @foreach (var document in Documents) + { +
+ @document.Key + +
+ } +} + +@code { + [Parameter] public string? NoteId { get; set; } + [Parameter] public string? User { get; set; } + private State State { get; set; } + private Dictionary Documents { get; set; } = []; + + protected override async Task OnInitializedAsync() + { + if (NoteId != null) + { + + } + else if (User != null) + { + var pattern = "^@([^@]+)@?(.+)?$"; + var matches = Regex.Match(User, pattern); + var userResponse = await Api.Users.LookupUserAsync(matches.Groups[1].Value, matches.Groups[2].Value); + + if (userResponse is null) + { + State = State.NotFound; + } + else + { + var profileResponse = await Api.Users.GetUserProfileAsync(userResponse.Id); + + Documents.Add(Loc["User response"], JsonSerializer.SerializeToDocument(userResponse)); + Documents.Add(Loc["Profile response"], JsonSerializer.SerializeToDocument(profileResponse)); + + State = State.Loaded; + } + } + else + { + State = State.NotFound; + } + } +} \ No newline at end of file diff --git a/Iceshrimp.Frontend/Pages/About.razor.css b/Iceshrimp.Frontend/Pages/About.razor.css new file mode 100644 index 00000000..72337c59 --- /dev/null +++ b/Iceshrimp.Frontend/Pages/About.razor.css @@ -0,0 +1,14 @@ +details { + padding: 1rem 1rem; + width: 100%; +} + +summary { + padding: 0.5rem 1rem; + background-color: var(--highlight-color); + cursor: pointer; +} + +summary:hover { + background-color: var(--hover-color); +} \ No newline at end of file