feat: Add comment trees

This commit is contained in:
Xyphyn committed 2023-06-30 19:44:30 -07:00
1 parent 83608fdaa6
commit 09d86dd721
13 files changed
+325 -91

No files matched your search

+1 -1
View File
@@ -19,6 +19,7 @@
"svelte": "^3.54.0",
"svelte-check": "^3.0.1",
"svelte-hero-icons": "^5.0.0",
"svelte-markdown": "^0.2.3",
"tailwindcss": "^3.3.2",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
@@ -26,7 +27,6 @@
},
"dependencies": {
"lemmy-js-client": "^0.18.0-rc.2"
},
"type": "module"
}
+12
View File
@@ -0,0 +1,12 @@
<script lang="ts">
export let date: Date
const rtf = new Intl.RelativeTimeFormat('en', {
numeric: 'auto',
style: 'short',
}).format(
Math.floor((Date.now() - date.getTime()) / 1000 / 60 / 60 / 24),
'days'
)
</script>
{rtf}
+1 -1
View File
@@ -1,3 +1,3 @@
import { LemmyHttp } from 'lemmy-js-client'
export const lemmy = new LemmyHttp('https://lemmy.ml')
export const lemmy = new LemmyHttp('https://programming.dev')
+5
View File
@@ -0,0 +1,5 @@
import { lemmy } from '$lib/lemmy.js'
export function load() {
return lemmy.getPosts({ limit: 20, page: 1 })
}
+19 -5
View File
@@ -1,10 +1,24 @@
<script lang="ts">
import Link from '$lib/components/Link.svelte'
import MultiSelect from '$lib/components/MultiSelect.svelte'
import Post from './post/[id]/Post.svelte'
let community = ''
export let data
</script>
<input bind:value={community} placeholder="Community name" />
<div class="w-max">
<Link href="/c/{community}">Go</Link>
<div class="flex flex-row">
<div class="flex flex-col gap-2">
<MultiSelect options={['Subscribed', 'Local', 'All']} selected="Local" />
<div class="flex flex-col gap-4">
{#each data.posts as post}
<Post {post} />
{/each}
</div>
</div>
<div
class="bg-white dark:bg-zinc-900 border dark:border-zinc-800 border-slate-200
rounded-md flex flex-col overflow-hidden max-w-sm ml-auto w-full p-8 h-max"
>
<h1 class="font-bold text-xl">Lemmy</h1>
</div>
</div>
+105 -26
View File
@@ -1,36 +1,115 @@
<script lang="ts">
import MultiSelect from '$lib/components/MultiSelect.svelte'
import type { GetCommentsResponse, GetPostResponse } from 'lemmy-js-client'
import MultiSelect from '$lib/components/MultiSelect.svelte'
import type {
GetCommentsResponse,
GetPostResponse,
Comment,
CommentView,
} from 'lemmy-js-client'
import type { CommentNodeI } from './comments.js'
import Comments from './Comments.svelte'
export let data: { post: GetPostResponse; comments: GetCommentsResponse }
export let data: { post: GetPostResponse; comments: GetCommentsResponse }
const postData = data.post.post_view
const post = postData.post
const postData = data.post.post_view
const post = postData.post
const comments = data.comments.comments
const comments = data.comments.comments
function getCommentParentId(comment?: Comment): number | undefined {
const split = comment?.path.split('.')
// remove the 0
split?.shift()
return split && split.length > 1
? Number(split.at(split.length - 2))
: undefined
}
function getDepthFromComment(comment?: Comment): number | undefined {
const len = comment?.path.split('.').length
return len ? len - 2 : undefined
}
export function buildCommentsTree(
comments: CommentView[],
parentComment: boolean
): CommentNodeI[] {
const map = new Map<number, CommentNodeI>()
const depthOffset = !parentComment
? 0
: getDepthFromComment(comments[0].comment) ?? 0
for (const comment_view of comments) {
const depthI = getDepthFromComment(comment_view.comment) ?? 0
const depth = depthI ? depthI - depthOffset : 0
const node: CommentNodeI = {
comment_view,
children: [],
depth,
}
map.set(comment_view.comment.id, { ...node })
}
const tree: CommentNodeI[] = []
// if its a parent comment fetch, then push the first comment to the top node.
if (parentComment) {
const cNode = map.get(comments[0].comment.id)
if (cNode) {
tree.push(cNode)
}
}
for (const comment_view of comments) {
const child = map.get(comment_view.comment.id)
if (child) {
const parent_id = getCommentParentId(comment_view.comment)
if (parent_id) {
const parent = map.get(parent_id)
// Necessary because blocked comment might not exist
if (parent) {
parent.children.push(child)
}
} else {
if (!parentComment) {
tree.push(child)
}
}
}
}
return tree
}
let newComment = ''
async function comment() {}
</script>
<div class="flex flex-col gap-2">
<span class="flex flex-row gap-2 text-sm opacity-70">
<a href="/">c/{postData.community.name}</a>
<span class="opacity-50">{postData.creator.name}</span>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
{#if post.thumbnail_url}
<img src={post.thumbnail_url} alt={post.name} />
{/if}
{#if post.body}
<p>{post.body}</p>
{/if}
<span class="flex flex-row gap-2 text-sm opacity-70">
<a href="/">c/{postData.community.name}</a>
<span class="opacity-50">{postData.creator.name}</span>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
{#if post.thumbnail_url}
<img src={post.thumbnail_url} alt={post.name} />
{/if}
{#if post.body}
<p
class="bg-slate-100 border border-slate-200 dark:border-zinc-800 dark:bg-zinc-900 p-2 text-sm rounded-md"
>
{post.body}
</p>
{/if}
</div>
<div class="mt-4 flex flex-col gap-2">
<div class="font-bold text-lg">Comments</div>
<MultiSelect
options={['hot', 'new', 'top']}
optionNames={['Hot', 'New', 'Top']}
selected="hot"
/>
<div class="font-bold text-lg">Comments</div>
<MultiSelect
options={['hot', 'new', 'top']}
optionNames={['Hot', 'New', 'Top']}
selected="hot"
/>
</div>
{#each comments as comment}
{comment.comment.content}
{/each}
<Comments nodes={buildCommentsTree(comments, false)} isParent={true} />
+8
View File
@@ -0,0 +1,8 @@
<script lang="ts">
export let url: string
export let alt: string = ''
export let width: number
</script>
<img src={url} {alt} {width} class="rounded-full" />
+47
View File
@@ -0,0 +1,47 @@
<script lang="ts">
import { ArrowDown, ArrowUp, Icon } from 'svelte-hero-icons'
import Avatar from './Avatar.svelte'
import type { CommentNodeI } from './comments.js'
import SvelteMarkdown from 'svelte-markdown'
import RelativeDate from '$lib/components/RelativeDate.svelte'
export let node: CommentNodeI
</script>
<li class="py-2">
<details open class="flex flex-col gap-1">
<summary class="flex flex-row cursor-pointer arrow gap-2 items-center">
<Avatar
url={node.comment_view.creator.avatar ??
`https://api.dicebear.com/6.x/initials/svg?seed=${node.comment_view.creator.name}`}
width={24}
/>
<span class="text-sm">{node.comment_view.creator.name}</span>
<span class="text-sm opacity-60">
{node.comment_view.counts.score} point{node.comment_view.counts.score ==
1
? ''
: 's'}
</span>
<span class="text-sm opacity-60">
<RelativeDate date={new Date(node.comment_view.comment.published)} />
</span>
</summary>
<div class="mt-1">
<div class="flex flex-col gap-1">
<SvelteMarkdown source={node.comment_view.comment.content} />
<div class="flex flex-row gap-2">
<button>
<Icon src={ArrowUp} width={16} mini />
</button>
<span class="text-sm font-medium">
{node.comment_view.counts.score}
</span>
<button>
<Icon src={ArrowDown} width={16} mini />
</button>
</div>
</div>
</div>
</details>
</li>
+22
View File
@@ -0,0 +1,22 @@
<script lang="ts">
import Comment from './Comment.svelte'
import type { CommentNodeI } from './comments.js'
const maxComments = 10
export let nodes: CommentNodeI[]
export let isParent: boolean
</script>
<ul
class={isParent
? ''
: 'ml-2 mx-2.5 px-2.5 border-l-2 border-black/10 dark:border-white/10'}
>
{#each nodes.slice(0, maxComments) as node}
<Comment {node} />
{#if node.children?.length > 0}
<svelte:self nodes={node.children} isParent={false} />
{/if}
{/each}
</ul>
+64 -54
View File
@@ -1,66 +1,76 @@
<script lang="ts">
import type { GetPostResponse, PostView } from 'lemmy-js-client'
import {
ArrowDown,
ArrowUp,
ChatBubbleLeft,
ChevronDown,
ChevronUp,
Icon,
} from 'svelte-hero-icons'
import type {
CommentView,
GetPostResponse,
PostView,
Comment,
} from 'lemmy-js-client'
import {
ArrowDown,
ArrowUp,
ChatBubbleLeft,
ChevronDown,
ChevronUp,
Icon,
} from 'svelte-hero-icons'
import type { CommentNodeI } from './comments.js'
import RelativeDate from '$lib/components/RelativeDate.svelte'
let postRes: PostView
export { postRes as post }
let postRes: PostView
export { postRes as post }
const post = postRes.post
const post = postRes.post
</script>
<div
class="bg-white dark:bg-zinc-900 border dark:border-zinc-800 border-slate-200
class="bg-white dark:bg-zinc-900 border dark:border-zinc-800 border-slate-200
rounded-md flex flex-col overflow-hidden max-w-xl"
>
<div class="flex flex-col gap-2 bg-white p-4 rounded-md">
<span class="flex flex-row gap-2 text-sm opacity-70">
<a href="/">c/{postRes.community.name}</a>
<span class="opacity-50">{postRes.creator.name}</span>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
{#if post.url}
<a
href={post.url}
class="text-sky-400 max-w-[24ch] overflow-hidden
<div class="flex flex-col gap-2 bg-white dark:bg-zinc-900 p-4 rounded-md">
<span class="flex flex-row gap-2 text-sm opacity-70">
<a href="/">c/{postRes.community.name}</a>
<span class="opacity-50">{postRes.creator.name}</span>
<span class="opacity-50">
<RelativeDate date={new Date(postRes.post.published)} />
</span>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
{#if post.url}
<a
href={post.url}
class="text-sky-400 max-w-[24ch] overflow-hidden
whitespace-nowrap text-ellipsis text-xs hover:underline"
>
{post.url}
</a>
{/if}
{#if post.body}
<p class="text-sm max-h-24 line-clamp-3">
{post.body}
</p>
{/if}
>
{post.url}
</a>
{/if}
{#if post.body}
<p class="text-sm max-h-24 line-clamp-3">
{post.body}
</p>
{/if}
</div>
<div class="flex flex-row gap-2 p-2">
<div
class="flex flex-row items-center gap-1 rounded-md
bg-slate-100 dark:bg-zinc-800"
>
<button aria-label="Upvote" class="p-1 px-1.5">
<Icon src={ChevronUp} mini width={20} height={20} />
</button>
<span class="text-sm">{postRes.counts.score}</span>
<button aria-label="Downvote" class="p-1 px-1.5">
<Icon src={ChevronDown} mini width={20} height="20}" />
</button>
</div>
<div class="flex flex-row gap-2 p-2">
<div
class="flex flex-row items-center gap-1 rounded-md
bg-slate-100"
>
<button aria-label="Upvote" class="p-1 px-1.5">
<Icon src={ChevronUp} mini width={20} height={20} />
</button>
<span class="text-sm">{postRes.counts.score}</span>
<button aria-label="Downvote" class="p-1 px-1.5">
<Icon src={ChevronDown} mini width={20} height="20}" />
</button>
</div>
<a
class="flex flex-row items-center gap-2.5 p-1 px-2 rounded-md
bg-slate-100"
href="/post/{post.id}"
>
<Icon src={ChatBubbleLeft} width={16} height={16} />
<span class="text-sm">{postRes.counts.comments}</span>
</a>
</div>
<a
class="flex flex-row items-center gap-2.5 p-1 px-2 rounded-md
bg-slate-100 dark:bg-zinc-800"
href="/post/{post.id}"
>
<Icon src={ChatBubbleLeft} width={16} height={16} />
<span class="text-sm">{postRes.counts.comments}</span>
</a>
</div>
</div>
+7
View File
@@ -0,0 +1,7 @@
import type { CommentView } from 'lemmy-js-client'
export interface CommentNodeI {
comment_view: CommentView
children: Array<CommentNodeI>
depth: number
}
+16 -4
View File
@@ -3,13 +3,25 @@
@tailwind utilities;
:root {
@apply bg-slate-50 dark:bg-zinc-950 text-zinc-900 dark:text-slate-100;
@apply bg-slate-50 dark:bg-zinc-950 text-zinc-900
dark:text-slate-100;
}
@media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
}
}
#nprogress .bar {
@apply bg-black dark:bg-white !important;
@apply bg-black dark:bg-white !important;
}
#nprogress .peg {
display: none !important;
}
display: none !important;
}
pre {
@apply bg-slate-100 p-2 rounded-md m-2 max-w-full whitespace-pre-wrap
border-slate-200 border dark:bg-zinc-900 dark:border-zinc-800 text-sm;
}
+18
View File
@@ -235,6 +235,11 @@
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.5.1.tgz#b29aa1f91a59f35e29ff8f7cb24faf1a3a750554"
integrity sha512-COUnqfB2+ckwXXSFInsFdOAWQzCCx+a5hq2ruyj+Vjund94RJQd4LG2u9hnvJrTgunKAaax7ancBYlDrNYxA0g==
"@types/marked@^4.0.1":
version "4.3.1"
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-4.3.1.tgz#45fb6dfd47afb595766c71ed7749ead23f137de3"
integrity sha512-vSSbKZFbNktrQ15v7o1EaH78EbWV+sPQbPjHG+Cp8CaNcPFUEfjZ0Iml/V0bFDwsTlYe8o6XC5Hfdp91cqPV2g==
"@types/nprogress@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@types/nprogress/-/nprogress-0.2.0.tgz#86c593682d4199212a0509cc3c4d562bbbd6e45f"
@@ -679,6 +684,11 @@ magic-string@^0.30.0:
dependencies:
"@jridgewell/sourcemap-codec" "^1.4.13"
marked@^4.0.10:
version "4.3.0"
resolved "https://registry.yarnpkg.com/marked/-/marked-4.3.0.tgz#796362821b019f734054582038b116481b456cf3"
integrity sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==
merge2@^1.3.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
@@ -1057,6 +1067,14 @@ svelte-hmr@^0.15.1:
resolved "https://registry.yarnpkg.com/svelte-hmr/-/svelte-hmr-0.15.2.tgz#d2f6fc82e040f2734abd54cea5cbef46828f2538"
integrity sha512-q/bAruCvFLwvNbeE1x3n37TYFb3mTBJ6TrCq6p2CoFbSTNhDE9oAtEfpy+wmc9So8AG0Tja+X0/mJzX9tSfvIg==
svelte-markdown@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/svelte-markdown/-/svelte-markdown-0.2.3.tgz#16c6db61605512100cf25c3e18f8e4105837895c"
integrity sha512-2h680NzTXnAD0CXhxe3GeHl6W+ayG4iKQRl+BIDRw+R0mUE0OiNxP1Vt8Rn+aWevB/LBiBIPCAwvL+0BkG057A==
dependencies:
"@types/marked" "^4.0.1"
marked "^4.0.10"
svelte-preprocess@^5.0.3:
version "5.0.4"
resolved "https://registry.yarnpkg.com/svelte-preprocess/-/svelte-preprocess-5.0.4.tgz#2123898e079a074f7f4ef1799e10e037f5bcc55b"