feat: Add 'load more' to allow infinite comment trees
This commit is contained in:
1 parent
6b372fc906
commit
32a5641a0d
5 files changed
+192
-112
No files matched your search
@@ -98,62 +98,56 @@
|
||||
<span
|
||||
class="ml-auto translate-x-1 opacity-0
|
||||
group-hover:translate-x-0 group-hover:opacity-100 text-xs transition-all
|
||||
flex flex-row items-center gap-1 hover:dark:bg-white/10 p-1 rounded-md
|
||||
hover:bg-black/10 px-2"
|
||||
flex flex-row items-center gap-1 dark:bg-white/5 p-1 rounded-md
|
||||
bg-black/5 px-2"
|
||||
>
|
||||
{#if open}
|
||||
<div class="inline">
|
||||
<Icon src={Minus} width={16} height={16} mini />
|
||||
</div>
|
||||
Collapse
|
||||
<Icon src={Minus} width={16} height={16} mini />
|
||||
{:else}
|
||||
<Icon src={Plus} width={16} height={16} mini />
|
||||
Expand
|
||||
{#if node.children.length > 0}
|
||||
<span class="text-xs opacity-50">+{node.children.length}</span>
|
||||
{/if}
|
||||
{/if}
|
||||
</span>
|
||||
</summary>
|
||||
<div class="mt-1">
|
||||
<div
|
||||
class="flex flex-col gap-1 whitespace-pre-wrap
|
||||
<div
|
||||
class="flex flex-col gap-1 whitespace-pre-wrap
|
||||
max-w-full"
|
||||
>
|
||||
<div class="max-w-full break-words text-sm leading-6">
|
||||
<Markdown source={node.comment_view.comment.content} />
|
||||
</div>
|
||||
<div class="flex flex-row gap-2 items-center">
|
||||
<div class="flex flex-row items-center">
|
||||
<button
|
||||
on:click={upvote}
|
||||
class="pr-1.5 {node.comment_view.my_vote == 1
|
||||
? 'text-orange-500'
|
||||
: ''}"
|
||||
>
|
||||
<Icon src={ArrowUp} width={16} mini />
|
||||
</button>
|
||||
<span class="text-sm font-medium">
|
||||
{score}
|
||||
</span>
|
||||
<button
|
||||
on:click={downvote}
|
||||
class="pl-1.5 {node.comment_view.my_vote == -1
|
||||
? 'text-blue-500'
|
||||
: ''}"
|
||||
>
|
||||
<Icon src={ArrowDown} width={16} mini />
|
||||
</button>
|
||||
</div>
|
||||
>
|
||||
<div class="max-w-full break-words text-sm leading-6">
|
||||
<Markdown source={node.comment_view.comment.content} />
|
||||
</div>
|
||||
<div class="flex flex-row gap-2 items-center">
|
||||
<div class="flex flex-row items-center">
|
||||
<button
|
||||
class="flex flex-row gap-1 items-center transition-colors px-2 py-1
|
||||
rounded-md {Color.secondary}"
|
||||
on:click={() => (replying = !replying)}
|
||||
on:click={upvote}
|
||||
class="pr-1.5 {node.comment_view.my_vote == 1
|
||||
? 'text-orange-500'
|
||||
: ''}"
|
||||
>
|
||||
<Icon src={ChatBubbleOvalLeft} width={16} height={16} mini />
|
||||
<span class="text-xs">Reply</span>
|
||||
<Icon src={ArrowUp} width={16} mini />
|
||||
</button>
|
||||
<span class="text-sm font-medium">
|
||||
{score}
|
||||
</span>
|
||||
<button
|
||||
on:click={downvote}
|
||||
class="pl-1.5 {node.comment_view.my_vote == -1
|
||||
? 'text-blue-500'
|
||||
: ''}"
|
||||
>
|
||||
<Icon src={ArrowDown} width={16} mini />
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
class="flex flex-row gap-1 items-center transition-colors px-2 py-1
|
||||
rounded-md {Color.secondary}"
|
||||
on:click={() => (replying = !replying)}
|
||||
>
|
||||
<Icon src={ChatBubbleOvalLeft} width={16} height={16} mini />
|
||||
<span class="text-xs">Reply</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{#if replying}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
<script lang="ts">
|
||||
import Comment from './Comment.svelte'
|
||||
import type { CommentNodeI } from './comments'
|
||||
import type { Post } from 'lemmy-js-client'
|
||||
import { buildCommentsTree, type CommentNodeI } from './comments'
|
||||
import { page } from '$app/stores'
|
||||
import { onMount } from 'svelte'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import { ChevronDown, Icon } from 'svelte-hero-icons'
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
|
||||
const maxComments = 250
|
||||
|
||||
@@ -18,6 +20,39 @@
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
let loadingChildren = false
|
||||
|
||||
async function fetchChildren(parent: CommentNodeI) {
|
||||
if (
|
||||
!(
|
||||
parent.comment_view.counts.child_count > 0 &&
|
||||
parent.children.length == 0
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
loadingChildren = true
|
||||
|
||||
const newComments = await getClient().getComments({
|
||||
auth: $authData?.token,
|
||||
max_depth: 3,
|
||||
parent_id: parent.comment_view.comment.id,
|
||||
})
|
||||
|
||||
parent.children = buildCommentsTree(
|
||||
newComments.comments.filter(
|
||||
(comment) => comment.comment.id != parent.comment_view.comment.id
|
||||
),
|
||||
true
|
||||
).map((node) => ({
|
||||
children: node.children,
|
||||
comment_view: node.comment_view,
|
||||
depth: node.depth + parent.depth,
|
||||
}))
|
||||
|
||||
loadingChildren = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<ul
|
||||
@@ -30,6 +65,18 @@
|
||||
{#if node.children?.length > 0}
|
||||
<svelte:self {post} nodes={node.children} isParent={false} />
|
||||
{/if}
|
||||
{#if node.comment_view.counts.child_count > 0 && node.children.length == 0}
|
||||
<div class="m-1">
|
||||
<Button
|
||||
loading={loadingChildren}
|
||||
disabled={loadingChildren}
|
||||
on:click={() => fetchChildren(node)}
|
||||
>
|
||||
<Icon src={ChevronDown} width={16} mini />
|
||||
Load {node.comment_view.counts.child_count} more
|
||||
</Button>
|
||||
</div>
|
||||
{/if}
|
||||
</Comment>
|
||||
{/each}
|
||||
</ul>
|
||||
@@ -1,7 +1,73 @@
|
||||
import type { CommentView } from 'lemmy-js-client'
|
||||
import type { Comment, CommentView } from 'lemmy-js-client'
|
||||
|
||||
export interface CommentNodeI {
|
||||
comment_view: CommentView
|
||||
children: Array<CommentNodeI>
|
||||
depth: number
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
<script lang="ts">
|
||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||
import type { Comment, CommentView } from 'lemmy-js-client'
|
||||
import type { CommentNodeI } from '$lib/components/lemmy/comment/comments.js'
|
||||
import {
|
||||
buildCommentsTree,
|
||||
type CommentNodeI,
|
||||
} from '$lib/components/lemmy/comment/comments.js'
|
||||
import Comments from '$lib/components/lemmy/comment/Comments.svelte'
|
||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||
import { isImage, isVideo } from '$lib/ui/image.js'
|
||||
@@ -14,6 +17,7 @@
|
||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
||||
import CommunityCard from '$lib/components/community/CommunityCard.svelte'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -30,71 +34,9 @@
|
||||
}
|
||||
})
|
||||
|
||||
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 commentsPage = 1
|
||||
let loading = false
|
||||
let moreComments = true
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -204,6 +146,37 @@
|
||||
nodes={buildCommentsTree(comments.comments, false)}
|
||||
isParent={true}
|
||||
/>
|
||||
{#if moreComments}
|
||||
<Button
|
||||
{loading}
|
||||
disabled={loading}
|
||||
on:click={async () => {
|
||||
if (!moreComments) return
|
||||
loading = true
|
||||
|
||||
const loadedComments = await getClient().getComments({
|
||||
auth: $authData?.token,
|
||||
max_depth: 3,
|
||||
page: ++commentsPage,
|
||||
limit: 25,
|
||||
post_id: data.post.post_view.post.id,
|
||||
})
|
||||
|
||||
if (loadedComments.comments.length > 0) {
|
||||
comments.comments = [
|
||||
...comments.comments,
|
||||
...loadedComments.comments,
|
||||
]
|
||||
} else {
|
||||
moreComments = false
|
||||
}
|
||||
|
||||
loading = false
|
||||
}}
|
||||
>
|
||||
Load more
|
||||
</Button>
|
||||
{/if}
|
||||
{:catch}
|
||||
<div class="bg-red-500/10 border border-red-500 rounded-md p-4">
|
||||
Failed to load comments.
|
||||
|
||||
@@ -11,9 +11,9 @@ export async function load({ params }) {
|
||||
comments: getClient(params.instance).getComments({
|
||||
post_id: Number(params.id),
|
||||
type_: 'All',
|
||||
limit: 250,
|
||||
limit: 25,
|
||||
page: 1,
|
||||
max_depth: 6,
|
||||
max_depth: 3,
|
||||
saved_only: false,
|
||||
sort: 'Hot',
|
||||
auth: get(authData)?.token,
|
||||
|
||||
Reference in new issue
Block a user