feat: Add 'load more' to allow infinite comment trees

This commit is contained in:
Xyphyn committed 2023-07-07 13:45:59 -07:00
1 parent 6b372fc906
commit 32a5641a0d
5 files changed
+192 -112

No files matched your search

@@ -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>