fix: combine crossposts on virtualfeede

This commit is contained in:
Xylight committed 2024-07-16 13:15:47 -07:00
1 parent 49c7e46081
commit bb7c5f7ae6
4 files changed
+69 -62

No files matched your search

BIN
View File
Binary file not shown.
@@ -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<string>()
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
@@ -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]}
<Post
hideCommunity={community}
view={(posts[row.index]?.post.featured_community ||
posts[row.index]?.post.featured_local) &&
view={(combinedPosts[row.index]?.post.featured_community ||
combinedPosts[row.index]?.post.featured_local) &&
$userSettings.posts.compactFeatured
? 'compact'
: $userSettings.view}
post={posts?.[row.index]}
post={combinedPosts?.[row.index]}
class="transition-all duration-250"
on:hide={() => {
posts = posts.toSpliced(row.index, 1)
@@ -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<string>()
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
}