mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 06:10:44 +08:00
Add infinite scrolling for patches listing
This commit is contained in:
@@ -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 };
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -119,6 +119,10 @@ impl Patch {
|
||||
revision_count: patch.revisions().count(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn timestamp(&self) -> u64 {
|
||||
self.timestamp.as_millis()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, TS)]
|
||||
|
||||
Reference in New Issue
Block a user