feat: add basic infinite scroll
This commit is contained in:
1 parent
005a8c0e07
commit
322874dafa
6 files changed
+57
-26
No files matched your search
@@ -40,6 +40,7 @@
|
|||||||
"svelte": "latest",
|
"svelte": "latest",
|
||||||
"svelte-check": "^3.0.1",
|
"svelte-check": "^3.0.1",
|
||||||
"svelte-hero-icons": "^5.0.0",
|
"svelte-hero-icons": "^5.0.0",
|
||||||
|
"svelte-infinite-scroll": "^2.0.1",
|
||||||
"svelte-tiny-virtual-list": "^2.0.5",
|
"svelte-tiny-virtual-list": "^2.0.5",
|
||||||
"sveltekit-i18n": "^2.4.2",
|
"sveltekit-i18n": "^2.4.2",
|
||||||
"tailwindcss": "^3.3.3",
|
"tailwindcss": "^3.3.3",
|
||||||
|
|||||||
@@ -98,7 +98,7 @@
|
|||||||
</Placeholder>
|
</Placeholder>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{: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)}
|
{#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)}
|
||||||
<li
|
<li
|
||||||
in:fly|global={{
|
in:fly|global={{
|
||||||
@@ -175,4 +175,5 @@
|
|||||||
{/if}
|
{/if}
|
||||||
{/each}
|
{/each}
|
||||||
{/if}
|
{/if}
|
||||||
|
<slot />
|
||||||
</ul>
|
</ul>
|
||||||
+28
-11
@@ -1,26 +1,37 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores'
|
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 Pageination from '$lib/components/ui/Pageination.svelte'
|
||||||
import Sort from '$lib/components/lemmy/dropdowns/Sort.svelte'
|
import Sort from '$lib/components/lemmy/dropdowns/Sort.svelte'
|
||||||
import ViewSelect from '$lib/components/lemmy/dropdowns/ViewSelect.svelte'
|
import ViewSelect from '$lib/components/lemmy/dropdowns/ViewSelect.svelte'
|
||||||
import { searchParam } from '$lib/util.js'
|
import { searchParam } from '$lib/util.js'
|
||||||
import PostFeed from '$lib/components/lemmy/post/PostFeed.svelte'
|
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 { ChartBar, GlobeAmericas, Icon, ServerStack } from 'svelte-hero-icons'
|
||||||
import { profile } from '$lib/auth.js'
|
import { getClient, site } from '$lib/lemmy.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 Location from '$lib/components/lemmy/dropdowns/Location.svelte'
|
import Location from '$lib/components/lemmy/dropdowns/Location.svelte'
|
||||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
import { t } from '$lib/translations.js'
|
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
|
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>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col gap-4 max-w-full w-full min-w-0">
|
<div class="flex flex-col gap-4 max-w-full w-full min-w-0">
|
||||||
@@ -34,7 +45,13 @@
|
|||||||
<ViewSelect />
|
<ViewSelect />
|
||||||
</div>
|
</div>
|
||||||
</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">
|
<div class="mt-auto">
|
||||||
<Pageination
|
<Pageination
|
||||||
page={data.page}
|
page={data.page}
|
||||||
|
|||||||
+23
-12
@@ -1,10 +1,13 @@
|
|||||||
import { getClient } from '$lib/lemmy.js'
|
import { getClient } from '$lib/lemmy.js'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
import { get, writable } from 'svelte/store'
|
||||||
import { error } from '@sveltejs/kit'
|
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 }) {
|
export async function load({ url, fetch }) {
|
||||||
const cursor = url.searchParams.get('cursor') as string | undefined
|
const cursor = url.searchParams.get('cursor') as string | undefined
|
||||||
@@ -18,14 +21,22 @@ export async function load({ url, fetch }) {
|
|||||||
const listingType: ListingType =
|
const listingType: ListingType =
|
||||||
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
||||||
|
|
||||||
const posts = await getClient(undefined, fetch).getPosts({
|
const cached = get(_posts)
|
||||||
limit: 20,
|
|
||||||
page: page,
|
const posts =
|
||||||
sort: sort,
|
cached && url.searchParams.toString() == cached.params.toString()
|
||||||
type_: listingType,
|
? cached.data
|
||||||
page_cursor: cursor,
|
: await getClient(undefined, fetch).getPosts({
|
||||||
show_hidden: settings.posts.showHidden,
|
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 {
|
try {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { getClient } from '$lib/lemmy.js'
|
|||||||
import { awaitIfServer } from '$lib/promise.js'
|
import { awaitIfServer } from '$lib/promise.js'
|
||||||
import { SSR_ENABLED, userSettings } from '$lib/settings.js'
|
import { SSR_ENABLED, userSettings } from '$lib/settings.js'
|
||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
|
import type { GetComments } from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
export async function load({ params, url, fetch }) {
|
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 sort = get(userSettings)?.defaultSort?.comments ?? 'Hot'
|
||||||
|
|
||||||
const commentParams: any = {
|
const commentParams: GetComments = {
|
||||||
post_id: Number(params.id),
|
post_id: Number(params.id),
|
||||||
type_: 'All',
|
type_: 'All',
|
||||||
limit: 50,
|
limit: 50,
|
||||||
@@ -47,10 +48,10 @@ export async function load({ params, url, fetch }) {
|
|||||||
parent_id: parentId,
|
parent_id: parentId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const comments = getClient(params.instance, fetch).getComments(commentParams)
|
||||||
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
const post = await getClient(params.instance.toLowerCase(), fetch).getPost({
|
||||||
id: Number(params.id),
|
id: Number(params.id),
|
||||||
})
|
})
|
||||||
const comments = getClient(params.instance, fetch).getComments(commentParams)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
thread: {
|
thread: {
|
||||||
|
|||||||
Reference in new issue
Block a user