-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution: Create a comments component - publish events of kind 1 with root marker for parent comments and with reply marker for threaded replies
- Loading branch information
1 parent
a427615
commit 1e99f77
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<script lang="ts"> | ||
import { | ||
Button, | ||
InlineNotification, | ||
StructuredList, | ||
StructuredListBody, | ||
StructuredListCell, | ||
StructuredListHead, | ||
StructuredListRow, | ||
TextArea, | ||
ToastNotification | ||
} from "carbon-components-svelte"; | ||
import {NDKEvent} from "@nostr-dev-kit/ndk"; | ||
import {ndk} from "$lib/stores/event_sources/relays/ndk"; | ||
import {currentUser} from "$lib/stores/hot_resources/current-user"; | ||
import {onMount} from "svelte"; | ||
import CommentUser from "./CommentUser.svelte"; | ||
import {formatDateTime} from "$lib/helpers/mundane"; | ||
export let parentId: string; | ||
export let isRoot: boolean = false | ||
let comment: string | ||
let commentEvents: Set<NDKEvent> | ||
let selectedCommentId: string; | ||
let toastTimeout: number = 0; | ||
const postComment = async (content: string) => { | ||
const ndkEvent = new NDKEvent($ndk) | ||
const eventMarker = isRoot ? 'root' : 'reply' | ||
const eventId = isRoot ? parentId : selectedCommentId | ||
ndkEvent.kind = 1 | ||
ndkEvent.content = content | ||
ndkEvent.tags = [...ndkEvent.tags, ...[["e", eventId, "", eventMarker], ["p", $currentUser!.pubkey]]] | ||
await ndkEvent.publish() | ||
toastTimeout = 2000 | ||
comment = '' | ||
const timeout = setTimeout(() => { | ||
toastTimeout = 0 | ||
clearTimeout(timeout) | ||
}, toastTimeout) | ||
} | ||
const onReplyComment = (commentId) => { | ||
selectedCommentId = commentId | ||
isRoot = false | ||
} | ||
onMount(() => { | ||
(async () => { | ||
commentEvents = await $ndk.fetchEvents({kinds: [1], '#e': [parentId]}) | ||
})() | ||
}) | ||
</script> | ||
|
||
|
||
{#if commentEvents} | ||
{#each commentEvents?.entries() as [key, commentEvent]} | ||
<StructuredList key={commentEvent.id} style="margin-bottom: 1rem"> | ||
<StructuredListHead> | ||
<StructuredListRow head style="border-bottom: none; padding-bottom: 0"> | ||
<StructuredListCell head style="display: flex; align-items: center; justify-content: space-between"> | ||
<div> | ||
<CommentUser pubkey={commentEvent.pubkey}/> | ||
<span style="color: rgb(148, 163, 184);"> | ||
commented {formatDateTime(commentEvent.created_at)} | ||
</span> | ||
</div> | ||
|
||
<Button kind="ghost" size="small" on:click={() => onReplyComment(commentEvent.id)}>reply | ||
</Button> | ||
</StructuredListCell> | ||
</StructuredListRow> | ||
</StructuredListHead> | ||
<StructuredListBody> | ||
<StructuredListRow> | ||
<StructuredListCell> | ||
{commentEvent.content} | ||
</StructuredListCell> | ||
</StructuredListRow> | ||
</StructuredListBody> | ||
</StructuredList> | ||
{/each} | ||
{/if} | ||
|
||
{#if $currentUser} | ||
{#if toastTimeout > 0} | ||
<ToastNotification | ||
fullWidth | ||
kind="success" | ||
title="Success" | ||
subtitle="Your comment has been successfully posted" | ||
timeout={toastTimeout} | ||
/> | ||
{/if} | ||
<TextArea hideLabel placeholder="Write your comment here..." bind:value={comment}/> | ||
<Button size="field" style="margin-top: 1rem; text-align: center" on:click={() => postComment(comment)}> | ||
Comment | ||
</Button> | ||
{:else} | ||
<InlineNotification | ||
fullWidth | ||
hideCloseButton | ||
kind="warning" | ||
title="Login required:" | ||
subtitle="You need to login to post a comment." | ||
/> | ||
{/if} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script lang="ts"> | ||
import type {NDKUser, NDKUserProfile} from "@nostr-dev-kit/ndk"; | ||
import {ndk} from "$lib/stores/event_sources/relays/ndk"; | ||
export let pubkey: string; | ||
let commentUser: NDKUser; | ||
let userProfile: NDKUserProfile | undefined | ||
$: if (userProfile === undefined) { | ||
(async () => { | ||
commentUser = $ndk.getUser({ hexpubkey: pubkey }); | ||
await commentUser.fetchProfile(); | ||
userProfile = commentUser?.profile | ||
})() | ||
} | ||
</script> | ||
|
||
|
||
<span style="color: #fb923c">{userProfile?.name}</span> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters