Merge pull request #610 from Xyphyn/fix-feed-reactive
fix: updates to posts in a feed don't restore when returning
This commit is contained in:
8 files changed
+72
-48
No files matched your search
@@ -68,11 +68,6 @@
|
|||||||
view != 'compact' &&
|
view != 'compact' &&
|
||||||
type != 'iframe',
|
type != 'iframe',
|
||||||
)
|
)
|
||||||
let hideBody = $derived(
|
|
||||||
settings.posts.deduplicateEmbed &&
|
|
||||||
post.post.embed_description == post.post.body &&
|
|
||||||
view != 'compact',
|
|
||||||
)
|
|
||||||
|
|
||||||
let badges = $derived({
|
let badges = $derived({
|
||||||
deleted: post.post.deleted,
|
deleted: post.post.deleted,
|
||||||
@@ -154,7 +149,7 @@
|
|||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
{/key}
|
{/key}
|
||||||
{#if post.post.body && !post.post.nsfw && view != 'compact' && !hideBody && rule != 'hide'}
|
{#if post.post.body && !post.post.nsfw && view != 'compact'}
|
||||||
<PostBody
|
<PostBody
|
||||||
element="section"
|
element="section"
|
||||||
body={post.post.body}
|
body={post.post.body}
|
||||||
|
|||||||
@@ -112,11 +112,11 @@
|
|||||||
{style}
|
{style}
|
||||||
>
|
>
|
||||||
<PostVote
|
<PostVote
|
||||||
bind:post={post.post}
|
post={post.post}
|
||||||
vote={post.my_vote}
|
bind:vote={post.my_vote}
|
||||||
score={post.counts.score}
|
bind:score={post.counts.score}
|
||||||
upvotes={post.counts.upvotes}
|
bind:upvotes={post.counts.upvotes}
|
||||||
downvotes={post.counts.downvotes}
|
bind:downvotes={post.counts.downvotes}
|
||||||
showCounts={profile.data?.user?.local_user_view?.local_user?.show_scores ??
|
showCounts={profile.data?.user?.local_user_view?.local_user?.show_scores ??
|
||||||
true}
|
true}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -35,15 +35,15 @@
|
|||||||
upvotes?: number
|
upvotes?: number
|
||||||
downvotes?: number
|
downvotes?: number
|
||||||
showCounts?: boolean
|
showCounts?: boolean
|
||||||
children?: import('svelte').Snippet<[{ vote: number; score: number }]>
|
children?: import('svelte').Snippet<[{ vote?: number; score?: number }]>
|
||||||
}
|
}
|
||||||
|
|
||||||
let {
|
let {
|
||||||
post = $bindable(),
|
post = $bindable(),
|
||||||
vote = $bindable(0),
|
vote = $bindable(),
|
||||||
score = $bindable(),
|
score = $bindable(),
|
||||||
upvotes = $bindable(0),
|
upvotes = $bindable(),
|
||||||
downvotes = $bindable(0),
|
downvotes = $bindable(),
|
||||||
showCounts = true,
|
showCounts = true,
|
||||||
children,
|
children,
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
@@ -99,7 +99,7 @@
|
|||||||
out:fly={{ duration: 400, y: 10, easing: backOut }}
|
out:fly={{ duration: 400, y: 10, easing: backOut }}
|
||||||
aria-label={$t('aria.vote.upvotes', { default: upvotes })}
|
aria-label={$t('aria.vote.upvotes', { default: upvotes })}
|
||||||
>
|
>
|
||||||
<FormattedNumber number={upvotes} />
|
<FormattedNumber number={upvotes ?? 0} />
|
||||||
</span>
|
</span>
|
||||||
{/key}
|
{/key}
|
||||||
</span>
|
</span>
|
||||||
@@ -127,7 +127,7 @@
|
|||||||
out:fly={{ duration: 400, y: 10, easing: backOut }}
|
out:fly={{ duration: 400, y: 10, easing: backOut }}
|
||||||
aria-label={$t('aria.vote.downvotes', { default: downvotes })}
|
aria-label={$t('aria.vote.downvotes', { default: downvotes })}
|
||||||
>
|
>
|
||||||
<FormattedNumber number={downvotes} />
|
<FormattedNumber number={downvotes ?? 0} />
|
||||||
</span>
|
</span>
|
||||||
{/key}
|
{/key}
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -50,6 +50,7 @@
|
|||||||
let hasMore = $state(true)
|
let hasMore = $state(true)
|
||||||
|
|
||||||
async function loadMore() {
|
async function loadMore() {
|
||||||
|
console.log(hasMore, loading)
|
||||||
if (!hasMore || loading) return
|
if (!hasMore || loading) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -255,7 +256,14 @@
|
|||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<InfiniteScroll window threshold={1000} on:loadMore={loadMore} />
|
<InfiniteScroll
|
||||||
|
window
|
||||||
|
threshold={600}
|
||||||
|
on:loadMore={() => {
|
||||||
|
console.log('loadMore triggered')
|
||||||
|
loadMore()
|
||||||
|
}}
|
||||||
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
{@render children?.()}
|
{@render children?.()}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<script lang="ts" generics="T">
|
<script lang="ts" generics="T">
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { debounce } from 'mono-svelte/util/time'
|
import { debounce } from 'mono-svelte/util/time'
|
||||||
import { onDestroy, untrack, type Snippet } from 'svelte'
|
import { onDestroy, onMount, untrack, type Snippet } from 'svelte'
|
||||||
import type { HTMLAttributes } from 'svelte/elements'
|
import type { HTMLAttributes } from 'svelte/elements'
|
||||||
import { innerHeight } from 'svelte/reactivity/window'
|
import { innerHeight } from 'svelte/reactivity/window'
|
||||||
|
|
||||||
@@ -72,15 +72,6 @@
|
|||||||
let viewportHeight = $state(0)
|
let viewportHeight = $state(0)
|
||||||
let visibleItems = $state<{ index: number; offset: number }[]>([])
|
let visibleItems = $state<{ index: number; offset: number }[]>([])
|
||||||
|
|
||||||
// Initial render
|
|
||||||
$effect.pre(() => {
|
|
||||||
if (virtualListEl && browser) {
|
|
||||||
untrack(() => {
|
|
||||||
visibleItems = updateVisibleItems()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
$effect.pre(() => {
|
$effect.pre(() => {
|
||||||
if (items.length > itemHeights.length) {
|
if (items.length > itemHeights.length) {
|
||||||
const missing = items.length - itemHeights.length
|
const missing = items.length - itemHeights.length
|
||||||
@@ -88,6 +79,14 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (items.length) {
|
||||||
|
untrack(() => {
|
||||||
|
visibleItems = updateVisibleItems()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function findFirstVisibleIndex(
|
function findFirstVisibleIndex(
|
||||||
scrollTop: number,
|
scrollTop: number,
|
||||||
cumulativeHeights: number[],
|
cumulativeHeights: number[],
|
||||||
@@ -146,7 +145,6 @@
|
|||||||
const newHeight = entry.contentRect.height
|
const newHeight = entry.contentRect.height
|
||||||
if (itemHeights[index] !== newHeight) {
|
if (itemHeights[index] !== newHeight) {
|
||||||
itemHeights[index] = newHeight
|
itemHeights[index] = newHeight
|
||||||
visibleItems = updateVisibleItems()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, debounceResize)
|
}, debounceResize)
|
||||||
@@ -179,6 +177,25 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
if (virtualListEl && browser) {
|
||||||
|
Array.from(virtualListEl.children).forEach(node => {
|
||||||
|
const indexAttr = node.getAttribute('data-index')
|
||||||
|
if (indexAttr === null) return
|
||||||
|
const index = Number(indexAttr)
|
||||||
|
if (isNaN(index)) return
|
||||||
|
|
||||||
|
const newHeight = node.getBoundingClientRect().height
|
||||||
|
if (itemHeights[index] !== newHeight) {
|
||||||
|
itemHeights[index] = newHeight
|
||||||
|
}
|
||||||
|
})
|
||||||
|
untrack(() => {
|
||||||
|
visibleItems = updateVisibleItems()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window
|
<svelte:window
|
||||||
|
|||||||
@@ -9,14 +9,15 @@
|
|||||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||||
import { t } from '$lib/i18n/translations.js'
|
import { t } from '$lib/i18n/translations.js'
|
||||||
import { site } from '$lib/lemmy.svelte.js'
|
import { site } from '$lib/lemmy.svelte.js'
|
||||||
|
import { postFeeds } from '$lib/lemmy/postfeed.svelte.js'
|
||||||
import { settings } from '$lib/settings.svelte.js'
|
import { settings } from '$lib/settings.svelte.js'
|
||||||
import Button from 'mono-svelte/button/Button.svelte'
|
import Button from 'mono-svelte/button/Button.svelte'
|
||||||
import { ArrowRight, ChartBar, Icon } from 'svelte-hero-icons'
|
import { ArrowRight, ChartBar, Icon } from 'svelte-hero-icons'
|
||||||
|
|
||||||
let { data = $bindable() } = $props()
|
let { data = $bindable() } = $props()
|
||||||
|
|
||||||
let type = $state(data.feed.type_)
|
let type = $state(data.feed.value.type_)
|
||||||
let sort = $state(data.feed.sort)
|
let sort = $state(data.feed.value.sort)
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (type) settings.defaultSort.feed = type
|
if (type) settings.defaultSort.feed = type
|
||||||
@@ -62,8 +63,8 @@
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
<FeedComponent
|
<FeedComponent
|
||||||
bind:posts={data.feed.posts.posts}
|
bind:posts={data.feed.value.posts.posts}
|
||||||
bind:feedData={data.feed}
|
bind:feedData={() => data.feed.value, v => (postFeeds.value.main.data = v)}
|
||||||
feedId="main"
|
feedId="main"
|
||||||
/>
|
/>
|
||||||
<svelte:element
|
<svelte:element
|
||||||
@@ -73,7 +74,7 @@
|
|||||||
class="mt-auto"
|
class="mt-auto"
|
||||||
>
|
>
|
||||||
<Pageination
|
<Pageination
|
||||||
cursor={{ next: data.feed.cursor.next }}
|
cursor={{ next: data.feed.value.cursor.next }}
|
||||||
href={page =>
|
href={page =>
|
||||||
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
|
typeof page == 'number' ? `?page=${page}` : `?cursor=${page}`}
|
||||||
back={false}
|
back={false}
|
||||||
|
|||||||
+15
-12
@@ -1,5 +1,6 @@
|
|||||||
import { t } from '$lib/i18n/translations.js'
|
import { t } from '$lib/i18n/translations.js'
|
||||||
import { postFeed } from '$lib/lemmy/postfeed.svelte.js'
|
import { postFeed } from '$lib/lemmy/postfeed.svelte.js'
|
||||||
|
import { ReactiveState } from '$lib/promise.svelte.js'
|
||||||
import { settings } from '$lib/settings.svelte'
|
import { settings } from '$lib/settings.svelte'
|
||||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||||
import { ChevronDoubleUp } from 'svelte-hero-icons'
|
import { ChevronDoubleUp } from 'svelte-hero-icons'
|
||||||
@@ -13,18 +14,20 @@ export async function load({ url, fetch }) {
|
|||||||
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
(url.searchParams.get('type') as ListingType) || settings.defaultSort.feed
|
||||||
|
|
||||||
return {
|
return {
|
||||||
feed: await postFeed({
|
feed: new ReactiveState(
|
||||||
id: 'main',
|
await postFeed({
|
||||||
request: {
|
id: 'main',
|
||||||
page_cursor: cursor,
|
request: {
|
||||||
sort: sort,
|
page_cursor: cursor,
|
||||||
type_: listingType,
|
sort: sort,
|
||||||
limit: 20,
|
type_: listingType,
|
||||||
show_hidden: settings.posts.showHidden,
|
limit: 20,
|
||||||
},
|
show_hidden: settings.posts.showHidden,
|
||||||
url: url,
|
},
|
||||||
fetch: fetch,
|
url: url,
|
||||||
}),
|
fetch: fetch,
|
||||||
|
}),
|
||||||
|
),
|
||||||
contextual: {
|
contextual: {
|
||||||
actions: [
|
actions: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -116,7 +116,7 @@
|
|||||||
<PostMeta
|
<PostMeta
|
||||||
community={data.post.value.post_view.community}
|
community={data.post.value.post_view.community}
|
||||||
user={data.post.value.post_view.creator}
|
user={data.post.value.post_view.creator}
|
||||||
bind:subscribed={data.post.value.community_view.subscribed}
|
subscribed={data.post.value.community_view.subscribed}
|
||||||
badges={{
|
badges={{
|
||||||
deleted: data.post.value.post_view.post.deleted,
|
deleted: data.post.value.post_view.post.deleted,
|
||||||
removed: data.post.value.post_view.post.removed,
|
removed: data.post.value.post_view.post.removed,
|
||||||
@@ -162,7 +162,7 @@
|
|||||||
{/if}
|
{/if}
|
||||||
<div class="w-full relative">
|
<div class="w-full relative">
|
||||||
<PostActions
|
<PostActions
|
||||||
bind:post={data.post.value.post_view}
|
post={data.post.value.post_view}
|
||||||
onedit={() =>
|
onedit={() =>
|
||||||
toast({
|
toast({
|
||||||
content: 'The post was edited successfully.',
|
content: 'The post was edited successfully.',
|
||||||
|
|||||||
Reference in new issue
Block a user