Iceshrimp.NET/Iceshrimp.Frontend/Components/FollowRequestList.razor

113 lines
No EOL
2.8 KiB
Text

@using Iceshrimp.Frontend.Core.Miscellaneous
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Frontend.Localization
@using Iceshrimp.Shared.Schemas.Web
@using Microsoft.Extensions.Localization
@inject ApiService Api;
@inject IStringLocalizer<Localization> Loc;
@inject ILogger<FollowRequestList> Logger;
@if (_init == LoadState.Ready)
{
<div class="scroller">
@foreach (var el in FollowRequests)
{
<FollowRequestEntry FollowRequest="el" OnDelete="Delete"/>
}
<ScrollEnd IntersectionChange="LoadMore" ManualLoad="LoadMore"/>
</div>
}
@if (_init == LoadState.Loading)
{
<div class="loading"><LoadingSpinner Scale="2" /></div>
}
@if (_init == LoadState.Emtpy)
{
@Loc["All done!"]
}
@code {
private List<FollowRequestResponse> FollowRequests { get; set; } = [];
private LoadState _init;
private string? _minId;
protected override async Task OnInitializedAsync()
{
await Init();
}
private async Task Init()
{
try
{
var pq = new PaginationQuery { Limit = 20 };
FollowRequests = await Api.FollowRequests.GetFollowRequestsAsync(pq);
if (FollowRequests.Count == 0)
{
_init = LoadState.Emtpy;
return;
}
_minId = FollowRequests.Last().Id;
_init = LoadState.Ready;
}
catch (ApiException)
{
_init = LoadState.Error;
}
catch (HttpRequestException)
{
_init = LoadState.Error;
Logger.LogError("Network error");
}
}
private void Delete(string id)
{
var i = FollowRequests.FindIndex(p => p.Id == id);
if (FollowRequests.Count == 1)
{
_init = LoadState.Emtpy;
StateHasChanged();
FollowRequests.RemoveAt(i);
_minId = null;
}
else
{
if (i == FollowRequests.Count - 1) _minId = FollowRequests[^2].Id;
if (i >= 0) FollowRequests.RemoveAt(i);
StateHasChanged();
}
}
private async Task LoadMore()
{
var pq = new PaginationQuery { MaxId = _minId, Limit = 20 };
var res = await Api.FollowRequests.GetFollowRequestsAsync(pq);
if (res.Count > 0)
{
FollowRequests.AddRange(res);
_minId = res.Last().Id;
StateHasChanged();
}
}
protected override async Task OnParametersSetAsync()
{
_init = LoadState.Loading;
_minId = null;
FollowRequests = [];
StateHasChanged();
await Init();
StateHasChanged();
}
private enum LoadState
{
Loading,
Ready,
Emtpy,
Error
}
}