From c01a48939e19a06a42587ab8e7bca8e0aa4b0eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Fri, 11 Oct 2024 18:03:47 +0200 Subject: [PATCH] Add comment threads --- public/index.css | 22 ++ src/components/Border.svelte | 5 +- src/components/Comment.svelte | 212 +++++++++++ src/components/CommentToggleInput.svelte | 74 ++++ src/components/ExtendedTextarea.svelte | 150 ++++++++ src/components/Icon.svelte | 35 ++ src/components/Id.svelte | 110 ++++++ src/components/IssueMetadata.svelte | 66 ++++ src/components/IssueStateBadge.svelte | 18 + src/components/IssueTeaser.svelte | 4 +- .../IssueTimelineLifecycleAction.svelte | 22 ++ src/components/PatchTeaser.svelte | 4 +- src/components/Popover.svelte | 4 +- src/components/ReactionSelector.svelte | 74 ++++ src/components/Reactions.svelte | 54 +++ src/components/RepoCard.svelte | 7 +- src/components/TextInput.svelte | 4 +- src/components/Textarea.svelte | 33 +- src/components/ThemeSwitch.svelte | 1 + src/components/Thread.svelte | 144 ++++++++ src/lib/file.ts | 38 ++ src/lib/roles.ts | 26 ++ src/lib/utils.ts | 27 +- src/views/repo/Issue.svelte | 338 ++++++++++++------ src/views/repo/Patch.svelte | 6 +- 25 files changed, 1336 insertions(+), 142 deletions(-) create mode 100644 src/components/Comment.svelte create mode 100644 src/components/CommentToggleInput.svelte create mode 100644 src/components/ExtendedTextarea.svelte create mode 100644 src/components/Id.svelte create mode 100644 src/components/IssueMetadata.svelte create mode 100644 src/components/IssueStateBadge.svelte create mode 100644 src/components/IssueTimelineLifecycleAction.svelte create mode 100644 src/components/ReactionSelector.svelte create mode 100644 src/components/Reactions.svelte create mode 100644 src/components/Thread.svelte create mode 100644 src/lib/file.ts create mode 100644 src/lib/roles.ts diff --git a/public/index.css b/public/index.css index dd489e5ca..fbac4f002 100644 --- a/public/index.css +++ b/public/index.css @@ -29,6 +29,13 @@ body { font-weight: var(--font-weight-regular); } +.global-commit { + color: var(--color-foreground-dim); + font-size: var(--font-size-small); + font-family: var(--font-family-monospace); + font-weight: var(--font-weight-semibold); +} + .global-flex { display: flex; align-items: center; @@ -104,6 +111,21 @@ body { 0 calc(100% - 4px) ); + --2px-top-corner-fill: polygon( + 0 4px, + 2px 4px, + 2px 2px, + 4px 2px, + 4px 0, + calc(100% - 4px) 0, + calc(100% - 4px) 2px, + calc(100% - 2px) 2px, + calc(100% - 2px) 4px, + 100% 4px, + 100% 100%, + 0 100% + ); + --3px-corner-fill: polygon( 0 6px, 2px 6px, diff --git a/src/components/Border.svelte b/src/components/Border.svelte index 911135a21..fc3152909 100644 --- a/src/components/Border.svelte +++ b/src/components/Border.svelte @@ -1,5 +1,5 @@ + + + +
+
+
+ + + {caption} + {#if id} + + {/if} + + {utils.formatTimestamp(timestamp)} + + {#if lastEdit} + + {/if} +
+ {#if id && editComment} +
+ +
+ {/if} + {#if id && reactions && reactOnComment} + { + try { + await reactOnComment(authors, emoji); + } finally { + closeFocused(); + } + }} /> + {/if} + +
+
+
+ + {#if body.trim() === "" && state === "read"} +
+ + No description. + +
+ {:else} +
+ {#if editComment && state !== "read"} +
+ { + state = "submit"; + try { + await editComment(comment, Array.from(embeds.values())); + } finally { + state = "read"; + } + }} + on:close={async () => { + body = body; + await tick(); + state = "read"; + }} /> +
+ {:else} +
+
+ +
+
+ {/if} +
+ {/if} + {#if reactions && reactions.length > 0} +
+ {#if id && reactions && reactOnComment} + { + try { + await reactOnComment(authors, emoji); + } finally { + closeFocused(); + } + }} /> + {/if} + +
+ {/if} +
diff --git a/src/components/CommentToggleInput.svelte b/src/components/CommentToggleInput.svelte new file mode 100644 index 000000000..79f950af3 --- /dev/null +++ b/src/components/CommentToggleInput.svelte @@ -0,0 +1,74 @@ + + + + +{#if state !== "collapsed"} + { + if (onclose !== undefined) { + onclose(); + } else { + state = "collapsed"; + } + }} + on:submit={async ({ detail: { comment, embeds } }) => { + try { + state = "submit"; + await submit(comment, Array.from(embeds.values())); + } finally { + state = "collapsed"; + } + }} /> +{:else} + { + state = "expanded"; + if (onexpand !== undefined) { + onexpand(); + } + }}> +
+ {placeholder} +
+
+{/if} diff --git a/src/components/ExtendedTextarea.svelte b/src/components/ExtendedTextarea.svelte new file mode 100644 index 000000000..f023a0c73 --- /dev/null +++ b/src/components/ExtendedTextarea.svelte @@ -0,0 +1,150 @@ + + + + +
+ {#if preview} +
+ +
+ {:else} + + diff --git a/src/components/ThemeSwitch.svelte b/src/components/ThemeSwitch.svelte index 18d7b1fa6..2f15404a9 100644 --- a/src/components/ThemeSwitch.svelte +++ b/src/components/ThemeSwitch.svelte @@ -44,6 +44,7 @@ clip-path: var(--1px-corner-fill); height: 24px; font-size: var(--font-size-small); + margin: 0; } diff --git a/src/components/Thread.svelte b/src/components/Thread.svelte new file mode 100644 index 000000000..37a837f95 --- /dev/null +++ b/src/components/Thread.svelte @@ -0,0 +1,144 @@ + + + + +
+
+ 1 ? root.edits.at(-1) : undefined} + author={root.author} + reactions={root.reactions} + timestamp={root.edits.slice(-1)[0].timestamp} + body={root.edits.slice(-1)[0].body} + editComment={editComment && + canEditComment(root.author.did) && + partial(editComment, root.id)} + reactOnComment={reactOnComment && partial(reactOnComment, root.id)}> + + + + +
+ {#if replies.length > 0 || (createReply && showReplyForm)} + +
+ {#if replies.length > 0} + {#each replies as reply} + 1 ? reply.edits.at(-1) : undefined} + id={reply.id} + author={reply.author} + caption="replied" + reactions={reply.reactions} + timestamp={reply.edits.slice(-1)[0].timestamp} + body={reply.edits.slice(-1)[0].body} + editComment={editComment && + canEditComment(reply.author.did) && + partial(editComment, reply.id)} + reactOnComment={reactOnComment && + partial(reactOnComment, reply.id)} /> + {/each} + {/if} + {#if createReply && showReplyForm} +
+ (showReplyForm = false)} + placeholder="Reply to comment" + submitCaption="Reply" + onexpand={toggleReply} + submit={partial(createReply, root.id)} /> +
+ {/if} +
+
+
+ {/if} +
diff --git a/src/lib/file.ts b/src/lib/file.ts new file mode 100644 index 000000000..94b0b92a9 --- /dev/null +++ b/src/lib/file.ts @@ -0,0 +1,38 @@ +async function parseGitOid(bytes: Uint8Array): Promise { + // Create the header + const header = new TextEncoder().encode(`blob ${bytes.length}\0`); + + // Concatenate the header and the original file content + const combined = new Uint8Array(header.length + bytes.length); + combined.set(header); + combined.set(bytes, header.length); + + // Compute the SHA-1 hash + const hashBuffer = await crypto.subtle.digest("SHA-1", combined); + const hashArray = Array.from(new Uint8Array(hashBuffer)); + + // Convert the hash to a hexadecimal string + return hashArray.map(b => b.toString(16).padStart(2, "0")).join(""); +} + +function base64String(file: File): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = (event: ProgressEvent) => { + if (event.target?.result && typeof event.target.result === "string") { + resolve(event.target.result); + } else { + reject(new Error("Failed to generate base64 string")); + } + }; + + reader.readAsDataURL(file); + }); +} + +export async function embed(file: File) { + const bytes = new Uint8Array(await file.arrayBuffer()); + const oid = await parseGitOid(bytes); + const content = await base64String(file); + return { oid, name: file.name, content }; +} diff --git a/src/lib/roles.ts b/src/lib/roles.ts new file mode 100644 index 000000000..81b6bac9c --- /dev/null +++ b/src/lib/roles.ts @@ -0,0 +1,26 @@ +import { publicKeyFromDid } from "@app/lib/utils"; + +export function isDelegate( + publicKey: string | undefined, + delegates: string[], +): true | undefined { + if (!publicKey) { + return undefined; + } + return ( + delegates.some(delegate => publicKeyFromDid(delegate) === publicKey) || + undefined + ); +} + +export function isDelegateOrAuthor( + publicKey: string | undefined, + delegates: string[], + author: string, +): true | undefined { + return ( + isDelegate(publicKey, delegates) || + publicKey === publicKeyFromDid(author) || + undefined + ); +} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e2696c49b..6d5dae3db 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -118,7 +118,9 @@ export function twemoji( export function scrollIntoView(id: string, options?: ScrollIntoViewOptions) { const lineElement = document.getElementById(id); - if (lineElement) lineElement.scrollIntoView(options); + if (lineElement) { + lineElement.scrollIntoView(options); + } } export const issueStatusColor: Record = { @@ -154,3 +156,26 @@ export const patchStatusBackgroundColor: Record< export function authorForNodeId(author: Author): ComponentProps { return { publicKey: publicKeyFromDid(author.did), alias: author.alias }; } + +export function absoluteTimestamp(timestamp: number) { + return new Date(Number(timestamp)).toLocaleString(); +} + +export function formatEditedCaption(author: Author, timestamp: number) { + return `${author.alias ? author.alias : truncateDid(author.did)} edited ${absoluteTimestamp(timestamp)}`; +} + +export function isMac() { + if ( + (navigator.platform && navigator.platform.includes("Mac")) || + navigator.userAgent.includes("OS X") + ) { + return true; + } else { + return false; + } +} + +export function modifierKey() { + return isMac() ? "⌘" : "ctrl"; +} diff --git a/src/views/repo/Issue.svelte b/src/views/repo/Issue.svelte index 05d4bcb12..fd31a236a 100644 --- a/src/views/repo/Issue.svelte +++ b/src/views/repo/Issue.svelte @@ -1,35 +1,169 @@ @@ -155,102 +286,75 @@
- -
-
Status
-
- {capitalize(issue.state.status)} -
-
+ -
- -
-
Labels
-
- {#each issue.labels as label} -
{label}
- {:else} - No labels. - {/each} -
-
- -
- -
-
Assignees
-
- {#each issue.assignees as assignee} - - {:else} - Not assigned to anyone. - {/each} -
-
-
- -
-
- - opened -
{formatOid(issue.id)}
- {formatTimestamp(issue.timestamp)} - {#if issue.discussion[0].edits.length > 1} - {@const lastEdit = issue.discussion[0].edits.slice(-1)[0]} - - • edited - - {/if} -
- {#if issue.discussion[0].edits.slice(-1)[0].body.trim() === ""} - - No description. - - {:else} - - {/if} +
+ 1 + ? issue.discussion[0].edits.at(-1) + : undefined} + author={issue.discussion[0].author} + reactions={issue.discussion[0].reactions} + timestamp={issue.discussion[0].edits.slice(-1)[0].timestamp} + body={issue.discussion[0].edits.slice(-1)[0].body} + editComment={roles.isDelegateOrAuthor( + config.publicKey, + repo.delegates.map(delegate => delegate.did), + issue.discussion[0].author.did, + ) && partial(editComment, issue.discussion[0].id)} + reactOnComment={partial( + reactOnComment, + config.publicKey, + issue.discussion[0].id, + )}> + + + + opened +
+
+
{#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={op.author.alias} /> change of status to {op.state.status} - - {formatTimestamp(op.timestamp)} -
-
+ +
{:else if op.type === "comment"} -
- -
- - {#if op.replyTo} - replied to
- {formatOid(op.replyTo)} -
- {:else} - commented - {/if} - - {formatTimestamp(op.timestamp)} -
-
+ {@const thread = threads.find(t => t.root.id === op.entryId)} + {#if thread} + delegate.did), + )} + {editComment} + createReply={partial(createReply)} + reactOnComment={partial(reactOnComment, config.publicKey)} /> +
+ {/if} {/if} {/each} {/await}
+ +
+ (topLevelReplyOpen = false) + : undefined} + placeholder="Leave a comment" + submit={partial(createComment)} /> +
diff --git a/src/views/repo/Patch.svelte b/src/views/repo/Patch.svelte index 41d9647e7..72c8a6626 100644 --- a/src/views/repo/Patch.svelte +++ b/src/views/repo/Patch.svelte @@ -6,7 +6,6 @@ import { authorForNodeId, - formatOid, formatTimestamp, patchStatusColor, } from "@app/lib/utils"; @@ -20,6 +19,7 @@ import Link from "@app/components/Link.svelte"; import NodeId from "@app/components/NodeId.svelte"; import Markdown from "@app/components/Markdown.svelte"; + import Id from "@app/components/Id.svelte"; export let repo: RepoInfo; export let patch: Patch; @@ -158,13 +158,13 @@
opened -
{formatOid(patch.id)}
+ {formatTimestamp(patch.timestamp)}
Revisions
{#each revisions as revision} -
{formatOid(revision.id)}
+ {/each}