feat: add basic infinite scroll

This commit is contained in:
Xylight committed 2024-07-03 11:21:02 -07:00
1 parent 005a8c0e07
commit 322874dafa
6 files changed
+57 -26

No files matched your search

@@ -98,7 +98,7 @@
</Placeholder>
</div>
{:else}
{#each combinedPosts as post, index (post.post.id)}
{#each combinedPosts as post, index}
{#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)}
<li
in:fly|global={{
@@ -175,4 +175,5 @@
{/if}
{/each}
{/if}
<slot />
</ul>
+28 -11
View File
@@ -1,26 +1,37 @@
<script lang="ts">
import { page } from '$app/stores'
import StickyCard from '$lib/components/ui/StickyCard.svelte'
import SiteCard from '$lib/components/lemmy/SiteCard.svelte'
import Pageination from '$lib/components/ui/Pageination.svelte'
import Sort from '$lib/components/lemmy/dropdowns/Sort.svelte'
import ViewSelect from '$lib/components/lemmy/dropdowns/ViewSelect.svelte'
import { searchParam } from '$lib/util.js'
import PostFeed from '$lib/components/lemmy/post/PostFeed.svelte'
import { Button, Modal, Select, Spinner } from 'mono-svelte'
import { ChartBar, GlobeAmericas, Icon, ServerStack } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js'
import { site } from '$lib/lemmy.js'
import { amModOfAny } from '$lib/components/lemmy/moderation/moderation.js'
import { feature } from '$lib/version.js'
import { userSettings } from '$lib/settings.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { getClient, site } from '$lib/lemmy.js'
import Location from '$lib/components/lemmy/dropdowns/Location.svelte'
import Header from '$lib/components/ui/layout/pages/Header.svelte'
import { t } from '$lib/translations.js'
import { enhance } from '$app/forms'
import InfiniteScroll from 'svelte-infinite-scroll'
import { _posts } from './+page.js'
import { Spinner } from 'mono-svelte'
export let data
async function loadMore() {
console.log('loading more')
const newPosts = await getClient().getPosts({
limit: 20,
page: data.page,
sort: data.sort,
type_: data.listingType,
page_cursor: data.cursor.next,
})
data.cursor.next = newPosts.next_page
data.posts.posts = [...data.posts.posts, ...newPosts.posts]
_posts.set({ data: data.posts, params: $page.url.searchParams })
}
</script>
<div class="flex flex-col gap-4 max-w-full w-full min-w-0">
@@ -34,7 +45,13 @@
<ViewSelect />
</div>
</div>
<PostFeed posts={data.posts.posts} />
<PostFeed posts={data.posts.posts}>
<div class="w-full grid place-items-center h-48">
<Spinner width={32} />
</div>
<InfiniteScroll window threshold={400} on:loadMore={loadMore} />
</PostFeed>
<div class="mt-auto">
<Pageination
page={data.page}
+23 -12
View File
@@ -1,10 +1,13 @@
import { getClient } from '$lib/lemmy.js'
import { userSettings } from '$lib/settings.js'
import type { ListingType, SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { error } from '@sveltejs/kit'
import { profile } from '$lib/auth.js'
import { feature } from '$lib/version.js'
export const _posts = writable<{
data: GetPostsResponse
params: URLSearchParams
}>(undefined)
export async function load({ url, fetch }) {
const cursor = url.searchParams.get('cursor') as string | undefined
@@ -18,14 +21,22 @@ export async function load({ url, fetch }) {
const listingType: ListingType =
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
const posts = await getClient(undefined, fetch).getPosts({
limit: 20,
page: page,
sort: sort,
type_: listingType,
page_cursor: cursor,
show_hidden: settings.posts.showHidden,
})
const cached = get(_posts)
const posts =
cached && url.searchParams.toString() == cached.params.toString()
? cached.data
: await getClient(undefined, fetch).getPosts({
limit: 20,
page: page,
sort: sort,
type_: listingType,
page_cursor: cursor,
show_hidden: settings.posts.showHidden,
})
if (!(cached && url.searchParams.toString() == cached.params.toString()))
_posts.set({ data: posts, params: url.searchParams })
try {
return {
@@ -6,6 +6,7 @@ import { getClient } from '$lib/lemmy.js'
import { awaitIfServer } from '$lib/promise.js'
import { SSR_ENABLED, userSettings } from '$lib/settings.js'
import { error } from '@sveltejs/kit'
import type { GetComments } from 'lemmy-js-client'
import { get } from 'svelte/store'
export async function load({ params, url, fetch }) {
@@ -36,7 +37,7 @@ export async function load({ params, url, fetch }) {
const sort = get(userSettings)?.defaultSort?.comments ?? 'Hot'
const commentParams: any = {
const commentParams: GetComments = {
post_id: Number(params.id),
type_: 'All',
limit: 50,
@@ -47,10 +48,10 @@ export async function load({ params, url, fetch }) {
parent_id: parentId,
}
const comments = getClient(params.instance, fetch).getComments(commentParams)
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
id: Number(params.id),
})
const comments = getClient(params.instance, fetch).getComments(commentParams)
return {
thread: {