Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Refactor patch loading
Rūdolfs Ošiņš committed 1 year ago
commit 2274a9c79bca66bd67d044570e0047d9c9be6d21
parent 87a455d2a4873a93d1b7dce28a7a56018e2153ad
8 files changed +106 -73
modified 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;
@@ -80,6 +79,17 @@ pub fn revision_by_patch_and_id(
}

#[tauri::command]
+
pub fn review_by_patch_and_revision_and_id(
+
    ctx: tauri::State<AppState>,
+
    rid: identity::RepoId,
+
    id: git::Oid,
+
    revision_id: git::Oid,
+
    review_id: cob::patch::ReviewId,
+
) -> Result<Option<models::patch::Review>, Error> {
+
    ctx.review_by_id(rid, id, revision_id, review_id)
+
}
+

+
#[tauri::command]
pub fn edit_patch(
    ctx: tauri::State<AppState>,
    rid: identity::RepoId,
modified 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,
modified 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<Option<models::patch::Review>, 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 {
modified src/components/PatchTeaser.svelte
@@ -21,7 +21,7 @@

  interface Props {
    compact?: boolean;
-
    loadPatch?: (rid: string, patchId: string) => void;
+
    loadPatch?: (patchId: string) => Promise<void>;
    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",
modified src/components/Review.svelte
@@ -34,7 +34,7 @@
    config: Config;
    onNavigateBack: () => void;
    patchId: string;
-
    reload: (reviewId?: string) => Promise<void>;
+
    loadReview: () => Promise<void>;
    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();
    }
  }
</script>
modified src/components/Reviews.svelte
@@ -21,11 +21,11 @@
    revision: Revision;
    config: Config;
    status: PatchStatus | undefined;
-
    reload: () => Promise<void>;
+
    loadPatch: () => Promise<void>;
  }

  /* 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();
    }
  }
</script>
modified src/components/Revision.svelte
@@ -26,12 +26,19 @@
    revision: Revision;
    config: Config;
    status: PatchStatus | undefined;
-
    reload: () => Promise<void>;
+
    loadPatch: () => Promise<void>;
  }

  /* 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();
    }
  }
</script>
@@ -214,7 +221,7 @@
  </CommentComponent>
</div>

-
<Reviews {config} {patchId} {reload} {revision} {rid} {status} />
+
<Reviews {config} {patchId} {loadPatch} {revision} {rid} {status} />

<Discussion
  cobId={patchId}
modified src/views/repo/Patch.svelte
@@ -86,12 +86,6 @@
  let selectedRevision: Revision = $state(revisions.slice(-1)[0]);

  $effect(() => {
-
    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>("patch_by_id", {
-
      rid: rid,
-
      id: patchId,
-
    });
-
    revisions = await invoke<Revision[]>("revisions_by_patch", {
-
      rid: rid,
-
      id: patchId,
-
    });
-
    activity = await invoke<Operation<Action>[]>("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>("config"),
-
      invoke<RepoInfo>("repo_by_id", {
-
        rid: repo.rid,
-
      }),
-
      invoke<PaginatedQuery<Patch[]>>("list_patches", {
-
        rid: repo.rid,
-
        status,
-
      }),
+
  async function loadPatch(patchId: string = patch.id) {
+
    [patch, revisions, activity, patches] = await Promise.all([
      invoke<Patch>("patch_by_id", {
        rid: repo.rid,
-
        id: patch.id,
+
        id: patchId,
      }),
      invoke<Revision[]>("revisions_by_patch", {
        rid: repo.rid,
-
        id: patch.id,
+
        id: patchId,
      }),
      invoke<Operation<Action>[]>("activity_by_patch", {
        rid: repo.rid,
-
        id: patch.id,
+
        id: patchId,
+
      }),
+
      invoke<PaginatedQuery<Patch[]>>("list_patches", {
+
        rid: repo.rid,
+
        status,
      }),
    ]);
-
    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>("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}
        <PatchTeaser
          compact
-
          {loadPatch}
+
          loadPatch={async (id: string) => {
+
            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} />