Iceshrimp.NET/Iceshrimp.Frontend/Components/RecursiveNote.razor

116 lines
No EOL
3.9 KiB
Text

@* 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
@if (_hidden == false)
{
<div class="@(Depth > 0 ? "descendant" : "root-note")">
<div class="note-container">
@if (_indented)
{
<div class="note-indent">
<img class="user-avatar" src="@Note.User.AvatarUrl"/>
@if (Note.Descendants?.Count > 0)
{
<Line></Line>
}
</div>
}
<div class="note" @onclick="OpenNote">
<Note Indented="_indented" NoteResponse="Note"/>
</div>
</div>
@if (Note.Descendants != null)
{
<div class="@(Note.Descendants?.Count > 1 ? "replies" : "reply")">
@* We are checking for null 3 lines up. *@
@for (var i = 0; i < Note.Descendants!.Count; i++)
{
var note = Note.Descendants[i];
@if (Note.Descendants?.Count > 1)
{
<div class="indented-reply">
<div class="reply-indent">
@if (i == Note.Descendants.Count - 1)
{
<EndIcon/>
<div class="line-container"></div>
}
else
{
<BranchIcon/>
<div class="line-container">
<Line/>
</div>
}
</div>
<RecursiveNote Note="note" Depth="Depth + 1" MaxDepth="MaxDepth"/>
</div>
}
else
{
<RecursiveNote Note="note" Depth="Depth + 1" MaxDepth="MaxDepth"/>
}
}
</div>
}
</div>
}
@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;
private IDisposable _noteChangedHandler = null!;
private bool _hidden = false;
protected override void OnInitialized()
{
_noteChangedHandler = MessageService.Register(Note.Id, OnNoteChanged, MessageService.Type.Updated);
if (Depth > 0 || Note.Descendants?.Count > 0)
{
_indented = true;
}
// If the note is filtered, we don't want to render its tree, and hide this note.
if (Note.Filtered is { Hide: true })
{
Note.Descendants = null;
_hidden = 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()
{
_noteChangedHandler.Dispose();
}
}