feat: add setting for infinite scroll

This commit is contained in:
Xylight committed 2024-07-04 13:40:33 -07:00
1 parent 82e3c8efd6
commit ef74a4e6d2
5 files changed
+26 -8

No files matched your search

+4
View File
@@ -594,6 +594,10 @@
"title": "Default sort", "title": "Default sort",
"description": "The default sorting to use in feeds." "description": "The default sorting to use in feeds."
}, },
"infiniteScroll": {
"title": "Infinite scroll",
"description": "New posts are added as you scroll down, without having to click 'next'."
},
"postsInNewTab": { "postsInNewTab": {
"title": "Open posts in new tab", "title": "Open posts in new tab",
"description": "Open posts in a separate tab instead of the current." "description": "Open posts in a separate tab instead of the current."
+2
View File
@@ -88,6 +88,7 @@ interface Settings {
compactFeatured: boolean compactFeatured: boolean
showHidden: boolean showHidden: boolean
} }
infiniteScroll: boolean
language: string | null language: string | null
translator: string | undefined translator: string | undefined
} }
@@ -155,6 +156,7 @@ export const defaultSettings: Settings = {
compactFeatured: toBool(env.PUBLIC_COMPACT_FEATURED) ?? true, compactFeatured: toBool(env.PUBLIC_COMPACT_FEATURED) ?? true,
showHidden: false, showHidden: false,
}, },
infiniteScroll: true,
language: env.PUBLIC_LANGUAGE ?? null, language: env.PUBLIC_LANGUAGE ?? null,
translator: env.PUBLIC_TRANSLATOR ?? undefined, translator: env.PUBLIC_TRANSLATOR ?? undefined,
} }
+12 -8
View File
@@ -21,11 +21,12 @@
import { Spinner } from 'mono-svelte' import { Spinner } from 'mono-svelte'
import { writable } from 'svelte/store' import { writable } from 'svelte/store'
import type { GetPostsResponse } from 'lemmy-js-client' import type { GetPostsResponse } from 'lemmy-js-client'
import { userSettings } from '$lib/settings.js'
export let data export let data
const limit = 20 const limit = 20
let hasMore = data.posts.posts.length == limit let hasMore = data.posts.posts.length != 0
async function loadMore() { async function loadMore() {
if (!hasMore) return if (!hasMore) return
@@ -38,14 +39,12 @@
page_cursor: data.cursor.next, page_cursor: data.cursor.next,
}) })
hasMore = newPosts.posts.length == limit hasMore = newPosts.posts.length != 0
console.log(hasMore)
data.cursor.next = newPosts.next_page data.cursor.next = newPosts.next_page
data.posts.posts = [...data.posts.posts, ...newPosts.posts] data.posts.posts = [...data.posts.posts, ...newPosts.posts]
_posts.set({ data: data.posts, params: $page.url.searchParams }) _posts.update((ps) => ({ data: data.posts, params: ps.params }))
} }
</script> </script>
@@ -62,6 +61,7 @@
</div> </div>
<PostFeed posts={data.posts.posts}> <PostFeed posts={data.posts.posts}>
{#if $userSettings.infiniteScroll}
{#if hasMore} {#if hasMore}
<div class="w-full flex flex-col skeleton gap-2 animate-pulse pt-6"> <div class="w-full flex flex-col skeleton gap-2 animate-pulse pt-6">
<div class="w-96 h-8"></div> <div class="w-96 h-8"></div>
@@ -72,9 +72,13 @@
</div> </div>
</div> </div>
{/if} {/if}
<InfiniteScroll window threshold={750} on:loadMore={loadMore} /> <InfiniteScroll window threshold={1000} on:loadMore={loadMore} />
{/if}
</PostFeed> </PostFeed>
<div class="mt-auto"> <svelte:element
this={$userSettings.infiniteScroll ? 'noscript' : 'div'}
class="mt-auto"
>
<Pageination <Pageination
page={data.page} page={data.page}
cursor={{ next: data.cursor.next, back: data.cursor.back }} cursor={{ next: data.cursor.next, back: data.cursor.back }}
@@ -91,7 +95,7 @@
})} })}
</span> </span>
</Pageination> </Pageination>
</div> </svelte:element>
</div> </div>
<style lang="postcss"> <style lang="postcss">
+3
View File
@@ -20,6 +20,9 @@ export async function load({ url, fetch }) {
const cached = get(_posts) const cached = get(_posts)
console.log(url.searchParams.toString())
console.log(cached?.params?.toString())
const posts = const posts =
cached && url.searchParams.toString() == cached.params.toString() cached && url.searchParams.toString() == cached.params.toString()
? cached.data ? cached.data
+5
View File
@@ -214,6 +214,11 @@
</div> </div>
</div> </div>
</Setting> </Setting>
<ToggleSetting
bind:checked={$userSettings.infiniteScroll}
title={$t('settings.app.infiniteScroll.title')}
description={$t('settings.app.infiniteScroll.description')}
/>
<ToggleSetting <ToggleSetting
bind:checked={$userSettings.openLinksInNewTab} bind:checked={$userSettings.openLinksInNewTab}
title={$t('settings.app.postsInNewTab.title')} title={$t('settings.app.postsInNewTab.title')}