From ee0ee919d644ab51a66231b7ef966192a6ef3ed4 Mon Sep 17 00:00:00 2001 From: Lilian Date: Sat, 20 Jul 2024 03:17:05 +0200 Subject: [PATCH] [frontend] Make reply tree update when new replies are added (ISH-399) --- Iceshrimp.Frontend/Components/Compose.razor | 104 +++++++++--------- .../Components/RecursiveNote.razor | 35 +++++- Iceshrimp.Frontend/Pages/SingleNote.razor | 30 ++++- 3 files changed, 109 insertions(+), 60 deletions(-) diff --git a/Iceshrimp.Frontend/Components/Compose.razor b/Iceshrimp.Frontend/Components/Compose.razor index a2ac4ad4..51d22bd6 100644 --- a/Iceshrimp.Frontend/Components/Compose.razor +++ b/Iceshrimp.Frontend/Components/Compose.razor @@ -10,45 +10,46 @@ @inject ComposeService ComposeService @inject SessionService SessionService @inject IStringLocalizer Loc; -@inject GlobalComponentSvc GlobalComponentSvc +@inject GlobalComponentSvc GlobalComponentSvc +@inject MessageService MessageService
-
- - - -
- @if (ReplyOrQuote != null) - { -
- +
+ + +
- } - @if (NoteDraft.Cw != null) - { - -
- } - -
@@ -70,12 +71,7 @@ Cw = null }; - private Dictionary AvailablePlaceholders { get; set; } = new() - { - { "default", "What's on your mind?" }, - { "reply", "Reply goes here!" }, - { "quote", "Quote this post!" } - }; + private Dictionary AvailablePlaceholders { get; set; } = new() { { "default", "What's on your mind?" }, { "reply", "Reply goes here!" }, { "quote", "Quote this post!" } }; RenderFragment DropdownIcon(NoteVisibility vis) { @@ -108,9 +104,7 @@ new DropdownElement { #pragma warning disable BL0005 // Setting this outside the component is fine until this is reworked - Icon = DropdownIcon(vis), - Content = DropdownContent(vis), - Selection = vis + Icon = DropdownIcon(vis), Content = DropdownContent(vis), Selection = vis #pragma warning restore BL0005 }) .ToList(); @@ -203,14 +197,9 @@ private void ResetState() { - ReplyOrQuote = null; - Attachments = new List(); - NoteDraft = new NoteCreateRequest - { - Text = "", - Visibility = NoteVisibility.Followers, - Cw = null - }; + ReplyOrQuote = null; + Attachments = new List(); + NoteDraft = new NoteCreateRequest { Text = "", Visibility = NoteVisibility.Followers, Cw = null }; TextPlaceholder = AvailablePlaceholders["default"]; } @@ -227,7 +216,14 @@ } await ApiService.Notes.CreateNote(NoteDraft); + if (ReplyOrQuote != null) + { + var res = await ApiService.Notes.GetNote(ReplyOrQuote.Id); + if (res != null) _ = MessageService.UpdateNote(res); + } + await CloseDialog(); + // FIXME: Implement timeline refresh and call it here. } @@ -253,7 +249,7 @@ } private void ToggleEmojiPicker() - { + { GlobalComponentSvc.EmojiPicker?.Open(EmojiButton, new EventCallback(this, AddEmoji)); } @@ -270,4 +266,4 @@ { TextPlaceholder = AvailablePlaceholders["default"]; } -} +} \ No newline at end of file diff --git a/Iceshrimp.Frontend/Components/RecursiveNote.razor b/Iceshrimp.Frontend/Components/RecursiveNote.razor index 7483d26a..e1acce90 100644 --- a/Iceshrimp.Frontend/Components/RecursiveNote.razor +++ b/Iceshrimp.Frontend/Components/RecursiveNote.razor @@ -1,7 +1,11 @@ @* ReSharper disable once RedundantUsingDirective *@ @using Iceshrimp.Frontend.Components.Note +@using Iceshrimp.Frontend.Core.Services @using Iceshrimp.Shared.Schemas.Web @inject NavigationManager NavigationManager +@inject MessageService MessageService +@inject ApiService Api +@implements IDisposable
@@ -44,13 +48,13 @@
}
- + } else { - + } } @@ -58,20 +62,43 @@ @code { - [Parameter] [EditorRequired] public required NoteResponse Note { get; set; } - [Parameter] [EditorRequired] public required int Depth { get; set; } + [Parameter] [EditorRequired] public required NoteResponse Note { get; set; } + [Parameter] [EditorRequired] public required int Depth { get; set; } + [Parameter] [EditorRequired] public required int MaxDepth { get; set; } private bool _indented = false; protected override void OnInitialized() { + MessageService.Register(Note.Id, OnNoteChanged); if (Depth > 0 || Note.Descendants?.Count > 0) { _indented = true; } } + + private void OnNoteChanged(object? _, NoteResponse note) + { + var __ = Refresh(); + } + + private async Task Refresh() + { + if (Depth < MaxDepth) + { + var res = await Api.Notes.GetNoteDescendants(Note.Id, MaxDepth - Depth); + Note.Descendants = res; + StateHasChanged(); + } + + } private void OpenNote() { NavigationManager.NavigateTo($"/notes/{Note.Id}"); } + + public void Dispose() + { + MessageService.Unregister(Note.Id, OnNoteChanged); + } } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Pages/SingleNote.razor b/Iceshrimp.Frontend/Pages/SingleNote.razor index 7e932690..e242a320 100644 --- a/Iceshrimp.Frontend/Pages/SingleNote.razor +++ b/Iceshrimp.Frontend/Pages/SingleNote.razor @@ -5,6 +5,8 @@ @using Iceshrimp.Shared.Schemas.Web @inject ApiService ApiService @inject IJSRuntime Js +@inject MessageService MessageService +@implements IDisposable @if (_init) { @@ -28,7 +30,7 @@
@foreach (var element in Descendants) { - + }
} @@ -54,6 +56,7 @@ else private ElementReference RootNoteRef { get; set; } private bool _init; private bool _error; + private int _depth = 20; protected override async Task OnParametersSetAsync() { @@ -71,13 +74,31 @@ else return; } - Descendants = await ApiService.Notes.GetNoteDescendants(NoteId, default); + Descendants = await ApiService.Notes.GetNoteDescendants(NoteId, _depth); Ascendants = await ApiService.Notes.GetNoteAscendants(NoteId, default); _init = true; StateHasChanged(); } + protected override void OnInitialized() + { + if (NoteId != null) MessageService.Register(NoteId, OnNoteChanged); + } + + private void OnNoteChanged(object? _, NoteResponse note) + { + var __ = Refresh(); + } + + private async Task Refresh() + { + if (NoteId == null) throw new InvalidOperationException("RefreshNote called under impossible circumstances"); + Descendants = await ApiService.Notes.GetNoteDescendants(NoteId, default); + Ascendants = await ApiService.Notes.GetNoteAscendants(NoteId, default); + StateHasChanged(); + } + protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) @@ -91,4 +112,9 @@ else .InvokeVoidAsync("ScrollIntoView", RootNoteRef); } } + + public void Dispose() + { + if (NoteId != null) MessageService.Unregister(NoteId, OnNoteChanged); + } } \ No newline at end of file