From caebd0a325b48d9e5b203f2e461df00cecfbf363 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 22 Oct 2024 11:19:01 +0200 Subject: [PATCH] Narrow down `IssueOp` type by flattening it --- crates/radicle-tauri/bindings/IssueOp.ts | 48 +++++++++++++++++++++++ crates/radicle-tauri/bindings/IssueOps.ts | 10 ----- crates/radicle-tauri/src/commands/cob.rs | 34 ++++++++-------- src/views/repo/Issue.svelte | 27 ++++++------- 4 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 crates/radicle-tauri/bindings/IssueOp.ts delete mode 100644 crates/radicle-tauri/bindings/IssueOps.ts diff --git a/crates/radicle-tauri/bindings/IssueOp.ts b/crates/radicle-tauri/bindings/IssueOp.ts new file mode 100644 index 000000000..3567bf0bd --- /dev/null +++ b/crates/radicle-tauri/bindings/IssueOp.ts @@ -0,0 +1,48 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Author } from "./Author"; + +export type IssueOp = { entryId: string; timestamp: number; author: Author } & ( + | { type: "assign"; assignees: Array } + | { type: "edit"; title: string } + | { + type: "lifecycle"; + state: + | { status: "closed"; reason: "other" | "solved" } + | { status: "open" }; + } + | { type: "label"; labels: Array } + | { + type: "comment"; + /** + * Comment body. + */ + body: string; + /** + * Comment this is a reply to. + * Should be [`None` | `null`] if it's the top-level comment. + * Should be the root [`CommentId`] if it's a top-level comment. + */ + replyTo: string | null; + /** + * Embeded content. + */ + embeds: Array; + } + | { + type: "comment.edit"; + /** + * Comment being edited. + */ + id: string; + /** + * New value for the comment body. + */ + body: string; + /** + * New value for the embeds list. + */ + embeds: Array; + } + | { type: "comment.redact"; id: string } + | { type: "comment.react"; id: string; reaction: string; active: boolean } +); diff --git a/crates/radicle-tauri/bindings/IssueOps.ts b/crates/radicle-tauri/bindings/IssueOps.ts deleted file mode 100644 index e48fefb82..000000000 --- a/crates/radicle-tauri/bindings/IssueOps.ts +++ /dev/null @@ -1,10 +0,0 @@ -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { Author } from "./Author"; -import type { IssueAction } from "./IssueAction"; - -export type IssueOps = { - id: string; - action: IssueAction; - timestamp: number; - author: Author; -}; diff --git a/crates/radicle-tauri/src/commands/cob.rs b/crates/radicle-tauri/src/commands/cob.rs index d925c6335..f9d77bb7c 100644 --- a/crates/radicle-tauri/src/commands/cob.rs +++ b/crates/radicle-tauri/src/commands/cob.rs @@ -5,6 +5,7 @@ use radicle::git; use radicle::identity; use radicle::storage::{ReadRepository, ReadStorage}; +use crate::types::cobs::IssueAction; use crate::{error, types, AppState}; pub mod draft; @@ -12,14 +13,16 @@ pub mod issue; pub mod patch; #[derive(serde::Serialize, ts_rs::TS)] +#[serde(rename_all = "camelCase")] #[ts(export)] -pub struct IssueOps { +pub struct IssueOp { #[ts(as = "String")] - id: git::Oid, - action: types::cobs::IssueAction, + pub entry_id: git::Oid, + #[serde(flatten)] + pub action: types::cobs::IssueAction, #[ts(type = "number")] - timestamp: cob::Timestamp, - author: types::cobs::Author, + pub timestamp: cob::Timestamp, + pub author: types::cobs::Author, } #[tauri::command] @@ -53,25 +56,24 @@ pub fn activity_by_id( rid: identity::RepoId, type_name: cob::TypeName, id: git::Oid, -) -> Result, error::Error> { +) -> Result, error::Error> { let aliases = ctx.profile.aliases(); let repo = ctx.profile.storage.repository(rid)?; let ops = cob::store::ops(&id.into(), &type_name, &repo).unwrap(); - let mut actions: Vec = Vec::new(); + let mut actions: Vec = Vec::new(); for op in ops.into_iter() { actions.extend( op.actions .iter() - .filter_map(|action: &Vec| -> Option { - serde_json::from_slice(action).ok() - }) - .map(|action| { - serde_json::json!({ - "id": op.id, - "action": action, - "author": types::cobs::Author::new(op.author.into(), &aliases), - "timestamp": op.timestamp + .filter_map(|action: &Vec| -> Option { + let action: IssueAction = serde_json::from_slice(action).ok()?; + + Some(IssueOp { + entry_id: op.id, + action, + author: types::cobs::Author::new(op.author.into(), &aliases), + timestamp: op.timestamp, }) }), ) diff --git a/src/views/repo/Issue.svelte b/src/views/repo/Issue.svelte index 8ef1557b2..349432752 100644 --- a/src/views/repo/Issue.svelte +++ b/src/views/repo/Issue.svelte @@ -2,7 +2,7 @@ import type { Config } from "@bindings/Config"; import type { Issue } from "@bindings/Issue"; import type { RepoInfo } from "@bindings/RepoInfo"; - import type { IssueOps } from "@bindings/IssueOps"; + import type { IssueOp } from "@bindings/IssueOp"; import capitalize from "lodash/capitalize"; @@ -221,32 +221,31 @@ {/if}
- {#await invoke( "activity_by_id", { rid: repo.rid, typeName: "xyz.radicle.issue", id: issue.id }, ) then activity} - {#each activity.slice(1) as { action, timestamp, author }} - {#if action.type === "lifecycle"} + {#await invoke( "activity_by_id", { rid: repo.rid, typeName: "xyz.radicle.issue", id: issue.id }, ) then activity} + {#each activity.slice(1) as op} + {#if op.type === "lifecycle"}
- - alias={author.alias} /> change of status to {action.state - .status} + + alias={op.author.alias} /> change of status to {op.state.status} - {formatTimestamp(timestamp)} + {formatTimestamp(op.timestamp)}
- {:else if action.type === "comment"} + {:else if op.type === "comment"}
- +
- - {#if action.replyTo} + + {#if op.replyTo} replied to
- {formatOid(action.replyTo)} + {formatOid(op.replyTo)}
{:else} commented {/if} - {formatTimestamp(timestamp)} + {formatTimestamp(op.timestamp)}
{/if}