Add reactions to patch revisions

- Also adapts to the new reactions API schema everywhere.
This commit is contained in:
Sebastian Martinez
2024-02-02 13:33:07 +01:00
parent ebfec3fd8e
commit f0758de9f3
16 changed files with 273 additions and 173 deletions
+9 -10
View File
@@ -1,6 +1,5 @@
<script lang="ts" strictEvents>
import type { Comment, Embed } from "@httpd-client";
import type { GroupedReactions } from "@app/lib/reactions";
import { tick } from "svelte";
@@ -20,7 +19,7 @@
export let authorAlias: string | undefined = undefined;
export let body: string;
export let enableAttachments: boolean = false;
export let reactions: GroupedReactions | undefined = undefined;
export let reactions: Comment["reactions"] | undefined = undefined;
export let embeds: Map<string, Embed> | undefined = undefined;
export let caption = "commented";
export let rawPath: string;
@@ -34,7 +33,7 @@
export let editComment:
| ((body: string, embeds: Embed[]) => Promise<void>)
| undefined = undefined;
export let handleReaction:
export let reactOnComment:
| ((nids: string[], reaction: string) => Promise<void>)
| undefined = undefined;
</script>
@@ -175,22 +174,22 @@
<Markdown {rawPath} content={body} />
{/if}
</div>
{#if (id && handleReaction) || (id && reactions && reactions.size > 0)}
{#if (id && reactOnComment) || (id && reactions && reactions.length > 0)}
<div class="actions">
{#if id && handleReaction}
{@const handleReaction_ = handleReaction}
{#if id && reactOnComment}
{@const reactOnComment_ = reactOnComment}
<ReactionSelector
{reactions}
on:select={async ({ detail: { nids, reaction } }) => {
on:select={async ({ detail: { authors, emoji } }) => {
try {
await handleReaction_(nids, reaction);
await reactOnComment_(authors, emoji);
} finally {
closeFocused();
}
}} />
{/if}
{#if id && reactions && reactions.size > 0}
<Reactions {handleReaction} {reactions} />
{#if id && reactions && reactions.length > 0}
<Reactions handleReaction={reactOnComment} {reactions} />
{/if}
</div>
{/if}
+13 -9
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import type { GroupedReactions } from "@app/lib/reactions";
import type { Comment } from "@httpd-client";
import { createEventDispatcher } from "svelte";
@@ -9,10 +9,10 @@
import IconSmall from "./IconSmall.svelte";
import Popover from "./Popover.svelte";
export let reactions: GroupedReactions | undefined;
export let reactions: Comment["reactions"] | undefined = undefined;
const dispatch = createEventDispatcher<{
select: { nids: string[]; reaction: string };
select: { emoji: string; authors: string[] };
}>();
</script>
@@ -56,13 +56,17 @@
<div class="selector" slot="popover">
{#each config.reactions as reaction}
{@const lookedUpReaction = reactions?.find(
({ emoji }) => emoji === reaction,
)}
<button
class:active={Boolean(reactions?.get(reaction)?.self)}
on:click={() =>
dispatch("select", {
nids: reactions?.get(reaction)?.all ?? [],
reaction,
})}>
class:active={Boolean(lookedUpReaction)}
on:click={() => {
dispatch(
"select",
lookedUpReaction || { emoji: reaction, authors: [] },
);
}}>
{reaction}
</button>
{/each}
+7 -7
View File
@@ -1,11 +1,11 @@
<script lang="ts">
import type { GroupedReactions } from "@app/lib/reactions";
import type { Comment } from "@httpd-client";
import IconButton from "./IconButton.svelte";
export let reactions: GroupedReactions;
export let reactions: Comment["reactions"];
export let handleReaction:
| ((nids: string[], reaction: string) => Promise<void>)
| ((authors: string[], reaction: string) => Promise<void>)
| undefined;
</script>
@@ -24,16 +24,16 @@
</style>
<div class="reactions">
{#each reactions as [reaction, { all: nids }]}
{#each reactions as { emoji, authors }}
<IconButton
on:click={async () => {
if (handleReaction) {
await handleReaction(nids, reaction);
await handleReaction(authors, emoji);
}
}}>
<div class="reaction txt-tiny">
<span>{reaction}</span>
<span title={nids.join("\n")}>{nids.length}</span>
<span>{emoji}</span>
<span title={authors.join("\n")}>{authors.length}</span>
</div>
</IconButton>
{/each}
+7 -28
View File
@@ -1,26 +1,6 @@
<script lang="ts" context="module">
import type { Comment } from "@httpd-client";
import { groupReactions } from "@app/lib/reactions";
function groupReactionsInThread(thread: {
root: Comment;
replies: Comment[];
}) {
return {
root: {
...thread.root,
reactions: groupReactions(thread.root.reactions),
},
replies: thread.replies.map(reply => ({
...reply,
reactions: groupReactions(reply.reactions),
})),
};
}
</script>
<script lang="ts" strictEvents>
import type { Embed } from "@httpd-client";
import type { Comment } from "@httpd-client";
import * as utils from "@app/lib/utils";
import partial from "lodash/partial";
@@ -44,7 +24,7 @@
export let createReply:
| ((commentId: string, comment: string, embeds: Embed[]) => Promise<void>)
| undefined;
export let handleReaction:
export let reactOnComment:
| ((commentId: string, nids: string[], reaction: string) => Promise<void>)
| undefined;
@@ -57,9 +37,8 @@
});
}
$: threadWithReactions = groupReactionsInThread(thread);
$: root = threadWithReactions.root;
$: replies = threadWithReactions.replies;
$: root = thread.root;
$: replies = thread.replies;
</script>
<style>
@@ -102,7 +81,7 @@
editComment={editComment &&
canEditComment(root.author.id) &&
partial(editComment, root.id)}
handleReaction={handleReaction && partial(handleReaction, root.id)}>
reactOnComment={reactOnComment && partial(reactOnComment, root.id)}>
<IconSmall name="chat" slot="icon" />
</CommentComponent>
</div>
@@ -126,8 +105,8 @@
editComment={editComment &&
canEditComment(reply.author.id) &&
partial(editComment, reply.id)}
handleReaction={handleReaction &&
partial(handleReaction, reply.id)} />
reactOnComment={reactOnComment &&
partial(reactOnComment, reply.id)} />
{/each}
</div>
{/if}