feat: rewrite post feed fetching

This commit is contained in:
Xylight committed 2024-07-18 18:33:23 -07:00
1 parent 1a30bcee1d
commit 4e0d79ddef
8 files changed
+312 -241

No files matched your search

+8 -150
View File
@@ -1,27 +1,4 @@
<script lang="ts" context="module">
export interface CachedPosts {
data: {
sort: SortType
listingType: ListingType
page: number
posts: GetPostsResponse
cursor: {
next: string | undefined
}
}
params: URLSearchParams
lastSeen: number
instance: string
}
export const _posts = writable<CachedPosts>(undefined)
export const shouldReload = (
cache: CachedPosts | undefined,
params: string,
instance: string,
): boolean =>
cache?.instance != instance || cache?.params.toString() != params
</script>
<script lang="ts">
@@ -44,106 +21,18 @@
import { onMount } from 'svelte'
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
import { postFeeds, getPostFeed } from '$lib/lemmy/postfeed.js'
export let data
const limit = 20
let hasMore = data.posts.posts.length != 0
let error: any = undefined
let loading = false
async function loadMore() {
if (!hasMore || loading) return
try {
loading = true
const newPosts = await getClient()
.getPosts({
limit: limit,
page: data.page,
sort: data.sort,
type_: data.listingType,
page_cursor: data.cursor.next,
})
.catch((e) => {
throw new Error(e)
})
error = null
hasMore = newPosts.posts.length != 0
data.cursor.next = newPosts.next_page
data.posts.posts = [...data.posts.posts, ...newPosts.posts]
_posts.update((ps) => ({
data: data,
params: ps.params,
lastSeen: ps.lastSeen,
instance: ps.instance,
}))
loading = false
} catch (e) {
error = e
loading = false
}
}
const callback: IntersectionObserverCallback = (entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return
const element = entry.target as HTMLElement
const id = element.getAttribute('data-index')
if (!id) return
$_posts.lastSeen = Number(id)
observer.unobserve(element)
})
}
onMount(() => {
const observer = new IntersectionObserver(callback, {
root: null,
rootMargin: '0px',
threshold: 0.5,
})
const elements = document.querySelectorAll('.post-container')
elements.forEach((el) => observer.observe(el))
const postContainer = document.querySelector('#feed')
if (postContainer) {
const mutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (
node instanceof HTMLElement &&
node.classList.contains('post-container')
) {
observer.observe(node)
}
})
}
})
})
mutationObserver.observe(postContainer, {
childList: true,
subtree: true,
})
}
})
</script>
<div class="flex flex-col gap-4 max-w-full w-full min-w-0">
<header class="flex flex-col gap-4 relative">
<Header>{$t('routes.frontpage.title')}</Header>
<div class="flex items-center gap-2">
<Location changeDefault selected={data.listingType} />
{#if data.type_}
<Location changeDefault selected={data.type_} />
{/if}
<Sort changeDefault selected={data.sort} />
<ViewSelect />
</div>
@@ -152,46 +41,15 @@
<svelte:component
this={browser ? VirtualFeed : PostFeed}
posts={data.posts.posts}
>
{#if $userSettings.infiniteScroll && browser}
{#if error}
<div
class="flex flex-col justify-center items-center
rounded-xl gap-2 py-8 mt-6
border !border-b !border-red-500 bg-red-500/5 px-4"
>
<div class="bg-red-500/30 rounded-full p-3 text-red-500">
<Icon src={ExclamationTriangle} size="24" solid></Icon>
</div>
<pre class="py-0.5">{error}</pre>
<Button
color="primary"
{loading}
disabled={loading}
on:click={loadMore}
>
{$t('message.retry')}
</Button>
</div>
{:else if hasMore}
<div class="w-full flex flex-col skeleton gap-2 animate-pulse pt-6">
<div class="w-96 h-8"></div>
<div class="w-full h-48"></div>
<div class="!bg-transparent h-8 flex justify-between">
<div class="w-48 h-8"></div>
<div class="w-24 h-8"></div>
</div>
</div>
{/if}
<InfiniteScroll window threshold={1000} on:loadMore={loadMore} />
{/if}
</svelte:component>
bind:feedData={data}
lastSeen={$postFeeds.main.lastSeen}
feedId="main"
/>
<svelte:element
this={$userSettings.infiniteScroll ? 'noscript' : 'div'}
class="mt-auto"
>
<Pageination
page={data.page}
cursor={{ next: data.cursor.next }}
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
on:cursor={(c) => {
+12 -52
View File
@@ -3,14 +3,13 @@ import { userSettings } from '$lib/settings.js'
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { error } from '@sveltejs/kit'
import { _posts, shouldReload, type CachedPosts } from './+page.svelte'
import { browser } from '$app/environment'
import { instance } from '$lib/instance'
import { postFeed, postFeeds, shouldReload } from '$lib/lemmy/postfeed.js'
export async function load({ url, fetch }) {
const cursor = url.searchParams.get('cursor') as string | undefined
const prevCursor = url.searchParams.get('prevCursor') as string | undefined
const page = Number(url.searchParams.get('page')) || undefined
const settings = get(userSettings)
@@ -19,55 +18,16 @@ export async function load({ url, fetch }) {
const listingType: ListingType =
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
const cached: CachedPosts | undefined = get(_posts)
const posts = shouldReload(cached, url.searchParams.toString(), get(instance))
? await getClient(undefined, fetch).getPosts({
limit: 20,
page: page,
sort: sort,
type_: listingType,
page_cursor: cursor,
show_hidden: settings.posts.showHidden,
})
: cached.data.posts
if (
shouldReload(cached, url.searchParams.toString(), get(instance)) &&
browser
)
_posts.set({
data: {
sort: sort,
listingType: listingType,
page: page || 1,
posts: posts,
cursor: {
next: posts.next_page,
},
},
params: url.searchParams,
lastSeen: 0,
instance: get(instance),
})
try {
return shouldReload(cached, url.searchParams.toString(), get(instance))
? {
sort: sort,
listingType: listingType,
page: page || 1,
posts: posts,
cursor: {
next: posts.next_page,
back: prevCursor,
current: cursor,
},
}
: cached.data
} catch (err) {
error(500, {
message: 'Failed to fetch homepage.',
})
}
return await postFeed({
id: 'main',
request: {
page_cursor: cursor,
sort: sort,
type_: listingType,
limit: 20,
show_hidden: settings.posts.showHidden
},
url: url
})
}
+32 -22
View File
@@ -8,6 +8,7 @@
import { onDestroy, onMount } from 'svelte'
import { setSessionStorage } from '$lib/session.js'
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
import { Button, Modal, toast } from 'mono-svelte'
import { afterNavigate } from '$app/navigation'
import { browser } from '$app/environment'
@@ -22,6 +23,9 @@
import Placeholder from '$lib/components/ui/Placeholder.svelte'
import { profile } from '$lib/auth'
import { t } from '$lib/translations.js'
import { userSettings } from '$lib/settings.js'
import { site } from '$lib/lemmy.js'
import { postFeeds } from '$lib/lemmy/postfeed.js'
export let data
@@ -31,7 +35,7 @@
if (browser)
setSessionStorage(
'lastSeenCommunity',
data.community.community_view.community
data.community.community_view.community,
)
})
@@ -81,7 +85,7 @@
`!${data.community.community_view.community.name}@${
new URL(data.community.community_view.community.actor_id)
.hostname
}`
}`,
)
toast({ content: $t('toast.copied') })
@@ -89,7 +93,7 @@
class="dark:text-zinc-400 text-slate-600 text-sm text-left"
>
!{data.community.community_view.community.name}@{new URL(
data.community.community_view.community.actor_id
data.community.community_view.community.actor_id,
).hostname}
</button>
</div>
@@ -102,7 +106,7 @@
color="secondary"
href="/c/{fullCommunityName(
data.community.community_view.community.name,
data.community.community_view.community.actor_id
data.community.community_view.community.actor_id,
)}/settings"
>
<Icon src={Cog6Tooth} size="16" mini />
@@ -132,30 +136,36 @@
</Button>
</Placeholder>
{:else}
<PostFeed community={true} posts={data.posts.posts} />
<svelte:component
this={browser ? VirtualFeed : PostFeed}
posts={data.posts.posts.posts}
bind:feedData={data.posts}
lastSeen={$postFeeds.community.lastSeen}
feedId="main"
/>
{/if}
<Pageination
page={data.page}
cursor={{ next: data.posts.next_page }}
on:cursor={(c) => searchParam($page.url, 'cursor', c.detail)}
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
<svelte:element
this={$userSettings.infiniteScroll ? 'noscript' : 'div'}
class="mt-auto"
>
{#if data.posts.posts.length > 0}
<Pageination
page={data.page}
cursor={{ next: data.posts.cursor.next }}
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
on:cursor={(c) => {
searchParam($page.url, 'cursor', c.detail)
}}
>
<span class="flex flex-row items-center gap-1">
<Icon src={ChartBar} size="16" mini />
<span class="font-medium text-black dark:text-white">
{data.community.community_view.counts.users_active_day}
</span>
<span class="font-normal">
Active user{data.community.community_view.counts.users_active_day >
1
? 's'
: ''}
</span>
{$t('routes.frontpage.footer', {
// @ts-ignore
users: $site?.site_view.counts.users_active_day ?? '??',
})}
</span>
{/if}
</Pageination>
</Pageination>
</svelte:element>
</div>
</div>
+11 -7
View File
@@ -1,4 +1,5 @@
import { client } from '$lib/lemmy.js'
import { postFeed } from '$lib/lemmy/postfeed.js'
import { userSettings } from '$lib/settings.js'
import type { SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
@@ -16,13 +17,16 @@ export async function load({ params, fetch, url }) {
return {
sort: sort,
page: page || 1,
posts: await client({ func: fetch }).getPosts({
limit: 40,
community_name: params.name,
page: page,
sort: sort,
page_cursor: cursor,
show_hidden: get(userSettings).posts.showHidden,
posts: await postFeed({
id: 'community',
url: url,
request: {
page: page,
page_cursor: cursor,
sort: sort,
limit: 20,
community_name: params.name,
}
}),
}
}