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
@@ -7,15 +7,11 @@
|
|||||||
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
|
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
|
||||||
import { expoOut } from 'svelte/easing'
|
import { expoOut } from 'svelte/easing'
|
||||||
import { fly, slide } from 'svelte/transition'
|
import { fly, slide } from 'svelte/transition'
|
||||||
import { browser } from '$app/environment'
|
|
||||||
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
|
|
||||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
|
||||||
import { afterNavigate, beforeNavigate } from '$app/navigation'
|
|
||||||
import { _posts } from '../../../../../routes/+page.svelte'
|
|
||||||
import { combineCrossposts } from './crosspost'
|
import { combineCrossposts } from './crosspost'
|
||||||
|
|
||||||
export let posts: PostView[]
|
export let posts: PostView[]
|
||||||
export let community: boolean = false
|
export let community: boolean = false
|
||||||
|
let etc = $$restProps
|
||||||
|
|
||||||
$: combinedPosts = combineCrossposts(posts)
|
$: combinedPosts = combineCrossposts(posts)
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,40 @@
|
|||||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||||
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
import type { PostView } from 'lemmy-js-client'
|
import type {
|
||||||
|
GetPostsResponse,
|
||||||
|
ListingType,
|
||||||
|
PostView,
|
||||||
|
SortType,
|
||||||
|
} from 'lemmy-js-client'
|
||||||
import { Badge, Button } from 'mono-svelte'
|
import { Badge, Button } from 'mono-svelte'
|
||||||
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
|
import {
|
||||||
|
ArchiveBox,
|
||||||
|
ExclamationTriangle,
|
||||||
|
Icon,
|
||||||
|
Minus,
|
||||||
|
Plus,
|
||||||
|
} from 'svelte-hero-icons'
|
||||||
import { expoOut } from 'svelte/easing'
|
import { expoOut } from 'svelte/easing'
|
||||||
import { fly, slide } from 'svelte/transition'
|
import { fly, slide } from 'svelte/transition'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
|
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
|
||||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||||
import { afterNavigate, beforeNavigate } from '$app/navigation'
|
import { afterNavigate, beforeNavigate } from '$app/navigation'
|
||||||
import { _posts } from '../../../../../routes/+page.svelte'
|
|
||||||
import { combineCrossposts } from './crosspost'
|
import { combineCrossposts } from './crosspost'
|
||||||
|
import { client } from '$lib/lemmy'
|
||||||
|
import {
|
||||||
|
postFeeds,
|
||||||
|
type PostFeed,
|
||||||
|
type PostFeedID,
|
||||||
|
} from '$lib/lemmy/postfeed'
|
||||||
|
import { t } from '$lib/translations'
|
||||||
|
import InfiniteScroll from 'svelte-infinite-scroll'
|
||||||
|
|
||||||
export let posts: PostView[]
|
export let posts: PostView[]
|
||||||
export let community: boolean = false
|
export let community: boolean = false
|
||||||
|
export let lastSeen: number | undefined = undefined
|
||||||
|
export let feedId: PostFeedID
|
||||||
|
|
||||||
let combinedPosts = combineCrossposts(posts)
|
let combinedPosts = combineCrossposts(posts)
|
||||||
$: combinedPosts = combineCrossposts(posts)
|
$: combinedPosts = combineCrossposts(posts)
|
||||||
@@ -39,7 +59,104 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
afterNavigate(() => {
|
afterNavigate(() => {
|
||||||
$virtualizer.scrollToIndex($_posts?.lastSeen)
|
$virtualizer.scrollToIndex(lastSeen ?? 0, { align: 'auto' })
|
||||||
|
})
|
||||||
|
|
||||||
|
export let feedData: PostFeed['data']
|
||||||
|
|
||||||
|
const limit = 20
|
||||||
|
let error: any = undefined
|
||||||
|
let loading = false
|
||||||
|
let hasMore = true
|
||||||
|
|
||||||
|
async function loadMore() {
|
||||||
|
if (!hasMore || loading) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
loading = true
|
||||||
|
|
||||||
|
const newPosts = await client()
|
||||||
|
.getPosts({
|
||||||
|
page_cursor: feedData.cursor.next,
|
||||||
|
disliked_only: feedData.disliked_only,
|
||||||
|
liked_only: feedData.liked_only,
|
||||||
|
community_id: feedData.community_id,
|
||||||
|
community_name: feedData.community_name,
|
||||||
|
limit: feedData.limit,
|
||||||
|
page: feedData.page,
|
||||||
|
saved_only: feedData.saved_only,
|
||||||
|
show_hidden: feedData.show_hidden,
|
||||||
|
sort: feedData.sort,
|
||||||
|
type_: feedData.type_,
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
throw new Error(e)
|
||||||
|
})
|
||||||
|
|
||||||
|
error = null
|
||||||
|
|
||||||
|
hasMore = newPosts.posts.length != 0
|
||||||
|
|
||||||
|
feedData.cursor.next = newPosts.next_page
|
||||||
|
feedData.posts.posts = [...feedData.posts.posts, ...newPosts.posts]
|
||||||
|
|
||||||
|
postFeeds.updateFeed(feedId, {
|
||||||
|
data: feedData,
|
||||||
|
})
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
$postFeeds.main.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>
|
</script>
|
||||||
|
|
||||||
@@ -114,5 +231,38 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#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}
|
||||||
<slot />
|
<slot />
|
||||||
</ul>
|
</ul>
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
Icon,
|
Icon,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
|
|
||||||
export let page: number
|
export let page: number = 0
|
||||||
export let cursor: { next?: string; back?: string } | undefined = undefined
|
export let cursor: { next?: string; back?: string } | undefined = undefined
|
||||||
|
|
||||||
const dispatcher = createEventDispatcher<{ change: number; cursor: string }>()
|
const dispatcher = createEventDispatcher<{ change: number; cursor: string }>()
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
import { browser } from "$app/environment"
|
||||||
|
import { instance } from "$lib/instance"
|
||||||
|
import { client } from "$lib/lemmy"
|
||||||
|
import type { GetPosts, GetPostsResponse, ListingType, SortType } from "lemmy-js-client"
|
||||||
|
import { get, writable } from "svelte/store"
|
||||||
|
|
||||||
|
export const shouldReload = (
|
||||||
|
cache: PostFeed | undefined,
|
||||||
|
url: URL,
|
||||||
|
instance: string,
|
||||||
|
): boolean =>
|
||||||
|
cache?.instance != instance || cache?.url.toString() != url.toString()
|
||||||
|
|
||||||
|
interface FetchPostFeed extends GetPosts {
|
||||||
|
sort: SortType
|
||||||
|
type_?: ListingType
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function postFeed(args: {
|
||||||
|
id: PostFeedID
|
||||||
|
request: FetchPostFeed,
|
||||||
|
url: URL
|
||||||
|
}) {
|
||||||
|
const feed = get(postFeeds)[args.id]
|
||||||
|
|
||||||
|
const posts = shouldReload(feed, args.url, get(instance))
|
||||||
|
? await client().getPosts(args.request)
|
||||||
|
: feed.data.posts
|
||||||
|
|
||||||
|
if (
|
||||||
|
shouldReload(feed, args.url, get(instance)) &&
|
||||||
|
browser
|
||||||
|
)
|
||||||
|
postFeeds.updateFeed(args.id, {
|
||||||
|
data: {
|
||||||
|
...args.request,
|
||||||
|
posts: posts,
|
||||||
|
cursor: {
|
||||||
|
next: posts.next_page
|
||||||
|
},
|
||||||
|
type_: args.request.type_
|
||||||
|
},
|
||||||
|
url: args.url,
|
||||||
|
lastSeen: 0,
|
||||||
|
instance: get(instance),
|
||||||
|
})
|
||||||
|
|
||||||
|
return get(postFeeds)[args.id].data
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPostFeed(feeds: Map<PostFeedID, PostFeed>, id: PostFeedID) {
|
||||||
|
return feeds.get(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
function createPostFeedsStore() {
|
||||||
|
// @ts-ignore
|
||||||
|
const { subscribe, update, set } = writable<Record<PostFeedID, PostFeed>>({});
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
addFeed: (id: PostFeedID, feed: PostFeed) => update(feeds => ({ ...feeds, [id]: feed })),
|
||||||
|
updateFeed: (id: PostFeedID, feed: Partial<PostFeed>) => update(feeds => {
|
||||||
|
return {
|
||||||
|
...feeds,
|
||||||
|
[id]: { ...feeds[id], ...feed } as PostFeed
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
removeFeed: (id: PostFeedID) => update(feeds => {
|
||||||
|
const { [id]: _, ...rest } = feeds;
|
||||||
|
return rest as Record<PostFeedID, PostFeed>;
|
||||||
|
}),
|
||||||
|
clearAll: () => set({} as Record<PostFeedID, PostFeed>),
|
||||||
|
set
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PostFeedID = 'main' | 'community' | 'profile'
|
||||||
|
|
||||||
|
export interface PostFeed {
|
||||||
|
data: GetPosts & {
|
||||||
|
posts: GetPostsResponse,
|
||||||
|
cursor: {
|
||||||
|
next?: string
|
||||||
|
},
|
||||||
|
sort: SortType,
|
||||||
|
}
|
||||||
|
url: URL
|
||||||
|
lastSeen: number
|
||||||
|
instance: string
|
||||||
|
id: PostFeedID
|
||||||
|
}
|
||||||
|
|
||||||
|
export let postFeeds = createPostFeedsStore()
|
||||||
+8
-150
@@ -1,27 +1,4 @@
|
|||||||
<script lang="ts" context="module">
|
<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>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
@@ -44,106 +21,18 @@
|
|||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
|
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
|
||||||
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
||||||
|
import { postFeeds, getPostFeed } from '$lib/lemmy/postfeed.js'
|
||||||
|
|
||||||
export let data
|
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>
|
</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">
|
||||||
<header class="flex flex-col gap-4 relative">
|
<header class="flex flex-col gap-4 relative">
|
||||||
<Header>{$t('routes.frontpage.title')}</Header>
|
<Header>{$t('routes.frontpage.title')}</Header>
|
||||||
<div class="flex items-center gap-2">
|
<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} />
|
<Sort changeDefault selected={data.sort} />
|
||||||
<ViewSelect />
|
<ViewSelect />
|
||||||
</div>
|
</div>
|
||||||
@@ -152,46 +41,15 @@
|
|||||||
<svelte:component
|
<svelte:component
|
||||||
this={browser ? VirtualFeed : PostFeed}
|
this={browser ? VirtualFeed : PostFeed}
|
||||||
posts={data.posts.posts}
|
posts={data.posts.posts}
|
||||||
>
|
bind:feedData={data}
|
||||||
{#if $userSettings.infiniteScroll && browser}
|
lastSeen={$postFeeds.main.lastSeen}
|
||||||
{#if error}
|
feedId="main"
|
||||||
<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>
|
|
||||||
<svelte:element
|
<svelte:element
|
||||||
this={$userSettings.infiniteScroll ? 'noscript' : 'div'}
|
this={$userSettings.infiniteScroll ? 'noscript' : 'div'}
|
||||||
class="mt-auto"
|
class="mt-auto"
|
||||||
>
|
>
|
||||||
<Pageination
|
<Pageination
|
||||||
page={data.page}
|
|
||||||
cursor={{ next: data.cursor.next }}
|
cursor={{ next: data.cursor.next }}
|
||||||
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
|
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
|
||||||
on:cursor={(c) => {
|
on:cursor={(c) => {
|
||||||
|
|||||||
+12
-52
@@ -3,14 +3,13 @@ import { userSettings } from '$lib/settings.js'
|
|||||||
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
|
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
|
||||||
import { get, writable } from 'svelte/store'
|
import { get, writable } from 'svelte/store'
|
||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
import { _posts, shouldReload, type CachedPosts } from './+page.svelte'
|
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { instance } from '$lib/instance'
|
import { instance } from '$lib/instance'
|
||||||
|
import { postFeed, postFeeds, shouldReload } from '$lib/lemmy/postfeed.js'
|
||||||
|
|
||||||
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
|
||||||
const prevCursor = url.searchParams.get('prevCursor') as string | undefined
|
const prevCursor = url.searchParams.get('prevCursor') as string | undefined
|
||||||
const page = Number(url.searchParams.get('page')) || undefined
|
|
||||||
|
|
||||||
const settings = get(userSettings)
|
const settings = get(userSettings)
|
||||||
|
|
||||||
@@ -19,55 +18,16 @@ 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 cached: CachedPosts | undefined = get(_posts)
|
|
||||||
|
|
||||||
const posts = shouldReload(cached, url.searchParams.toString(), get(instance))
|
return await postFeed({
|
||||||
? await getClient(undefined, fetch).getPosts({
|
id: 'main',
|
||||||
limit: 20,
|
request: {
|
||||||
page: page,
|
page_cursor: cursor,
|
||||||
sort: sort,
|
sort: sort,
|
||||||
type_: listingType,
|
type_: listingType,
|
||||||
page_cursor: cursor,
|
limit: 20,
|
||||||
show_hidden: settings.posts.showHidden,
|
show_hidden: settings.posts.showHidden
|
||||||
})
|
},
|
||||||
: cached.data.posts
|
url: url
|
||||||
|
})
|
||||||
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.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
import { onDestroy, onMount } from 'svelte'
|
import { onDestroy, onMount } from 'svelte'
|
||||||
import { setSessionStorage } from '$lib/session.js'
|
import { setSessionStorage } from '$lib/session.js'
|
||||||
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
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 { Button, Modal, toast } from 'mono-svelte'
|
||||||
import { afterNavigate } from '$app/navigation'
|
import { afterNavigate } from '$app/navigation'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
@@ -22,6 +23,9 @@
|
|||||||
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||||
import { profile } from '$lib/auth'
|
import { profile } from '$lib/auth'
|
||||||
import { t } from '$lib/translations.js'
|
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
|
export let data
|
||||||
|
|
||||||
@@ -31,7 +35,7 @@
|
|||||||
if (browser)
|
if (browser)
|
||||||
setSessionStorage(
|
setSessionStorage(
|
||||||
'lastSeenCommunity',
|
'lastSeenCommunity',
|
||||||
data.community.community_view.community
|
data.community.community_view.community,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -81,7 +85,7 @@
|
|||||||
`!${data.community.community_view.community.name}@${
|
`!${data.community.community_view.community.name}@${
|
||||||
new URL(data.community.community_view.community.actor_id)
|
new URL(data.community.community_view.community.actor_id)
|
||||||
.hostname
|
.hostname
|
||||||
}`
|
}`,
|
||||||
)
|
)
|
||||||
|
|
||||||
toast({ content: $t('toast.copied') })
|
toast({ content: $t('toast.copied') })
|
||||||
@@ -89,7 +93,7 @@
|
|||||||
class="dark:text-zinc-400 text-slate-600 text-sm text-left"
|
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.name}@{new URL(
|
||||||
data.community.community_view.community.actor_id
|
data.community.community_view.community.actor_id,
|
||||||
).hostname}
|
).hostname}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -102,7 +106,7 @@
|
|||||||
color="secondary"
|
color="secondary"
|
||||||
href="/c/{fullCommunityName(
|
href="/c/{fullCommunityName(
|
||||||
data.community.community_view.community.name,
|
data.community.community_view.community.name,
|
||||||
data.community.community_view.community.actor_id
|
data.community.community_view.community.actor_id,
|
||||||
)}/settings"
|
)}/settings"
|
||||||
>
|
>
|
||||||
<Icon src={Cog6Tooth} size="16" mini />
|
<Icon src={Cog6Tooth} size="16" mini />
|
||||||
@@ -132,30 +136,36 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</Placeholder>
|
</Placeholder>
|
||||||
{:else}
|
{: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}
|
{/if}
|
||||||
|
|
||||||
<Pageination
|
<svelte:element
|
||||||
page={data.page}
|
this={$userSettings.infiniteScroll ? 'noscript' : 'div'}
|
||||||
cursor={{ next: data.posts.next_page }}
|
class="mt-auto"
|
||||||
on:cursor={(c) => searchParam($page.url, 'cursor', c.detail)}
|
|
||||||
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
|
|
||||||
>
|
>
|
||||||
{#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">
|
<span class="flex flex-row items-center gap-1">
|
||||||
<Icon src={ChartBar} size="16" mini />
|
<Icon src={ChartBar} size="16" mini />
|
||||||
<span class="font-medium text-black dark:text-white">
|
{$t('routes.frontpage.footer', {
|
||||||
{data.community.community_view.counts.users_active_day}
|
// @ts-ignore
|
||||||
</span>
|
users: $site?.site_view.counts.users_active_day ?? '??',
|
||||||
<span class="font-normal">
|
})}
|
||||||
Active user{data.community.community_view.counts.users_active_day >
|
|
||||||
1
|
|
||||||
? 's'
|
|
||||||
: ''}
|
|
||||||
</span>
|
|
||||||
</span>
|
</span>
|
||||||
{/if}
|
</Pageination>
|
||||||
</Pageination>
|
</svelte:element>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { client } from '$lib/lemmy.js'
|
import { client } from '$lib/lemmy.js'
|
||||||
|
import { postFeed } from '$lib/lemmy/postfeed.js'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
import type { SortType } from 'lemmy-js-client'
|
import type { SortType } from 'lemmy-js-client'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
@@ -16,13 +17,16 @@ export async function load({ params, fetch, url }) {
|
|||||||
return {
|
return {
|
||||||
sort: sort,
|
sort: sort,
|
||||||
page: page || 1,
|
page: page || 1,
|
||||||
posts: await client({ func: fetch }).getPosts({
|
posts: await postFeed({
|
||||||
limit: 40,
|
id: 'community',
|
||||||
community_name: params.name,
|
url: url,
|
||||||
page: page,
|
request: {
|
||||||
sort: sort,
|
page: page,
|
||||||
page_cursor: cursor,
|
page_cursor: cursor,
|
||||||
show_hidden: get(userSettings).posts.showHidden,
|
sort: sort,
|
||||||
|
limit: 20,
|
||||||
|
community_name: params.name,
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user