feat: make comments available in ssr
This commit is contained in:
1 parent
b3ad7eeae9
commit
00840227d2
6 files changed
+62
-71
No files matched your search
+1
-1
@@ -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}
|
||||||
|
|||||||
@@ -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}`)
|
||||||
}
|
}
|
||||||
@@ -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,59 +290,45 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{#await data.streamed.comments}
|
{#if $profile?.user}
|
||||||
<div class="h-16 mx-auto grid place-items-center">
|
{#if showCreateComment}
|
||||||
<Spinner width={24} />
|
<CommentForm
|
||||||
</div>
|
postId={post.post_view.post.id}
|
||||||
{:then comments}
|
on:comment={(comment) =>
|
||||||
{#if $profile?.user}
|
(data.comments.comments = [
|
||||||
{#if showCreateComment}
|
comment.detail.comment_view,
|
||||||
<CommentForm
|
...data.comments.comments,
|
||||||
postId={post.post_view.post.id}
|
])}
|
||||||
on:comment={(comment) =>
|
locked={post.post_view.post.locked ||
|
||||||
(comments.comments = [
|
$page.params.instance.toLowerCase() != $instance.toLowerCase()}
|
||||||
comment.detail.comment_view,
|
/>
|
||||||
...comments.comments,
|
{:else}
|
||||||
])}
|
<Button
|
||||||
locked={post.post_view.post.locked ||
|
on:click={() => (showCreateComment = !showCreateComment)}
|
||||||
$page.params.instance.toLowerCase() != $instance.toLowerCase()}
|
size="lg"
|
||||||
/>
|
rounding="lg"
|
||||||
{:else}
|
class="mx-auto max-w-sm w-full"
|
||||||
<Button
|
>
|
||||||
on:click={() => (showCreateComment = !showCreateComment)}
|
<Icon src={Plus} size="16" mini slot="prefix" />
|
||||||
size="lg"
|
Add a comment
|
||||||
rounding="lg"
|
</Button>
|
||||||
class="mx-auto max-w-sm w-full"
|
|
||||||
>
|
|
||||||
<Icon src={Plus} size="16" mini slot="prefix" />
|
|
||||||
Add a comment
|
|
||||||
</Button>
|
|
||||||
{/if}
|
|
||||||
{/if}
|
{/if}
|
||||||
{#await buildCommentsTreeAsync(comments.comments)}
|
{/if}
|
||||||
<div class="h-16 mx-auto grid place-items-center">
|
<Comments
|
||||||
<Spinner width={36} />
|
post={post.post_view.post}
|
||||||
</div>
|
nodes={buildCommentsTree(data.comments.comments)}
|
||||||
{:then comments}
|
isParent={true}
|
||||||
<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.
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
color="tertiary"
|
color="tertiary"
|
||||||
on:click={() => window.scrollTo({ top: 0, behavior: 'smooth' })}
|
on:click={() => window.scrollTo({ top: 0, behavior: 'smooth' })}
|
||||||
slot="action"
|
slot="action"
|
||||||
>
|
>
|
||||||
<Icon src={ChevronDoubleUp} mini size="16" slot="prefix" />
|
<Icon src={ChevronDoubleUp} mini size="16" slot="prefix" />
|
||||||
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,
|
||||||
|
|||||||
Reference in new issue
Block a user