feat: make comments available in ssr

This commit is contained in:
Xylight committed 2023-11-27 17:46:21 -08:00
1 parent b3ad7eeae9
commit 00840227d2
6 files changed
+30 -39

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "photon-lemmy", "name": "photon-lemmy",
"version": "1.22.1", "version": "1.22.2",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
@@ -21,7 +21,10 @@
export let isParent: boolean export let isParent: boolean
export let post: Post export let post: Post
let hydrated = false
onMount(() => { onMount(() => {
hydrated = true
if (isParent && $page.url.hash) { if (isParent && $page.url.hash) {
document.getElementById($page.url.hash)?.scrollIntoView({ document.getElementById($page.url.hash)?.scrollIntoView({
behavior: 'smooth', behavior: 'smooth',
@@ -116,12 +119,10 @@
<svelte:self {post} bind:nodes={node.children} isParent={false} /> <svelte:self {post} bind:nodes={node.children} isParent={false} />
{/if} {/if}
{#if node.comment_view.counts.child_count > 0 && node.children.length == 0} {#if node.comment_view.counts.child_count > 0 && node.children.length == 0}
<button <svelte:element
this={hydrated ? 'div' : 'a'}
class="w-full my-2 h-8 border-l-2 border-slate-200 dark:border-zinc-800 pl-2 text-left" class="w-full my-2 h-8 border-l-2 border-slate-200 dark:border-zinc-800 pl-2 text-left"
on:click={() => { href="/comment/{$page.params.instance}/{node.comment_view.comment.id}"
node.loading = true
fetchChildren(node).then(() => (node.loading = false))
}}
> >
<Button <Button
loading={node.loading} loading={node.loading}
@@ -138,7 +139,7 @@
<Icon src={ChevronDoubleDown} mini size="16" slot="prefix" /> <Icon src={ChevronDoubleDown} mini size="16" slot="prefix" />
{node.comment_view.counts.child_count} more {node.comment_view.counts.child_count} more
</Button> </Button>
</button> </svelte:element>
{/if} {/if}
</Comment> </Comment>
{/each} {/each}
+1
View File
@@ -2,5 +2,6 @@ import { getInstance } from '$lib/lemmy.js'
import { redirect } from '@sveltejs/kit' import { redirect } from '@sveltejs/kit'
export function load({ params }) { export function load({ params }) {
// If you somehow got to /comment/instance, it likely means you passed in an ID, not an instance.
throw redirect(300, `/comment/${getInstance()}/${params.instance}`) throw redirect(300, `/comment/${getInstance()}/${params.instance}`)
} }
+1 -1
View File
@@ -10,6 +10,6 @@ export async function load({ params, fetch }) {
throw redirect( throw redirect(
300, 300,
`/post/${comment.comment_view.post.id}?thread=${comment.comment_view.comment.path}#${comment.comment_view.comment.id}` `/post/${params.instance}/${comment.comment_view.post.id}?thread=${comment.comment_view.comment.path}#${comment.comment_view.comment.id}`
) )
} }
@@ -1,5 +1,8 @@
<script lang="ts"> <script lang="ts">
import { buildCommentsTreeAsync } from '$lib/components/lemmy/comment/comments.js' import {
buildCommentsTree,
buildCommentsTreeAsync,
} from '$lib/components/lemmy/comment/comments.js'
import Comments from '$lib/components/lemmy/comment/Comments.svelte' import Comments from '$lib/components/lemmy/comment/Comments.svelte'
import { isImage, isVideo } from '$lib/ui/image.js' import { isImage, isVideo } from '$lib/ui/image.js'
import { getClient, site } from '$lib/lemmy.js' import { getClient, site } from '$lib/lemmy.js'
@@ -99,13 +102,10 @@
let commentsPage = 1 let commentsPage = 1
let commentSort: CommentSortType = data.commentSort let commentSort: CommentSortType = data.commentSort
let loading = false let loading = false
let moreComments = true
async function reloadComments() { async function reloadComments() {
data.singleThread = false loading = true
commentsPage = 1 data.comments = await getClient().getComments({
data.streamed.comments = getClient().getComments({
page: 1, page: 1,
limit: 25, limit: 25,
type_: 'All', type_: 'All',
@@ -113,6 +113,9 @@
sort: commentSort, sort: commentSort,
max_depth: data.post.post_view.counts.comments > 100 ? 1 : 3, max_depth: data.post.post_view.counts.comments > 100 ? 1 : 3,
}) })
loading = false
data.singleThread = false
commentsPage = 1
} }
let showCreateComment = false let showCreateComment = false
@@ -254,7 +257,9 @@
flex-wrap gap-4 sticky top-20 w-full box-border z-[50] mt-4" flex-wrap gap-4 sticky top-20 w-full box-border z-[50] mt-4"
> >
<p>You're viewing a single thread.</p> <p>You're viewing a single thread.</p>
<Button on:click={reloadComments}>View full thread</Button> <Button {loading} disabled={loading} on:click={reloadComments}>
View full thread
</Button>
</Material> </Material>
{/if} {/if}
<div class="mt-4 flex flex-col gap-2 w-full"> <div class="mt-4 flex flex-col gap-2 w-full">
@@ -285,19 +290,14 @@
</Button> </Button>
</div> </div>
</div> </div>
{#await data.streamed.comments}
<div class="h-16 mx-auto grid place-items-center">
<Spinner width={24} />
</div>
{:then comments}
{#if $profile?.user} {#if $profile?.user}
{#if showCreateComment} {#if showCreateComment}
<CommentForm <CommentForm
postId={post.post_view.post.id} postId={post.post_view.post.id}
on:comment={(comment) => on:comment={(comment) =>
(comments.comments = [ (data.comments.comments = [
comment.detail.comment_view, comment.detail.comment_view,
...comments.comments, ...data.comments.comments,
])} ])}
locked={post.post_view.post.locked || locked={post.post_view.post.locked ||
$page.params.instance.toLowerCase() != $instance.toLowerCase()} $page.params.instance.toLowerCase() != $instance.toLowerCase()}
@@ -314,13 +314,11 @@
</Button> </Button>
{/if} {/if}
{/if} {/if}
{#await buildCommentsTreeAsync(comments.comments)} <Comments
<div class="h-16 mx-auto grid place-items-center"> post={post.post_view.post}
<Spinner width={36} /> nodes={buildCommentsTree(data.comments.comments)}
</div> isParent={true}
{:then comments} />
<Comments post={post.post_view.post} nodes={comments} isParent={true} />
{#if comments.length > 5}
<EndPlaceholder> <EndPlaceholder>
You've viewed {post.post_view.counts.comments} comments. You've viewed {post.post_view.counts.comments} comments.
@@ -333,11 +331,4 @@
Scroll to top Scroll to top
</Button> </Button>
</EndPlaceholder> </EndPlaceholder>
{/if}
{/await}
{:catch}
<div class="bg-red-500/10 border border-red-500 rounded-md p-4">
Failed to load comments.
</div>
{/await}
</div> </div>
@@ -45,9 +45,7 @@ export async function load({ params, url, fetch }) {
singleThread: parentId != undefined, singleThread: parentId != undefined,
post: post, post: post,
commentSort: sort, commentSort: sort,
streamed: { comments: await getClient(params.instance, fetch).getComments(commentParams),
comments: getClient(params.instance, fetch).getComments(commentParams),
},
slots: { slots: {
sidebar: { sidebar: {
component: CommunityCard, component: CommunityCard,