fix: combine crossposts on virtualfeede
This commit is contained in:
1 parent
49c7e46081
commit
bb7c5f7ae6
4 files changed
+69
-62
No files matched your search
@@ -12,65 +12,11 @@
|
|||||||
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 { _posts } from '../../../../../routes/+page.svelte'
|
||||||
|
import { combineCrossposts } from './crosspost'
|
||||||
type PostViewWithCrossposts = PostView & {
|
|
||||||
withCrossposts: true
|
|
||||||
crossposts: PostView[]
|
|
||||||
}
|
|
||||||
type PostViewWithoutCrossposts = PostView & { withCrossposts?: false }
|
|
||||||
|
|
||||||
export let posts: PostView[]
|
export let posts: PostView[]
|
||||||
export let community: boolean = false
|
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)
|
$: combinedPosts = combineCrossposts(posts)
|
||||||
|
|
||||||
let viewPost: number = -1
|
let viewPost: number = -1
|
||||||
|
|||||||
@@ -12,18 +12,22 @@
|
|||||||
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 { _posts } from '../../../../../routes/+page.svelte'
|
||||||
|
import { combineCrossposts } from './crosspost'
|
||||||
|
|
||||||
export let posts: PostView[]
|
export let posts: PostView[]
|
||||||
export let community: boolean = false
|
export let community: boolean = false
|
||||||
|
|
||||||
|
let combinedPosts = combineCrossposts(posts)
|
||||||
|
$: combinedPosts = combineCrossposts(posts)
|
||||||
|
|
||||||
const virtualizer = createWindowVirtualizer({
|
const virtualizer = createWindowVirtualizer({
|
||||||
count: posts.length,
|
count: combinedPosts.length,
|
||||||
estimateSize: () => 150,
|
estimateSize: () => 150,
|
||||||
})
|
})
|
||||||
|
|
||||||
$: if (posts.length)
|
$: if (combinedPosts.length)
|
||||||
$virtualizer.setOptions({
|
$virtualizer.setOptions({
|
||||||
count: posts.length,
|
count: combinedPosts.length,
|
||||||
})
|
})
|
||||||
|
|
||||||
let virtualItemEls: HTMLElement[] = []
|
let virtualItemEls: HTMLElement[] = []
|
||||||
@@ -90,15 +94,15 @@
|
|||||||
data-index={row.index}
|
data-index={row.index}
|
||||||
class="relative post-container"
|
class="relative post-container"
|
||||||
>
|
>
|
||||||
{#if posts[row.index]}
|
{#if combinedPosts[row.index]}
|
||||||
<Post
|
<Post
|
||||||
hideCommunity={community}
|
hideCommunity={community}
|
||||||
view={(posts[row.index]?.post.featured_community ||
|
view={(combinedPosts[row.index]?.post.featured_community ||
|
||||||
posts[row.index]?.post.featured_local) &&
|
combinedPosts[row.index]?.post.featured_local) &&
|
||||||
$userSettings.posts.compactFeatured
|
$userSettings.posts.compactFeatured
|
||||||
? 'compact'
|
? 'compact'
|
||||||
: $userSettings.view}
|
: $userSettings.view}
|
||||||
post={posts?.[row.index]}
|
post={combinedPosts?.[row.index]}
|
||||||
class="transition-all duration-250"
|
class="transition-all duration-250"
|
||||||
on:hide={() => {
|
on:hide={() => {
|
||||||
posts = posts.toSpliced(row.index, 1)
|
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
|
||||||
|
}
|
||||||
Reference in new issue
Block a user