feat: rewrite post feed fetching
This commit is contained in:
1 parent
1a30bcee1d
commit
4e0d79ddef
8 files changed
+312
-241
No files matched your search
+8
-150
@@ -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) => {
|
||||
|
||||
Reference in new issue
Block a user