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

+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;
}