From 2274a9c79bca66bd67d044570e0047d9c9be6d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Thu, 13 Mar 2025 16:05:59 -0600 Subject: [PATCH] Refactor patch loading --- .../radicle-tauri/src/commands/cob/patch.rs | 14 ++- crates/radicle-tauri/src/lib.rs | 1 + crates/radicle-types/src/traits/patch.rs | 24 ++++- src/components/PatchTeaser.svelte | 6 +- src/components/Review.svelte | 12 +-- src/components/Reviews.svelte | 6 +- src/components/Revision.svelte | 25 +++-- src/views/repo/Patch.svelte | 97 +++++++++---------- 8 files changed, 109 insertions(+), 76 deletions(-) diff --git a/crates/radicle-tauri/src/commands/cob/patch.rs b/crates/radicle-tauri/src/commands/cob/patch.rs index afb795072..5e83844cc 100644 --- a/crates/radicle-tauri/src/commands/cob/patch.rs +++ b/crates/radicle-tauri/src/commands/cob/patch.rs @@ -1,6 +1,5 @@ -use radicle::git; -use radicle::identity; use radicle::patch::{Action, TYPENAME}; +use radicle::{cob, git, identity}; use radicle_types as types; use radicle_types::cobs; @@ -79,6 +78,17 @@ pub fn revision_by_patch_and_id( ctx.revision_by_id(rid, id, revision_id) } +#[tauri::command] +pub fn review_by_patch_and_revision_and_id( + ctx: tauri::State, + rid: identity::RepoId, + id: git::Oid, + revision_id: git::Oid, + review_id: cob::patch::ReviewId, +) -> Result, Error> { + ctx.review_by_id(rid, id, revision_id, review_id) +} + #[tauri::command] pub fn edit_patch( ctx: tauri::State, diff --git a/crates/radicle-tauri/src/lib.rs b/crates/radicle-tauri/src/lib.rs index 260dae1a6..9f8186b92 100644 --- a/crates/radicle-tauri/src/lib.rs +++ b/crates/radicle-tauri/src/lib.rs @@ -47,6 +47,7 @@ pub fn run() { cob::patch::list_patches, cob::patch::patch_by_id, cob::patch::edit_patch, + cob::patch::review_by_patch_and_revision_and_id, cob::patch::revisions_by_patch, cob::patch::revision_by_patch_and_id, thread::create_issue_comment, diff --git a/crates/radicle-types/src/traits/patch.rs b/crates/radicle-types/src/traits/patch.rs index f722f8fa8..600ec1e68 100644 --- a/crates/radicle-types/src/traits/patch.rs +++ b/crates/radicle-types/src/traits/patch.rs @@ -1,7 +1,7 @@ use radicle::node::Handle; use radicle::patch::cache::Patches as _; use radicle::storage::ReadStorage; -use radicle::{git, identity, Node}; +use radicle::{cob, git, identity, Node}; use crate::cobs; use crate::domain::patch::models; @@ -63,6 +63,28 @@ pub trait Patches: Profile { Ok::<_, Error>(revision) } + + fn review_by_id( + &self, + rid: identity::RepoId, + id: git::Oid, + revision_id: git::Oid, + review_id: cob::patch::ReviewId, + ) -> Result, Error> { + let profile = self.profile(); + let repo = profile.storage.repository(rid)?; + let patches = profile.patches(&repo)?; + let review = patches.get(&id.into())?.and_then(|patch| { + let aliases = &profile.aliases(); + + patch + .reviews_of(revision_id.into()) + .find(|(id, _)| *id == &review_id) + .map(|(_, review)| models::patch::Review::new(review.clone(), aliases)) + }); + + Ok::<_, Error>(review) + } } pub trait PatchesMut: Profile { diff --git a/src/components/PatchTeaser.svelte b/src/components/PatchTeaser.svelte index 1ba70bb86..5749150e1 100644 --- a/src/components/PatchTeaser.svelte +++ b/src/components/PatchTeaser.svelte @@ -21,7 +21,7 @@ interface Props { compact?: boolean; - loadPatch?: (rid: string, patchId: string) => void; + loadPatch?: (patchId: string) => Promise; patch: Patch; rid: string; selected?: boolean; @@ -79,9 +79,9 @@ class:selected class="patch-teaser" style:align-items="flex-start" - onclick={() => { + onclick={async () => { if (loadPatch) { - loadPatch(rid, patch.id); + await loadPatch(patch.id); } else { void push({ resource: "repo.patch", diff --git a/src/components/Review.svelte b/src/components/Review.svelte index 015ee84fb..ac8bdddeb 100644 --- a/src/components/Review.svelte +++ b/src/components/Review.svelte @@ -34,7 +34,7 @@ config: Config; onNavigateBack: () => void; patchId: string; - reload: (reviewId?: string) => Promise; + loadReview: () => Promise; repo: RepoInfo; review: Review; revision: Revision; @@ -44,7 +44,7 @@ config, onNavigateBack, patchId, - reload, + loadReview, repo, review, revision, @@ -139,7 +139,7 @@ console.error("Editing review failed: ", error); } finally { labelSaveInProgress = false; - await reload(reviewId); + await loadReview(); } } @@ -166,7 +166,7 @@ } catch (error) { console.error("Creating comment failed", error); } finally { - await reload(review.id); + await loadReview(); } } @@ -187,7 +187,7 @@ } catch (error) { console.error("Editing comment failed: ", error); } finally { - await reload(review.id); + await loadReview(); } } @@ -215,7 +215,7 @@ } catch (error) { console.error("Editing comment reactions failed", error); } finally { - await reload(review.id); + await loadReview(); } } diff --git a/src/components/Reviews.svelte b/src/components/Reviews.svelte index 796ff109f..5db761daa 100644 --- a/src/components/Reviews.svelte +++ b/src/components/Reviews.svelte @@ -21,11 +21,11 @@ revision: Revision; config: Config; status: PatchStatus | undefined; - reload: () => Promise; + loadPatch: () => Promise; } /* eslint-disable prefer-const */ - let { rid, patchId, revision, config, status, reload }: Props = $props(); + let { rid, patchId, revision, config, status, loadPatch }: Props = $props(); /* eslint-enable prefer-const */ let hideReviews = $state( @@ -67,7 +67,7 @@ } catch (error) { console.error("Creating a review failed: ", error); } finally { - await reload(); + await loadPatch(); } } diff --git a/src/components/Revision.svelte b/src/components/Revision.svelte index b1aa0add2..b16ff9a0d 100644 --- a/src/components/Revision.svelte +++ b/src/components/Revision.svelte @@ -26,12 +26,19 @@ revision: Revision; config: Config; status: PatchStatus | undefined; - reload: () => Promise; + loadPatch: () => Promise; } /* eslint-disable prefer-const */ - let { rid, repoDelegates, patchId, revision, config, status, reload }: Props = - $props(); + let { + rid, + repoDelegates, + patchId, + revision, + config, + status, + loadPatch, + }: Props = $props(); /* eslint-enable prefer-const */ const commentThreads = $derived( @@ -74,7 +81,7 @@ } catch (error) { console.error("Editing revision failed: ", error); } finally { - await reload(); + await loadPatch(); } } @@ -101,7 +108,7 @@ } catch (error) { console.error("Editing reactions failed", error); } finally { - await reload(); + await loadPatch(); } } @@ -119,7 +126,7 @@ } catch (error) { console.error("Creating comment failed", error); } finally { - await reload(); + await loadPatch(); } } @@ -140,7 +147,7 @@ } catch (error) { console.error("Editing comment failed: ", error); } finally { - await reload(); + await loadPatch(); } } @@ -168,7 +175,7 @@ } catch (error) { console.error("Editing comment reactions failed", error); } finally { - await reload(); + await loadPatch(); } } @@ -214,7 +221,7 @@ - + { - patchTeasers = patches.content; - cursor = patches.cursor; - more = patches.more; - }); - $effect(() => { // eslint-disable-next-line @typescript-eslint/no-unused-expressions patch.id; @@ -101,23 +95,13 @@ selectedRevision = revisions.slice(-1)[0]; }); - const project = $derived(repo.payloads["xyz.radicle.project"]!); + $effect(() => { + patchTeasers = patches.content; + cursor = patches.cursor; + more = patches.more; + }); - async function loadPatch(rid: string, patchId: string) { - patch = await invoke("patch_by_id", { - rid: rid, - id: patchId, - }); - revisions = await invoke("revisions_by_patch", { - rid: rid, - id: patchId, - }); - activity = await invoke[]>("activity_by_patch", { - rid: repo.rid, - id: patch.id, - }); - review = undefined; - } + const project = $derived(repo.payloads["xyz.radicle.project"]!); async function editTitle(rid: string, patchId: string, title: string) { if (patch.title === updatedTitle) { @@ -141,7 +125,7 @@ } catch (error) { console.error("Editing title failed: ", error); } finally { - await reload(); + await loadPatch(); } } @@ -161,7 +145,7 @@ console.error("Editing labels failed", error); } finally { labelSaveInProgress = false; - await reload(); + await loadPatch(); } } @@ -181,7 +165,7 @@ console.error("Editing assignees failed", error); } finally { assigneesSaveInProgress = false; - await reload(); + await loadPatch(); } } @@ -202,7 +186,7 @@ } catch (error) { console.error("Changing state failed", error); } finally { - await reload(); + await loadPatch(); } } @@ -220,32 +204,38 @@ } } - async function reload(reviewId?: string) { - [config, repo, patches, patch, revisions, activity] = await Promise.all([ - invoke("config"), - invoke("repo_by_id", { + async function loadPatch(patchId: string = patch.id) { + [patch, revisions, activity, patches] = await Promise.all([ + invoke("patch_by_id", { rid: repo.rid, + id: patchId, + }), + invoke("revisions_by_patch", { + rid: repo.rid, + id: patchId, + }), + invoke[]>("activity_by_patch", { + rid: repo.rid, + id: patchId, }), invoke>("list_patches", { rid: repo.rid, status, }), - invoke("patch_by_id", { - rid: repo.rid, - id: patch.id, - }), - invoke("revisions_by_patch", { - rid: repo.rid, - id: patch.id, - }), - invoke[]>("activity_by_patch", { - rid: repo.rid, - id: patch.id, - }), ]); - review = revisions - .flatMap(r => r.reviews || []) - .find(review => review.id === reviewId); + } + + async function loadReview(reviewId: string | undefined = review?.id) { + if (!reviewId) { + return; + } + + review = await invoke("review_by_patch_and_revision_and_id", { + rid: repo.rid, + id: patch.id, + revisionId: findReviewRevision(reviewId).id, + reviewId, + }); } async function loadPatches(filter: PatchStatus | undefined) { @@ -260,12 +250,12 @@ } } - function findReviewRevision(review: Review): Revision { + function findReviewRevision(reviewId: string): Revision { // Every review is guaranteed to have a revision according to the protocol // model, so using type assertions here is safe. return revisions.find(revision => { return revision.reviews!.find(rev => { - return rev.id === review.id; + return rev.id === reviewId; }); }) as Revision; } @@ -419,7 +409,10 @@ {#each patchTeasers as teaser} { + review = undefined; + await loadPatch(id); + }} patch={teaser} rid={repo.rid} {status} @@ -455,9 +448,9 @@ {config} patchId={patch.id} {repo} - {reload} + {loadReview} {review} - revision={findReviewRevision(review)} + revision={findReviewRevision(review.id)} onNavigateBack={() => { review = undefined; }} /> @@ -643,7 +636,7 @@ rid={repo.rid} repoDelegates={repo.delegates} patchId={patch.id} - {reload} + {loadPatch} {status} revision={revisions[0]} {config} /> @@ -654,7 +647,7 @@ rid={repo.rid} repoDelegates={repo.delegates} patchId={patch.id} - {reload} + {loadPatch} {status} revision={selectedRevision} {config} />