Add infinite scrolling for patches listing

This commit is contained in:
Sebastian Martinez
2024-10-07 13:11:08 +02:00
parent 6b8c73c615
commit e51df19fed
11 changed files with 142 additions and 53 deletions
+3
View File
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type PaginatedQuery<T> = { cursor: number; more: boolean; content: T };
+17 -9
View File
@@ -34,17 +34,25 @@ mod query {
Draft,
Archived,
Merged,
All,
}
impl PatchStatus {
pub fn matches(&self, patch: &patch::State) -> bool {
match self {
Self::Open => matches!(patch, patch::State::Open { .. }),
Self::Draft => matches!(patch, patch::State::Draft),
Self::Archived => matches!(patch, patch::State::Archived),
Self::Merged => matches!(patch, patch::State::Merged { .. }),
Self::All => true,
impl From<patch::Status> for PatchStatus {
fn from(value: patch::Status) -> Self {
match value {
patch::Status::Archived => Self::Archived,
patch::Status::Draft => Self::Draft,
patch::Status::Merged => Self::Merged,
patch::Status::Open => Self::Open,
}
}
}
impl From<PatchStatus> for patch::Status {
fn from(value: PatchStatus) -> Self {
match value {
PatchStatus::Archived => Self::Archived,
PatchStatus::Draft => Self::Draft,
PatchStatus::Merged => Self::Merged,
PatchStatus::Open => Self::Open,
}
}
}
+41 -18
View File
@@ -2,36 +2,59 @@ use radicle::git;
use radicle::identity::RepoId;
use radicle::patch::cache::Patches;
use radicle::storage::ReadStorage;
use serde::{Deserialize, Serialize};
use ts_rs::TS;
use crate::cob::query;
use crate::error::Error;
use crate::types::cobs;
use crate::AppState;
use crate::cob::query;
#[derive(Serialize, Deserialize, TS)]
#[ts(export)]
pub struct PaginatedQuery<T> {
pub cursor: usize,
pub more: bool,
pub content: T,
}
#[tauri::command]
pub fn list_patches(
ctx: tauri::State<AppState>,
pub async fn list_patches(
ctx: tauri::State<'_, AppState>,
rid: RepoId,
status: query::PatchStatus,
) -> Result<Vec<cobs::Patch>, Error> {
status: Option<query::PatchStatus>,
skip: Option<usize>,
take: Option<usize>,
) -> Result<PaginatedQuery<Vec<cobs::Patch>>, Error> {
let cursor = skip.unwrap_or(0);
let take = take.unwrap_or(20);
let repo = ctx.profile.storage.repository(rid)?;
let patches = ctx.profile.patches(&repo)?;
let mut patches: Vec<_> = patches
.list()?
.filter_map(|r| {
let (id, patch) = r.ok()?;
(status.matches(patch.state())).then_some((id, patch))
})
.collect::<Vec<_>>();
patches.sort_by(|(_, a), (_, b)| b.timestamp().cmp(&a.timestamp()));
let aliases = &ctx.profile.aliases();
let patches = patches
let cache = ctx.profile.patches(&repo)?;
let patches = match status {
None => cache.list()?.collect::<Vec<_>>(),
Some(s) => cache.list_by_status(&s.into())?.collect::<Vec<_>>(),
};
let more = cursor + take < patches.len();
let mut patches = patches
.into_iter()
.map(|(id, patch)| cobs::Patch::new(id, patch, aliases))
.filter_map(|p| {
p.map(|(id, patch)| cobs::Patch::new(id, patch, aliases))
.ok()
})
.skip(cursor)
.take(take)
.collect::<Vec<_>>();
Ok::<_, Error>(patches)
patches.sort_by_key(|b| std::cmp::Reverse(b.timestamp()));
Ok::<_, Error>(PaginatedQuery {
cursor,
more,
content: patches,
})
}
#[tauri::command]
+4
View File
@@ -119,6 +119,10 @@ impl Patch {
revision_count: patch.revisions().count(),
}
}
pub fn timestamp(&self) -> u64 {
self.timestamp.as_millis()
}
}
#[derive(Serialize, TS)]