diff --git a/crates/radicle-tauri/src/commands/cob/issue.rs b/crates/radicle-tauri/src/commands/cob/issue.rs index 77402a443..71eb5b63f 100644 --- a/crates/radicle-tauri/src/commands/cob/issue.rs +++ b/crates/radicle-tauri/src/commands/cob/issue.rs @@ -39,8 +39,11 @@ pub(crate) fn list_issues( ctx: tauri::State, rid: identity::RepoId, status: Option, -) -> Result, Error> { - ctx.list_issues(rid, status) + skip: Option, + // None: return all issues, `skip` is ignored. + take: Option, +) -> Result>, Error> { + ctx.list_issues(rid, status, skip, take) } #[tauri::command] diff --git a/crates/radicle-tauri/src/commands/cob/patch.rs b/crates/radicle-tauri/src/commands/cob/patch.rs index dc4b9ad2b..1e89845bf 100644 --- a/crates/radicle-tauri/src/commands/cob/patch.rs +++ b/crates/radicle-tauri/src/commands/cob/patch.rs @@ -57,9 +57,9 @@ pub async fn list_patches( let more = cursor + take < total; let content = patches - .map(|(id, patch)| models::patch::Patch::new(id, &patch, &aliases)) .skip(cursor) .take(take) + .map(|(id, patch)| models::patch::Patch::new(id, &patch, &aliases)) .collect::>(); Ok::<_, Error>(cobs::PaginatedQuery { diff --git a/crates/radicle-types/src/traits/issue.rs b/crates/radicle-types/src/traits/issue.rs index 787f869bc..43691ba47 100644 --- a/crates/radicle-types/src/traits/issue.rs +++ b/crates/radicle-types/src/traits/issue.rs @@ -15,10 +15,14 @@ pub trait Issues: Profile { &self, rid: identity::RepoId, status: Option, - ) -> Result, Error> { + skip: Option, + // None: return all issues, `skip` is ignored. + take: Option, + ) -> Result>, Error> { let profile = self.profile(); let repo = profile.storage.repository(rid)?; let status = status.unwrap_or_default(); + let cursor = skip.unwrap_or(0); let issues = profile.issues(&repo)?; let mut issues: Vec<_> = issues .list()? @@ -30,12 +34,36 @@ pub trait Issues: Profile { issues.sort_by(|(_, a), (_, b)| b.timestamp().cmp(&a.timestamp())); let aliases = &profile.aliases(); - let issues = issues - .into_iter() - .map(|(id, issue)| cobs::issue::Issue::new(&id, &issue, aliases)) - .collect::>(); - Ok::<_, Error>(issues) + match take { + None => { + let content = issues + .into_iter() + .map(|(id, issue)| cobs::issue::Issue::new(&id, &issue, aliases)) + .collect::>(); + + Ok::<_, Error>(cobs::PaginatedQuery { + cursor: 0, + more: false, + content, + }) + } + Some(take) => { + let more = cursor + take < issues.len(); + let content = issues + .into_iter() + .skip(cursor) + .take(take) + .map(|(id, issue)| cobs::issue::Issue::new(&id, &issue, aliases)) + .collect::>(); + + Ok::<_, Error>(cobs::PaginatedQuery { + cursor, + more, + content, + }) + } + } } fn issue_by_id( diff --git a/crates/test-http-api/src/api.rs b/crates/test-http-api/src/api.rs index 15f6367ec..6d2f00739 100644 --- a/crates/test-http-api/src/api.rs +++ b/crates/test-http-api/src/api.rs @@ -396,13 +396,20 @@ async fn repo_commit_handler( struct IssuesBody { pub rid: identity::RepoId, pub status: Option, + pub skip: Option, + pub take: Option, } async fn issues_handler( State(ctx): State, - Json(IssuesBody { rid, status }): Json, + Json(IssuesBody { + rid, + status, + skip, + take, + }): Json, ) -> impl IntoResponse { - let issues = ctx.list_issues(rid, status)?; + let issues = ctx.list_issues(rid, status, skip, take)?; Ok::<_, Error>(Json(issues)) } @@ -600,9 +607,9 @@ async fn patches_handler( let patches = patches .into_iter() - .map(|(id, patch)| models::patch::Patch::new(id, &patch, &aliases)) .skip(cursor) .take(take) + .map(|(id, patch)| models::patch::Patch::new(id, &patch, &aliases)) .collect::>(); Ok::<_, Error>(Json(cobs::PaginatedQuery { diff --git a/src/views/repo/Issues.svelte b/src/views/repo/Issues.svelte index e8702e15c..68fa90203 100644 --- a/src/views/repo/Issues.svelte +++ b/src/views/repo/Issues.svelte @@ -2,8 +2,10 @@ import type { IssueStatus } from "@app/views/repo/router"; import type { CacheEvent } from "@bindings/cob/CacheEvent"; import type { Issue } from "@bindings/cob/issue/Issue"; + import type { PaginatedQuery } from "@bindings/cob/PaginatedQuery"; import type { RepoInfo } from "@bindings/repo/RepoInfo"; + import { DEFAULT_TAKE } from "@app/views/repo/router"; import { Channel } from "@tauri-apps/api/core"; import fuzzysort from "fuzzysort"; import delay from "lodash/delay"; @@ -12,8 +14,10 @@ import { issueCountMismatch, resetIssueCounts, + updateIssueCounts, } from "@app/lib/issueCounts.svelte"; import { show } from "@app/lib/modal"; + import * as mutexExecutor from "@app/lib/mutexExecutor"; import * as router from "@app/lib/router"; import { modifierKey } from "@app/lib/utils"; @@ -21,6 +25,7 @@ import CobCacheWarning from "@app/components/CobCacheWarning.svelte"; import FuzzySearch from "@app/components/FuzzySearch.svelte"; import Icon from "@app/components/Icon.svelte"; + import InfiniteScrollSentinel from "@app/components/InfiniteScrollSentinel.svelte"; import IssueTeaser from "@app/components/IssueTeaser.svelte"; import ScrollArea from "@app/components/ScrollArea.svelte"; import Topbar from "@app/components/Topbar.svelte"; @@ -30,18 +35,66 @@ interface Props { repo: RepoInfo; - issues: Issue[]; + issues: PaginatedQuery; status: IssueStatus; } - /* eslint-disable prefer-const */ - let { repo, issues, status }: Props = $props(); - /* eslint-enable prefer-const */ + const { repo, issues, status }: Props = $props(); + // Parent reuses this component across status filter changes; a sibling + // $effect resets pagination state when the issues prop changes. + // svelte-ignore state_referenced_locally + let items = $state(issues.content); + // svelte-ignore state_referenced_locally + let more = $state(issues.more); + let loadingMore = $state(false); + let loading = $state(false); + let searchInput = $state(""); + let debouncedSearch = $state(""); + let showSearch = $state(false); let cacheState: CacheEvent | undefined = $state(); - let searchInput = $state(""); - let showSearch = $state(false); + const project = $derived(repo.payloads["xyz.radicle.project"]!); + + const loader = mutexExecutor.create(); + const abort = async (): Promise => undefined; + + $effect(() => { + items = issues.content; + more = issues.more; + // Abort any in-flight loadMoreContent so it cannot append a page + // from the previous filter onto the just-reset items. + void loader.run(abort); + }); + + $effect(() => { + if (more === false) { + updateIssueCounts( + items.length, + { + ...project.meta.issues, + all: project.meta.issues.open + project.meta.issues.closed, + }, + status, + ); + } + }); + + $effect(() => { + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + status; + + searchInput = ""; + showSearch = false; + }); + + $effect(() => { + const value = searchInput; + const timer = setTimeout(() => { + debouncedSearch = value; + }, 150); + return () => clearTimeout(timer); + }); async function rebuildIssueCache() { try { @@ -54,7 +107,19 @@ } catch (error) { console.error(error); } finally { - issues = await invoke("list_issues", { rid: repo.rid, status }); + const page = await loader.run(async () => { + return await invoke>("list_issues", { + rid: repo.rid, + skip: 0, + status, + take: DEFAULT_TAKE, + }); + }); + + if (page !== undefined) { + items = page.content; + more = page.more; + } resetIssueCounts(); @@ -64,16 +129,41 @@ } } - $effect(() => { - // eslint-disable-next-line @typescript-eslint/no-unused-expressions - status; + async function loadMoreContent(all: boolean = false): Promise { + if (!more) return; + loadingMore = true; + let superseded = false; + try { + const page = await loader.run(async () => { + return await invoke>("list_issues", { + rid: repo.rid, + status, + skip: all ? 0 : items.length, + take: all ? undefined : DEFAULT_TAKE, + }); + }); - searchInput = ""; - showSearch = false; - }); + // Superseded by a newer load (e.g. fuzzy-focus triggered a load-all). + // Leave items/more alone for the new call. The flag stays set too: the + // newer call owns it now, and clearing it here would let the + // virtualizer's auto load-more re-fire and abort that call in turn. + if (page === undefined) { + superseded = true; + return; + } + + more = page.more; + items = all ? page.content : [...items, ...page.content]; + if (page.content.length === 0) more = false; + } finally { + if (!superseded) { + loadingMore = false; + } + } + } const searchableIssues = $derived( - issues + items .flatMap(i => { return { issue: i, @@ -90,14 +180,12 @@ ); const searchResults = $derived( - fuzzysort.go(searchInput, searchableIssues, { + fuzzysort.go(debouncedSearch, searchableIssues, { keys: ["issue.title", "labels", "assignees", "author", "issue.id"], threshold: 0.5, all: true, }), ); - - const project = $derived(repo.payloads["xyz.radicle.project"]!);