diff --git a/bun.lockb b/bun.lockb index 5e590359..bc83e7c5 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/lib/components/lemmy/post/feed/PostFeed.svelte b/src/lib/components/lemmy/post/feed/PostFeed.svelte index 25fcf538..9ce82f95 100644 --- a/src/lib/components/lemmy/post/feed/PostFeed.svelte +++ b/src/lib/components/lemmy/post/feed/PostFeed.svelte @@ -12,65 +12,11 @@ import { createWindowVirtualizer } from '@tanstack/svelte-virtual' import { afterNavigate, beforeNavigate } from '$app/navigation' import { _posts } from '../../../../../routes/+page.svelte' - - type PostViewWithCrossposts = PostView & { - withCrossposts: true - crossposts: PostView[] - } - type PostViewWithoutCrossposts = PostView & { withCrossposts?: false } + import { combineCrossposts } from './crosspost' export let posts: PostView[] export let community: boolean = false - const addCrosspostProperty = ( - post: PostView, - crossposts: PostView[] - ): PostViewWithCrossposts => ({ - ...post, - crossposts: crossposts, - withCrossposts: true, - }) - - const combineCrossposts = ( - posts: PostView[] - ): (PostViewWithCrossposts | PostViewWithoutCrossposts)[] => { - const urlMap = new Map< - string, - PostViewWithCrossposts | PostViewWithoutCrossposts - >() - const results: (PostViewWithCrossposts | PostViewWithoutCrossposts)[] = [] - const seenUrls = new Set() - - posts?.forEach((post) => { - if ( - !post || - ($userSettings.hidePosts.deleted && post.post.deleted) || - ($userSettings.hidePosts.removed && post.post.removed) - ) - return - if (!post?.post?.url) { - results.push(post) - return - } - - let existing = urlMap.get(post.post.url) - if (existing) { - existing.withCrossposts = true - if (existing.withCrossposts) { - existing.crossposts = [...(existing.crossposts || []), post] - } - - urlMap.set(post.post.url, existing) - } else { - urlMap.set(post.post.url, post) - results.push(post) - } - seenUrls.add(post.post.url) - }) - - return results - } - $: combinedPosts = combineCrossposts(posts) let viewPost: number = -1 diff --git a/src/lib/components/lemmy/post/feed/VirtualFeed.svelte b/src/lib/components/lemmy/post/feed/VirtualFeed.svelte index 30b383c0..24cbbeac 100644 --- a/src/lib/components/lemmy/post/feed/VirtualFeed.svelte +++ b/src/lib/components/lemmy/post/feed/VirtualFeed.svelte @@ -12,18 +12,22 @@ import { createWindowVirtualizer } from '@tanstack/svelte-virtual' import { afterNavigate, beforeNavigate } from '$app/navigation' import { _posts } from '../../../../../routes/+page.svelte' + import { combineCrossposts } from './crosspost' export let posts: PostView[] export let community: boolean = false + let combinedPosts = combineCrossposts(posts) + $: combinedPosts = combineCrossposts(posts) + const virtualizer = createWindowVirtualizer({ - count: posts.length, + count: combinedPosts.length, estimateSize: () => 150, }) - $: if (posts.length) + $: if (combinedPosts.length) $virtualizer.setOptions({ - count: posts.length, + count: combinedPosts.length, }) let virtualItemEls: HTMLElement[] = [] @@ -90,15 +94,15 @@ data-index={row.index} class="relative post-container" > - {#if posts[row.index]} + {#if combinedPosts[row.index]} { posts = posts.toSpliced(row.index, 1) diff --git a/src/lib/components/lemmy/post/feed/crosspost.ts b/src/lib/components/lemmy/post/feed/crosspost.ts new file mode 100644 index 00000000..dfe283b9 --- /dev/null +++ b/src/lib/components/lemmy/post/feed/crosspost.ts @@ -0,0 +1,57 @@ +import { userSettings } from "$lib/settings" +import type { PostView } from "lemmy-js-client" +import { get } from "svelte/store" + +export type PostViewWithCrossposts = PostView & { + withCrossposts: true + crossposts: PostView[] +} +export type PostViewWithoutCrossposts = PostView & { withCrossposts?: false } +const addCrosspostProperty = ( + post: PostView, + crossposts: PostView[], +): PostViewWithCrossposts => ({ + ...post, + crossposts: crossposts, + withCrossposts: true, +}) + +export const combineCrossposts = ( + posts: PostView[], +): (PostViewWithCrossposts | PostViewWithoutCrossposts)[] => { + const urlMap = new Map< + string, + PostViewWithCrossposts | PostViewWithoutCrossposts + >() + const results: (PostViewWithCrossposts | PostViewWithoutCrossposts)[] = [] + const seenUrls = new Set() + + posts?.forEach((post) => { + if ( + !post || + (get(userSettings).hidePosts.deleted && post.post.deleted) || + (get(userSettings).hidePosts.removed && post.post.removed) + ) + return + if (!post?.post?.url) { + results.push(post) + return + } + + let existing = urlMap.get(post.post.url) + if (existing) { + existing.withCrossposts = true + if (existing.withCrossposts) { + existing.crossposts = [...(existing.crossposts || []), post] + } + + urlMap.set(post.post.url, existing) + } else { + urlMap.set(post.post.url, post) + results.push(post) + } + seenUrls.add(post.post.url) + }) + + return results +} \ No newline at end of file