~james/core

core/web/src/lib/components/comment/CommentCard.svelte -rw-r--r-- 9.3 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<script lang="ts">
	import type { Did } from "@atcute/lexicons/syntax";
	import Globe from "$icon/globe";
	import Pencil from "$icon/pencil";
	import Trash2 from "$icon/trash-2";
	import { deleteComment, eraseComment } from "#lib/api/comment.ts";
	import { cobRefOf } from "#lib/api/cob.ts";
	import { atUriOf, recordKeyOf, type KnotOrigin } from "#lib/api/syntax.ts";
	import { awaitDeletedRecord } from "#lib/api/awaitRecord.ts";
	import { createBobbinClient } from "#lib/api/client.ts";
	import { getAuth } from "#lib/auth.svelte.ts";
	import { type MarkupContext } from "#lib/markup/index.ts";
	import ErrorAlert from "#lib/components/ui/Error.svelte";
	import ConfirmModal from "#lib/components/ui/ConfirmModal.svelte";
	import ReportItems from "#lib/components/moderation/ReportItems.svelte";
	import { createReporting } from "#lib/components/moderation/reporting.svelte.ts";
	import { createAuthorGate } from "#lib/components/moderation/gate.svelte.ts";
	import DropdownItem from "#lib/components/ui/DropdownItem.svelte";
	import Separator from "#lib/components/ui/Separator.svelte";
	import MoreMenu from "#lib/components/ui/MoreMenu.svelte";
	import ReactionPicker from "#lib/components/reaction/ReactionPicker.svelte";
	import type { ReactionGroup } from "#lib/components/reaction/reactions.ts";
	import { createOptimisticReactions } from "#lib/components/reaction/reactions.svelte.ts";
	import Comment from "#lib/components/comment/Comment.svelte";
	import CommentBox from "#lib/components/comment/CommentBox.svelte";
	import CommentEditor from "#lib/components/comment/CommentEditor.svelte";
	import type {
		CommentThread,
		CommentView,
		ThreadInput
	} from "#lib/components/comment/comments.ts";
	import { revealComment } from "#lib/components/comment/reveal.ts";
	import { createAction } from "#lib/action.svelte.ts";
	import Card from "#lib/components/ui/Card.svelte";

	interface Props {
		thread: CommentThread;
		subjectUri: string;
		subjectCid?: string;
		markup: MarkupContext;
		knot?: KnotOrigin;
		onsubmitted?: (submitted: ThreadInput) => void;
		onedited?: (edited: ThreadInput) => void;
		ondeleted?: (uri: string) => void;
	}

	let { thread, subjectUri, subjectCid, markup, knot, onsubmitted, onedited, ondeleted }: Props =
		$props();

	const auth = getAuth();
	const currentUser = $derived(auth?.currentUser ?? null);
	const reporting = createReporting();
	const selfGate = createAuthorGate(
		() => thread.self.moderation,
		() => thread.self.authorDid,
		() => thread.self.uri
	);

	let editingUri = $state<string | null>(null);

	const reactionsByUri = $derived.by(() => {
		const map: Record<string, ReactionGroup[]> = {};
		map[thread.self.uri] = thread.self.reactions ?? [];
		for (const reply of thread.replies) map[reply.uri] = reply.reactions ?? [];
		return map;
	});

	const held = createOptimisticReactions({
		loaded: (uri) => reactionsByUri[uri],
		viewer: () => currentUser
	});

	const deleteSelected = createAction(async (comment: CommentView) => {
		if (!auth) return;
		// the knot checks permission itself, but a pds write has to come from the owner
		const agent = knot ? auth.agent : await auth.agentFor(comment.authorDid as Did);
		if (!agent) return;
		if (knot) await eraseComment(agent, knot, cobRefOf(comment));
		else await deleteComment(agent, recordKeyOf(comment.rkey));
		await awaitDeletedRecord(
			createBobbinClient({ serviceUrl: auth.bobbinUrl }),
			atUriOf(comment.uri)
		);
		ondeleted?.(comment.uri);
	});

	let deleteConfirmOpen = $state(false);
	let commentPendingDelete = $state<CommentView | null>(null);
	let commentPendingDeleteNoun = $state<"comment" | "reply">("comment");

	const handleDelete = (comment: CommentView, noun: "comment" | "reply") => {
		if (deleteSelected.loading) return;
		commentPendingDelete = comment;
		commentPendingDeleteNoun = noun;
		deleteConfirmOpen = true;
	};

	const deleteSelectedConfirmed = async () => {
		if (!commentPendingDelete) return;
		await deleteSelected.run(commentPendingDelete);
		if (!deleteSelected.error) deleteConfirmOpen = false;
	};
</script>

{#snippet commentActions(comment: CommentView, noun: "comment" | "reply")}
	<div
		class="flex items-center gap-3xs opacity-0 transition group-hover:opacity-100 focus-within:opacity-100"
	>
		{#if currentUser}
			<ReactionPicker
				subjectUri={comment.uri}
				subjectCid={comment.cid}
				{knot}
				groups={held.of(comment.uri)}
				onchange={(next) => held.set(comment.uri, next)}
				dropdownOrigin="tr"
				dropdownAlign="right"
				triggerClass="hit-area-x-0.5 overflow-visible"
			/>
		{/if}
		<MoreMenu
			group="comment-actions"
			size="small"
			width="fit"
			triggerClass="hit-area-x-0.5 overflow-visible"
			label={`More actions for ${comment.authorHandle}'s ${noun}`}
		>
			{#if auth?.hasAccount(comment.authorDid)}
				<DropdownItem icon={Pencil} onclick={() => (editingUri = comment.uri)}>
					Edit {noun}
				</DropdownItem>
				<DropdownItem
					icon={Trash2}
					danger={true}
					disabled={deleteSelected.loading &&
						deleteSelected.args?.[0].uri === comment.uri}
					onclick={() => handleDelete(comment, noun)}
				>
					Delete {noun}
				</DropdownItem>
				<Separator />
			{/if}
			<DropdownItem icon={Globe} onclick={() => navigator.clipboard.writeText(comment.uri)}>
				Copy AT URI
			</DropdownItem>
			{#if reporting.canReport(comment.authorDid)}
				<Separator />
			{/if}
			<ReportItems
				subjectUri={comment.uri}
				subjectCid={comment.cid}
				subjectNoun={noun}
				authorDid={comment.authorDid}
				authorHandle={comment.authorHandle}
			/>
		</MoreMenu>
	</div>
{/snippet}

{#snippet commentEditor(
	comment: CommentView,
	replyUri: string | undefined,
	replyCid: string | undefined
)}
	<CommentEditor
		{subjectUri}
		{subjectCid}
		replyToUri={replyUri}
		replyToCid={replyCid}
		authorDid={comment.authorDid}
		authorHandle={comment.authorHandle}
		uri={comment.uri}
		cid={comment.cid}
		createdAt={comment.createdAt}
		body={comment.body}
		{markup}
		{knot}
		rows={4}
		placeholder="Edit your comment. Markdown is supported."
		submitLabel="Save"
		submitIcon={Pencil}
		autofocus
		onsubmitted={(edited) => {
			editingUri = null;
			onedited?.(edited);
		}}
		oncancel={() => (editingUri = null)}
	/>
{/snippet}

{#snippet selfEditor()}
	{@render commentEditor(thread.self, undefined, undefined)}
{/snippet}

{#snippet selfActions()}
	{@render commentActions(thread.self, "comment")}
{/snippet}

<Card padding="none" class="overflow-visible bg-background-subtle dark:bg-background-default">
	<div
		data-testid="commentCard"
		data-uri={thread.self.uri}
		data-handle={thread.self.authorHandle}
		class="group overflow-clip rounded-t-md last:rounded-b-md last:*:border-b-0"
	>
		<Comment
			authorHandle={thread.self.authorHandle}
			authorDid={thread.self.authorDid}
			moderation={thread.self.moderation}
			gate={selfGate}
			createdAt={thread.self.createdAt}
			anchor={thread.self.rkey}
			body={thread.self.body}
			{markup}
			variant="top"
			deleted={thread.self.deleted}
			unloaded={thread.self.unloaded}
			reactions={held.of(thread.self.uri)}
			subjectUri={thread.self.uri}
			subjectCid={thread.self.cid}
			{knot}
			onreactionschange={(next) => held.set(thread.self.uri, next)}
			editor={editingUri === thread.self.uri ? selfEditor : undefined}
			actions={thread.self.deleted ? undefined : selfActions}
		/>
	</div>

	{#if thread.replies.length}
		<div data-testid="replies" class="ml-2xl">
			{#each thread.replies as reply, i (reply.uri)}
				{#snippet replyEditor()}
					{@render commentEditor(reply, thread.self.uri, thread.self.cid)}
				{/snippet}
				{#snippet replyActions()}
					{@render commentActions(reply, "reply")}
				{/snippet}
				<div in:revealComment|global={reply} class="group relative isolate -ml-lg">
					<!-- thread connector; on the last reply it stops at the avatar's centre (h-8) -->
					<div
						class={`pointer-events-none absolute top-0 left-4 -z-10 w-px -translate-x-1 bg-border-default ${i === thread.replies.length - 1 ? "h-8" : "bottom-0"}`}
					></div>
					<Comment
						authorHandle={reply.authorHandle}
						authorDid={reply.authorDid}
						moderation={reply.moderation}
						createdAt={reply.createdAt}
						anchor={reply.rkey}
						body={reply.body}
						{markup}
						variant="reply"
						reactions={held.of(reply.uri)}
						subjectUri={reply.uri}
						subjectCid={reply.cid}
						{knot}
						onreactionschange={(next) => held.set(reply.uri, next)}
						editor={editingUri === reply.uri ? replyEditor : undefined}
						actions={replyActions}
					/>
				</div>
			{/each}
		</div>
	{/if}

	{#if deleteSelected.error}
		<div class="border-t border-border-default px-xl py-xs">
			<ErrorAlert label={deleteSelected.error} />
		</div>
	{/if}

	{#if (thread.self.deleted && !thread.self.cid) || selfGate.masked}
		<!-- deleted parent with no recoverable strongRef; nothing to reply against -->
	{:else if currentUser}
		<div class={thread.replies.length ? "border-t border-border-default" : ""}>
			<CommentBox
				variant="thread"
				{subjectUri}
				{subjectCid}
				replyToUri={thread.self.uri}
				replyToCid={thread.self.cid}
				authorDid={currentUser.did}
				authorHandle={currentUser.handle}
				{markup}
				{knot}
				{onsubmitted}
			/>
		</div>
	{/if}
</Card>

<ConfirmModal
	bind:open={deleteConfirmOpen}
	title="Delete {commentPendingDeleteNoun}?"
	description="This cannot be undone."
	confirmLabel="Delete"
	loading={deleteSelected.loading}
	onconfirm={deleteSelectedConfirmed}
/>