perf: dynamically load comments on posts
This commit is contained in:
1 parent
5407a0e62d
commit
1cde3a1f78
3 files changed
+91
-57
No files matched your search
@@ -21,7 +21,7 @@
|
||||
} from 'svelte-hero-icons'
|
||||
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
|
||||
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
|
||||
import { Material, Select, removeToast, toast } from 'mono-svelte'
|
||||
import { Material, Select, Spinner, removeToast, toast } from 'mono-svelte'
|
||||
import type { CommentSortType } from 'lemmy-js-client'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { instance } from '$lib/instance.js'
|
||||
@@ -47,17 +47,17 @@
|
||||
|
||||
let post = data.post
|
||||
|
||||
onMount(async () => {
|
||||
if (
|
||||
!(post.post_view.read && $userSettings.markPostsAsRead) &&
|
||||
$profile?.jwt
|
||||
) {
|
||||
getClient().markPostAsRead({
|
||||
read: $userSettings.markPostsAsRead,
|
||||
post_id: post.post_view.post.id,
|
||||
})
|
||||
}
|
||||
})
|
||||
// onMount(async () => {
|
||||
// if (
|
||||
// !(post.post_view.read && $userSettings.markPostsAsRead) &&
|
||||
// $profile?.jwt
|
||||
// ) {
|
||||
// getClient().markPostAsRead({
|
||||
// read: $userSettings.markPostsAsRead,
|
||||
// post_id: post.post_view.post.id,
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
|
||||
afterNavigate(async () => {
|
||||
// reactivity hack
|
||||
@@ -109,6 +109,8 @@
|
||||
|
||||
$: remoteView =
|
||||
$page.params.instance?.toLowerCase() != $instance.toLowerCase()
|
||||
|
||||
const test = 'wow'
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -302,36 +304,48 @@ flex-wrap gap-4 sticky top-20 w-full box-border z-20 mt-4"
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{#if $profile?.user}
|
||||
<CommentForm
|
||||
postId={post.post_view.post.id}
|
||||
on:comment={(comment) =>
|
||||
(data.comments.comments = [
|
||||
comment.detail.comment_view,
|
||||
...data.comments.comments,
|
||||
])}
|
||||
locked={post.post_view.post.locked ||
|
||||
$page.params.instance.toLowerCase() != $instance.toLowerCase()}
|
||||
on:focus={() => (commenting = true)}
|
||||
tools={commenting}
|
||||
preview={commenting}
|
||||
placeholder={commenting ? undefined : $t('routes.post.addComment')}
|
||||
rows={commenting ? 7 : 1}
|
||||
{#await data.comments}
|
||||
<div class="flex flex-col gap-4">
|
||||
{#each new Array(10) as empty}
|
||||
<div class="animate-pulse flex flex-col gap-2 skeleton w-full">
|
||||
<div class="w-96 h-4" />
|
||||
<div class="w-full h-12" />
|
||||
<div class="w-48 h-4" />
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{:then comments}
|
||||
{#if $profile?.user}
|
||||
<CommentForm
|
||||
postId={post.post_view.post.id}
|
||||
on:comment={(comment) =>
|
||||
(comments.comments = [
|
||||
comment.detail.comment_view,
|
||||
...comments.comments,
|
||||
])}
|
||||
locked={post.post_view.post.locked ||
|
||||
$page.params.instance.toLowerCase() != $instance.toLowerCase()}
|
||||
on:focus={() => (commenting = true)}
|
||||
tools={commenting}
|
||||
preview={commenting}
|
||||
placeholder={commenting ? undefined : $t('routes.post.addComment')}
|
||||
rows={commenting ? 7 : 1}
|
||||
/>
|
||||
{/if}
|
||||
<Comments
|
||||
post={post.post_view.post}
|
||||
nodes={buildCommentsTree(
|
||||
comments.comments,
|
||||
undefined,
|
||||
(c) =>
|
||||
!(
|
||||
($userSettings.hidePosts.deleted && c.comment.deleted) ||
|
||||
($userSettings.hidePosts.removed && c.comment.removed)
|
||||
)
|
||||
)}
|
||||
isParent={true}
|
||||
/>
|
||||
{/if}
|
||||
<Comments
|
||||
post={post.post_view.post}
|
||||
nodes={buildCommentsTree(
|
||||
data.comments.comments,
|
||||
undefined,
|
||||
(c) =>
|
||||
!(
|
||||
($userSettings.hidePosts.deleted && c.comment.deleted) ||
|
||||
($userSettings.hidePosts.removed && c.comment.removed)
|
||||
)
|
||||
)}
|
||||
isParent={true}
|
||||
/>
|
||||
{/await}
|
||||
{#if post.post_view.counts.comments > 5}
|
||||
<EndPlaceholder>
|
||||
<span class="text-black dark:text-white font-bold">
|
||||
@@ -350,3 +364,9 @@ flex-wrap gap-4 sticky top-20 w-full box-border z-20 mt-4"
|
||||
</EndPlaceholder>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style lang="postcss">
|
||||
.skeleton * {
|
||||
@apply bg-slate-200 dark:bg-zinc-800 rounded-md;
|
||||
}
|
||||
</style>
|
||||
@@ -1,22 +1,20 @@
|
||||
import { browser } from '$app/environment'
|
||||
import { env } from '$env/dynamic/public'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { awaitIfServer } from '$lib/promise.js'
|
||||
import { SSR_ENABLED, userSettings } from '$lib/settings.js'
|
||||
import { error } from '@sveltejs/kit'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
export async function load({ params, url, fetch }) {
|
||||
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
||||
id: Number(params.id),
|
||||
})
|
||||
|
||||
let max_depth = post.post_view.counts.comments > 100 ? 1 : 3
|
||||
|
||||
const thread = url.searchParams.get('thread')
|
||||
let parentId: number | undefined
|
||||
let showContext: string | undefined = undefined
|
||||
|
||||
let max_depth = 3
|
||||
|
||||
if (thread) {
|
||||
const split = thread.split('.')
|
||||
if (split.length >= 9) {
|
||||
@@ -49,21 +47,26 @@ export async function load({ params, url, fetch }) {
|
||||
parent_id: parentId,
|
||||
}
|
||||
|
||||
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
||||
id: Number(params.id),
|
||||
})
|
||||
const comments = getClient(params.instance, fetch).getComments(commentParams)
|
||||
|
||||
return {
|
||||
thread: {
|
||||
showContext: showContext,
|
||||
singleThread: parentId != undefined
|
||||
singleThread: parentId != undefined,
|
||||
},
|
||||
post: post,
|
||||
commentSort: sort,
|
||||
comments: await getClient(params.instance, fetch).getComments(commentParams),
|
||||
slots: {
|
||||
sidebar: {
|
||||
component: CommunityCard,
|
||||
props: {
|
||||
community_view: post.community_view,
|
||||
},
|
||||
},
|
||||
},
|
||||
comments: (await awaitIfServer(comments)).data,
|
||||
// slots: {
|
||||
// sidebar: {
|
||||
// component: CommunityCard,
|
||||
// props: {
|
||||
// community_view: post.community_view,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user