[frontend] Make reply tree update when new replies are added (ISH-399)

This commit is contained in:
Lilian 2024-07-20 03:17:05 +02:00
parent 6dbde914a3
commit ee0ee919d6
No known key found for this signature in database
3 changed files with 109 additions and 60 deletions

View file

@ -11,6 +11,7 @@
@inject SessionService SessionService
@inject IStringLocalizer<Localization> Loc;
@inject GlobalComponentSvc GlobalComponentSvc
@inject MessageService MessageService
<dialog class="dialog" @ref="Dialog">
<div class="compose">
<div class="header">
@ -70,12 +71,7 @@
Cw = null
};
private Dictionary<string, string> AvailablePlaceholders { get; set; } = new()
{
{ "default", "What's on your mind?" },
{ "reply", "Reply goes here!" },
{ "quote", "Quote this post!" }
};
private Dictionary<string, string> 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<NoteVisibility>
{
#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();
@ -205,12 +199,7 @@
{
ReplyOrQuote = null;
Attachments = new List<DriveFileResponse>();
NoteDraft = new NoteCreateRequest
{
Text = "",
Visibility = NoteVisibility.Followers,
Cw = null
};
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.
}

View file

@ -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
<div class="@(Depth > 0 ? "descendant" : "root-note")">
<div class="note-container">
@ -44,13 +48,13 @@
</div>
}
</div>
<RecursiveNote Note="note" Depth="Depth + 1"/>
<RecursiveNote Note="note" Depth="Depth + 1" MaxDepth="MaxDepth"/>
</div>
}
else
{
<RecursiveNote Note="note" Depth="Depth + 1"/>
<RecursiveNote Note="note" Depth="Depth + 1" MaxDepth="MaxDepth"/>
}
}
</div>
@ -60,18 +64,41 @@
@code {
[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);
}
}

View file

@ -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 @@
<div class="descendants">
@foreach (var element in Descendants)
{
<RecursiveNote Note="element" Depth="0"/>
<RecursiveNote Note="element" Depth="0" MaxDepth="_depth"/>
}
</div>
}
@ -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);
}
}