From ea5124df7729459adff77c7d2920b3fc5b400721 Mon Sep 17 00:00:00 2001 From: James Xylight Date: Tue, 5 Aug 2025 12:41:40 -0700 Subject: [PATCH] impr: fetch post data from feeds to display page instantly --- .../lemmy/community/CommunityCard.svelte | 357 +++++++++--------- .../post/[instance]/[id=integer]/+page.svelte | 78 ++-- .../post/[instance]/[id=integer]/+page.ts | 66 +++- .../[id=integer]/CommentProvider.svelte | 19 +- 4 files changed, 297 insertions(+), 223 deletions(-) diff --git a/src/lib/components/lemmy/community/CommunityCard.svelte b/src/lib/components/lemmy/community/CommunityCard.svelte index 93dbd2bb..b86853df 100644 --- a/src/lib/components/lemmy/community/CommunityCard.svelte +++ b/src/lib/components/lemmy/community/CommunityCard.svelte @@ -91,6 +91,7 @@ MenuButton, modal, removeToast, + Spinner, toast, } from 'mono-svelte' import { @@ -111,29 +112,28 @@ subscribing: false, }) - async function subscribe() { + async function subscribe(community: CommunityView) { if (!profile.current?.jwt) return loading.subscribing = true const subscribed = - community_view.subscribed == 'Subscribed' || - community_view.subscribed == 'Pending' + community.subscribed == 'Subscribed' || community.subscribed == 'Pending' try { await getClient().followCommunity({ - community_id: community_view.community.id, + community_id: community.community.id, follow: !subscribed, }) } catch (err) { toast({ content: errorMessage(err as string), type: 'error' }) } - community_view.subscribed = subscribed ? 'NotSubscribed' : 'Subscribed' - addSubscription(community_view.community, !subscribed) + community.subscribed = subscribed ? 'NotSubscribed' : 'Subscribed' + addSubscription(community.community, !subscribed) loading.subscribing = false } interface Props { - community_view: CommunityView + community_view: CommunityView | Promise moderators?: CommunityModeratorView[] class?: string } @@ -145,200 +145,207 @@ }: Props = $props() -
- + +
+{:then community_view} +
- {#snippet customIcon()} - - {/snippet} - - -
-
- - {#snippet title()} - - {$t('cards.site.about')} -
-
- {/snippet} - -
- -
- - {#snippet title()} - - {$t('cards.site.stats')} -
-
- {/snippet} -
- + {#snippet customIcon()} + - -
-
+ {/snippet} + - {#if moderators && moderators.length > 0} +

- + {#snippet title()} - {$t('cards.community.moderators')} - + {$t('cards.site.about')}
{/snippet} - ({ - id: m.moderator.id, - name: m.moderator.name, - url: userLink(m.moderator), - avatar: m.moderator.avatar, - instance: new URL(m.moderator.actor_id).hostname, - }))} - /> +
- {/if} -
-
- {#if profile.current?.jwt} - - {/if} - {#if profile.current?.user && amMod(profile.current.user, community_view.community)} - - {/if} - - {#snippet target()} -
+ + + {#if moderators && moderators.length > 0} +
+ + {#snippet title()} + + {$t('cards.community.moderators')} + +
+
{/snippet} - - {/snippet} - - - {$t('cards.community.modlog')} - + ({ + id: m.moderator.id, + name: m.moderator.name, + url: userLink(m.moderator), + avatar: m.moderator.avatar, + instance: new URL(m.moderator.actor_id).hostname, + }))} + /> +
+ {/if} +
+
{#if profile.current?.jwt} - - block(community_view.community.id, !community_view.blocked)} + + {/if} + {#if profile.current?.user && amMod(profile.current.user, community_view.community)} + + {/if} + + {#snippet target()} + + {/snippet} + + + {$t('cards.community.modlog')} - {#if profile.current?.user} + {#if profile.current?.jwt} blockInstance(community_view.community.instance_id)} - > - {#snippet prefix()} - - {/snippet} - {$t('cards.community.blockInstance')} - - {/if} - {#if profile.current?.user && isAdmin(profile.current.user)} - - modal({ - title: $t('admin.purgeCommunity.title'), - body: `${community_view.community.title}: ${$t('admin.purgeCommunity.warning')}`, - actions: [ - action({ - close: true, - content: $t('common.cancel'), - }), - action({ - action: () => purgeCommunity(community_view.community.id), - close: true, - content: $t('admin.purge'), - type: 'danger', - icon: Fire, - }), - ], - dismissable: true, - type: 'error', - })} + block(community_view.community.id, !community_view.blocked)} > {#snippet prefix()} - + {/snippet} - {$t('admin.purge')} + {community_view.blocked + ? $t('cards.community.unblock') + : $t('cards.community.block')} + {#if profile.current?.user} + + blockInstance(community_view.community.instance_id)} + > + {#snippet prefix()} + + {/snippet} + {$t('cards.community.blockInstance')} + + {/if} + {#if profile.current?.user && isAdmin(profile.current.user)} + + modal({ + title: $t('admin.purgeCommunity.title'), + body: `${community_view.community.title}: ${$t('admin.purgeCommunity.warning')}`, + actions: [ + action({ + close: true, + content: $t('common.cancel'), + }), + action({ + action: () => purgeCommunity(community_view.community.id), + close: true, + content: $t('admin.purge'), + type: 'danger', + icon: Fire, + }), + ], + dismissable: true, + type: 'error', + })} + > + {#snippet prefix()} + + {/snippet} + {$t('admin.purge')} + + {/if} {/if} - {/if} - + +
- +{/await} diff --git a/src/routes/post/[instance]/[id=integer]/+page.svelte b/src/routes/post/[instance]/[id=integer]/+page.svelte index fc4f740d..4c43c683 100644 --- a/src/routes/post/[instance]/[id=integer]/+page.svelte +++ b/src/routes/post/[instance]/[id=integer]/+page.svelte @@ -20,7 +20,7 @@ import { settings } from '$lib/settings.svelte.js' import { isImage } from '$lib/ui/image.js' import { Button, toast } from 'mono-svelte' - import { onMount } from 'svelte' + import { onMount, untrack } from 'svelte' import { ArrowLeft, ArrowRight, @@ -32,6 +32,7 @@ import { fly } from 'svelte/transition' import CommentProvider from './CommentProvider.svelte' import { ReactiveState } from '$lib/promise.svelte' + import { postFeeds } from '$lib/lemmy/postfeed.svelte' let { data } = $props() @@ -54,6 +55,21 @@ }) }) + $effect(() => { + data.post.value.meta.then( + res => (data.post.value.post_view = res.post_view), + ) + }) + + $effect(() => { + if (data.cachedFeed && data.post.value.post_view) { + untrack(() => { + const { id, index } = data.cachedFeed! + postFeeds.value[id].data.posts.posts[index] = data.post.value.post_view + }) + } + }) + let loading = $state(false) async function reloadComments() { @@ -117,7 +133,7 @@ - {#if data.post.value.cross_posts?.length > 0} - {@const crossposts = data.post.value.cross_posts} - - {#snippet title()} -
- {crossposts.length} - {$t('routes.post.crosspostCount')} -
-
- {/snippet} -
0} + - {#key crossposts} - {#each crossposts as crosspost (crosspost.post.id)} - - {/each} - {/key} -
-
- {/if} + {#snippet title()} +
+ {crossposts.length} + {$t('routes.post.crosspostCount')} +
+
+ {/snippet} +
+ {#key crossposts} + {#each crossposts as crosspost (crosspost.post.id)} + + {/each} + {/key} +
+ + {/if} + {/await} {#if data.thread.value.showContext || data.thread.value.singleThread}
+ cross_posts: Array + post_view: PostView + }> +} function buildContext(thread?: string) { let parentId: number | undefined @@ -27,6 +43,31 @@ function buildContext(thread?: string) { return { parentId, showContext, max_depth, focus: thread?.split('.').at(-1) } } +function findInFeed( + postId: number, + feedId: 'main' | 'community', +): + | { + post_view: PostView + feedData?: { + id: 'main' | 'community' + index: number + } + } + | undefined { + const index = postFeeds.value[feedId]?.data.posts.posts.findIndex( + i => i.post.id == postId, + ) + + return { + post_view: postFeeds.value[feedId]?.data.posts.posts[index], + feedData: { + id: feedId, + index: index, + }, + } +} + export async function load({ params, url, fetch }) { if (profile.current.instance != params.instance) redirect(302, resolveRoute('/post/[instance]/[id]/confirm', params)) @@ -34,14 +75,19 @@ export async function load({ params, url, fetch }) { // TODO use Lemmy profile default settings const sort = settings?.defaultSort?.comments ?? 'Hot' - const { parentId, showContext, max_depth, focus } = buildContext( + const { parentId, showContext, focus } = buildContext( url.searchParams.get('thread') || undefined, ) + const cachedPost = + findInFeed(Number(params.id), 'main') ?? + findInFeed(Number(params.id), 'community') + + const max_depth = (cachedPost?.post_view.counts.comments ?? 0) > 100 ? 1 : 3 + const commentParams: GetComments = { post_id: Number(params.id), type_: 'All', - limit: 50, page: 1, max_depth: max_depth, saved_only: false, @@ -50,26 +96,34 @@ export async function load({ params, url, fetch }) { } const comments = client({ func: fetch }).getComments(commentParams) - const post = await client({ func: fetch }).getPost({ + const post = client({ func: fetch }).getPost({ id: Number(params.id), }) + const partialPost: PartialPost = { + post_view: cachedPost ? cachedPost.post_view : (await post).post_view, + meta: post.then(i => ({ + ...i, + })), + } + return { thread: new ReactiveState({ showContext: showContext, singleThread: parentId != undefined, focus, }), - post: new ReactiveState(post), + post: new ReactiveState(partialPost), commentSort: new ReactiveState(sort), comments: new ReactiveState((await awaitIfServer(comments)).data), slots: { sidebar: { component: CommunityCard, props: { - community_view: post.community_view, + community_view: post.then(i => i.community_view), }, }, }, + cachedFeed: cachedPost?.feedData, } } diff --git a/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte b/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte index 416dc047..e10da729 100644 --- a/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte +++ b/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte @@ -12,7 +12,7 @@ import type { CommentSortType, GetCommentsResponse, - GetPostResponse, + PostView, } from 'lemmy-js-client' import { Button, Option, Select } from 'mono-svelte' import { onMount } from 'svelte' @@ -28,7 +28,7 @@ } from 'svelte-hero-icons' interface Props { - post: GetPostResponse + post: PostView comments: GetCommentsResponse sort?: CommentSortType onupdate?: () => void @@ -68,13 +68,12 @@
{/if} {#if virtualize} - + {:else}
- +
{/if}