From fb0586ecbaee97a1d6576cab32348706fdc1a836 Mon Sep 17 00:00:00 2001 From: Lilian Date: Thu, 17 Oct 2024 03:19:14 +0200 Subject: [PATCH] [frontend/components] Update NoteDetails to use PaginationWrapper --- .../Components/NoteLikeDetails.razor | 55 +++++++++++++++-- .../Components/NoteQuoteDetails.razor | 59 ++++++++++--------- .../Components/NoteRenoteDetails.razor | 58 ++++++++++++++++-- 3 files changed, 133 insertions(+), 39 deletions(-) diff --git a/Iceshrimp.Frontend/Components/NoteLikeDetails.razor b/Iceshrimp.Frontend/Components/NoteLikeDetails.razor index ff87dda6..5cfc3cce 100644 --- a/Iceshrimp.Frontend/Components/NoteLikeDetails.razor +++ b/Iceshrimp.Frontend/Components/NoteLikeDetails.razor @@ -8,7 +8,7 @@ @if (State is State.Loaded) {
- @if (LikedBy != null) @foreach (var el in LikedBy) + @foreach (var el in LikedBy) {
} + @if (EndReached == false) + { + + } } @if (State is State.Loading) @@ -31,15 +35,27 @@ @code { - [Parameter, EditorRequired] public required string NoteId { get; set; } - private State State { get; set; } - private List? LikedBy { get; set; } = []; + [Parameter, EditorRequired] public required string NoteId { get; set; } + private State State { get; set; } + private List LikedBy { get; set; } = []; + private PaginationData Pd { get; set; } = null!; + private bool EndReached { get; set; } = false; protected override async Task OnInitializedAsync() { try { - LikedBy = await Api.Notes.GetNoteLikes(NoteId); + var pq = new PaginationQuery { Limit = 20 }; + var res = await Api.Notes.GetNoteLikes(NoteId, pq); + if (res is null) + { + State = State.NotFound; + Logger.LogWarning($"Likes for '{NoteId}' not found."); + return; + } + + if (res.Links.Next is null) EndReached = true; + LikedBy = res.Data; State = State.Loaded; } catch (ApiException e) @@ -49,6 +65,35 @@ } } + private async Task>?> FetchMore(PaginationData data) + { + if (data.Next is null) return null; + var pq = new PaginationQuery { MaxId = data.Next?.Split('=')[1], Limit = 20 }; + var res = await Api.Notes.GetNoteLikes(NoteId, pq); + return res; + } + + private async Task LoadMore() + { + if (EndReached) return; + try + { + var res = await FetchMore(Pd); + if (res is null) + { + EndReached = true; + return; + } + + Pd = res.Links; + LikedBy.AddRange(res.Data); + } + catch (ApiException e) + { + Logger.LogError(e, $"Failed to load likes"); + } + } + private void OpenProfile(string username, string? host) { var path = $"@{username}"; diff --git a/Iceshrimp.Frontend/Components/NoteQuoteDetails.razor b/Iceshrimp.Frontend/Components/NoteQuoteDetails.razor index 6ba97f32..0eee3c99 100644 --- a/Iceshrimp.Frontend/Components/NoteQuoteDetails.razor +++ b/Iceshrimp.Frontend/Components/NoteQuoteDetails.razor @@ -15,7 +15,7 @@ } - @if (_end is false) + @if (EndReached is false) { } @@ -23,31 +23,28 @@ } @code { - [Parameter, EditorRequired] public required string NoteId { get; set; } - private State State { get; set; } - private List Quotes { get; set; } = []; - private string? _minId; - private string? _maxId; - private bool _end = false; - private int _limit = 40; + [Parameter, EditorRequired] public required string NoteId { get; set; } + private State State { get; set; } + private List Quotes { get; set; } = []; + private PaginationData Pd { get; set; } = null!; + private bool EndReached { get; set; } = false; protected override async Task OnInitializedAsync() { try { - var res = await Api.Notes.GetQuotes(NoteId, new PaginationQuery { Limit = _limit }); - if (res is not null && res.Count > 0) + var res = await Api.Notes.GetQuotes(NoteId, new PaginationQuery { Limit = 20 }); + if (res is null) { - Quotes = res; - _minId = Quotes.Last().Id; - _maxId = Quotes.First().Id; - if (res.Count < _limit) - { - _end = true; - } + State = State.NotFound; + Logger.LogWarning($"Quotes for '{NoteId}' not found."); + return; } - State = State.Loaded; + if (res.Links.Next is null) EndReached = true; + Quotes = res.Data; + Pd = res.Links; + State = State.Loaded; } catch (ApiException e) { @@ -56,26 +53,32 @@ } } - private async void LoadMore() + private async Task>?> FetchMore(PaginationData data) { - var pq = new PaginationQuery { MaxId = _minId, Limit = 40 }; + if (data.Next is null) return null; + var pq = new PaginationQuery { MaxId = data.Next?.Split('=')[1], Limit = 20 }; + var res = await Api.Notes.GetQuotes(NoteId, pq); + return res; + } + + private async Task LoadMore() + { + if (EndReached) return; try { - var res = await Api.Notes.GetQuotes(NoteId, pq); - if (res is null) return; // If server returns null here, the note was likely deleted. - if (res.Count < 1) + var res = await FetchMore(Pd); + if (res is null) { - _end = true; + EndReached = true; return; } - if (res.Count < _limit) _end = true; - _minId = res.Last().Id; - Quotes.AddRange(res); + Pd = res.Links; + Quotes.AddRange(res.Data); } catch (ApiException e) { - Logger.LogError(e, "Failed to load more quotes"); + Logger.LogError(e, "Failed to load Quotes"); } } } \ No newline at end of file diff --git a/Iceshrimp.Frontend/Components/NoteRenoteDetails.razor b/Iceshrimp.Frontend/Components/NoteRenoteDetails.razor index 2e75b85d..c2b611e0 100644 --- a/Iceshrimp.Frontend/Components/NoteRenoteDetails.razor +++ b/Iceshrimp.Frontend/Components/NoteRenoteDetails.razor @@ -8,7 +8,7 @@ @if (State is State.Loaded) {
- @if (RenotedBy != null) @foreach (var el in RenotedBy) + @foreach (var el in RenotedBy) {
@@ -18,6 +18,10 @@
} + @if (EndReached == false) + { + + } } @if (State is State.Loading) @@ -31,16 +35,29 @@ @code { - [Parameter, EditorRequired] public required string NoteId { get; set; } - private State State { get; set; } - private List? RenotedBy { get; set; } = []; + [Parameter, EditorRequired] public required string NoteId { get; set; } + private State State { get; set; } + private List RenotedBy { get; set; } = []; + private PaginationData Pd { get; set; } = null!; + private bool EndReached { get; set; } protected override async Task OnInitializedAsync() { try { - RenotedBy = await Api.Notes.GetRenotes(NoteId); - State = State.Loaded; + var pq = new PaginationQuery { Limit = 20 }; + var res = await Api.Notes.GetRenotes(NoteId, pq); + if (res is null) + { + State = State.NotFound; + Logger.LogWarning($"Renotes for '{NoteId}' not found."); + return; + } + + if (res.Links.Next is null) EndReached = true; + RenotedBy = res.Data; + Pd = res.Links; + State = State.Loaded; } catch (ApiException e) { @@ -49,6 +66,35 @@ } } + private async Task>?> FetchMore(PaginationData data) + { + if (data.Next is null) return null; + var pq = new PaginationQuery { MaxId = data.Next?.Split('=')[1], Limit = 20 }; + var res = await Api.Notes.GetRenotes(NoteId, pq); + return res; + } + + private async Task LoadMore() + { + if (EndReached) return; + try + { + var res = await FetchMore(Pd); + if (res is null) + { + EndReached = true; + return; + } + + Pd = res.Links; + RenotedBy.AddRange(res.Data); + } + catch (ApiException e) + { + Logger.LogError(e, "Failed to load renotes"); + } + } + private void OpenProfile(string username, string? host) { var path = $"@{username}";