cleanup: switch to state-based post feeds

This commit is contained in:
James Xylight committed 2025-03-08 13:06:17 -08:00
1 parent 0236928bb9
commit 897a3c363c
10 files changed
+79 -85

No files matched your search

@@ -56,11 +56,6 @@
open = $bindable(true),
}: Props = $props()
// TODO this kinda sucks because you cant bind:, but we need to save the open state for the virtualizer
$effect(() => {
node.expanded = open
})
let editing = $state(false)
let newComment = $state(node.comment_view.comment.content)
let editingLoad = $state(false)
@@ -121,6 +121,7 @@
node.comment_view.counts.child_count > 0
? 'border-l'
: ''} ml-2.5 border-slate-200 dark:border-zinc-800 pl-2.5 "
bind:open={() => node.expanded, (value) => (node.expanded = value)}
>
{#if node.children?.length > 0}
<Comments {post} nodes={nodes[index].children} isParent={false} />
@@ -119,7 +119,7 @@
{style}
>
<PostVote
post={post.post}
bind:post={post.post}
vote={post.my_vote}
score={post.counts.score}
upvotes={post.counts.upvotes}
@@ -40,7 +40,7 @@
}
let {
post,
post = $bindable(),
vote = $bindable(0),
score = $bindable(),
upvotes = $bindable(0),
@@ -11,7 +11,7 @@
postFeeds,
type PostFeed,
type PostFeedID,
} from '$lib/lemmy/postfeed'
} from '$lib/lemmy/postfeed.svelte'
import { settings } from '$lib/settings.svelte.js'
import { t } from '$lib/translations'
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
@@ -31,7 +31,7 @@
let virtualListEl: HTMLElement | undefined = $state(undefined)
afterNavigate(() => {
$virtualizer.scrollToIndex($postFeeds[feedId]?.lastSeen ?? 0)
$virtualizer.scrollToIndex(postFeeds.value[feedId]?.lastSeen ?? 0)
})
interface Props {
@@ -94,9 +94,7 @@
feedData.cursor.next = newPosts.next_page
feedData.posts.posts = [...feedData.posts.posts, ...newPosts.posts]
postFeeds.updateFeed(feedId, {
data: feedData,
})
postFeeds.value[feedId].data = feedData
loading = false
} catch (e) {
@@ -114,9 +112,8 @@
if (!id) return
postFeeds.updateFeed(feedId, {
lastSeen: Number(id),
})
postFeeds.value[feedId].lastSeen = Number(id)
observer.unobserve(element)
})
}
@@ -164,10 +161,12 @@
})
let items = $derived($virtualizer.getVirtualItems())
$effect(() => {
if (virtualItemEls.length)
virtualItemEls.forEach($virtualizer.measureElement)
})
$effect(() => {
if (posts.length && virtualListEl)
$virtualizer.setOptions({
@@ -209,30 +208,28 @@
id="feed"
>
{#each items as row, index (posts[row.index]?.post.id)}
{#if posts[row.index]}
<li
bind:this={virtualItemEls[index]}
data-index={row.index}
style={row.index < 7 ? `--anim-delay: ${index * 100}ms` : ''}
class="relative post-container {row.index < 7
? 'pop-in opacity-0'
: ''} -mx-4 sm:-mx-6 px-4 sm:px-6"
>
<Post
bind:post={posts[row.index]}
hideCommunity={community}
view={(posts[row.index]?.post.featured_community ||
posts[row.index]?.post.featured_local) &&
settings.posts.compactFeatured
? 'compact'
: settings.view}
class="transition-all duration-250"
onhide={() => {
posts = posts.toSpliced(row.index, 1)
}}
></Post>
</li>
{/if}
<li
bind:this={virtualItemEls[index]}
data-index={row.index}
style={row.index < 7 ? `--anim-delay: ${index * 100}ms` : ''}
class="relative post-container {row.index < 7
? 'pop-in opacity-0'
: ''} -mx-4 sm:-mx-6 px-4 sm:px-6"
>
<Post
bind:post={posts[row.index]}
hideCommunity={community}
view={(posts[row.index]?.post.featured_community ||
posts[row.index]?.post.featured_local) &&
settings.posts.compactFeatured
? 'compact'
: settings.view}
class="transition-all duration-250"
onhide={() => {
posts = posts.toSpliced(row.index, 1)
}}
></Post>
</li>
{/each}
</div>
</div>
@@ -7,7 +7,6 @@ import type {
ListingType,
SortType,
} from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
export const shouldReload = (
cache: PostFeed | undefined,
@@ -30,14 +29,15 @@ export async function postFeed(args: {
init?: RequestInit | undefined,
) => Promise<Response>
}) {
const feed = get(postFeeds)[args.id]
const feed = postFeeds.value[args.id]
const posts = shouldReload(feed, args.url, instance.data)
? await client({ func: args.fetch }).getPosts(args.request)
: feed.data.posts
if (shouldReload(feed, args.url, instance.data) && browser)
postFeeds.updateFeed(args.id, {
postFeeds.value[args.id] = {
id: args.id,
data: {
...args.request,
posts: posts,
@@ -49,10 +49,10 @@ export async function postFeed(args: {
url: args.url,
lastSeen: 0,
instance: instance.data,
})
}
return (
get(postFeeds)[args.id]?.data ?? {
postFeeds.value[args.id]?.data ?? {
...args.request,
posts: posts,
cursor: {
@@ -67,28 +67,18 @@ 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>>({})
class PostFeedState {
#value: Record<PostFeedID, PostFeed> = $state({}) as 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,
get value() {
return this.#value
}
set value(value) {
this.#value = value
}
}
@@ -108,4 +98,4 @@ export interface PostFeed {
id: PostFeedID
}
export let postFeeds = createPostFeedsStore()
export let postFeeds = new PostFeedState()
+8 -4
View File
@@ -19,8 +19,8 @@
let { data = $bindable() } = $props()
let type = $state(data.type_)
let sort = $state(data.sort)
let type = $state(data.feed.value.type_)
let sort = $state(data.feed.value.sort)
$effect(() => {
if (type) settings.defaultSort.feed = type
@@ -61,7 +61,11 @@
</Header>
</header>
<FeedComponent bind:posts={data.posts.posts} feedData={data} feedId="main" />
<FeedComponent
bind:posts={data.feed.value.posts.posts}
bind:feedData={data.feed.value}
feedId="main"
/>
<svelte:element
this={settings.infiniteScroll && !settings.posts.noVirtualize
? 'noscript'
@@ -69,7 +73,7 @@
class="mt-auto"
>
<Pageination
cursor={{ next: data.cursor.next }}
cursor={{ next: data.feed.value.cursor.next }}
href={(page) =>
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
>
+20 -13
View File
@@ -1,8 +1,13 @@
import { settings } from '$lib/settings.svelte'
import type { GetPostsResponse, ListingType, SortType } from 'lemmy-js-client'
import { postFeed, postFeeds, shouldReload } from '$lib/lemmy/postfeed.js'
import {
postFeed,
postFeeds,
shouldReload,
} from '$lib/lemmy/postfeed.svelte.js'
import { t } from '$lib/translations.js'
import { ChevronDoubleUp } from 'svelte-hero-icons'
import { ReactiveState } from '$lib/promise.svelte.js'
export async function load({ url, fetch }) {
const cursor = url.searchParams.get('cursor') as string | undefined
@@ -14,18 +19,20 @@ export async function load({ url, fetch }) {
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
return {
...(await postFeed({
id: 'main',
request: {
page_cursor: cursor,
sort: sort,
type_: listingType,
limit: 20,
show_hidden: settings.posts.showHidden,
},
url: url,
fetch: fetch,
})),
feed: new ReactiveState(
await postFeed({
id: 'main',
request: {
page_cursor: cursor,
sort: sort,
type_: listingType,
limit: 20,
show_hidden: settings.posts.showHidden,
},
url: url,
fetch: fetch,
}),
),
contextual: {
actions: [
{
+1 -1
View File
@@ -1,4 +1,4 @@
import { postFeed } from '$lib/lemmy/postfeed.js'
import { postFeed } from '$lib/lemmy/postfeed.svelte.js'
import { settings } from '$lib/settings.svelte'
import type { SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
+1 -1
View File
@@ -1,5 +1,5 @@
import { client } from '$lib/lemmy.svelte.js'
import { postFeed } from '$lib/lemmy/postfeed.js'
import { postFeed } from '$lib/lemmy/postfeed.svelte.js'
import { error } from '@sveltejs/kit'
export async function load({ url, params, fetch }) {