misc: change preloading strategy
This commit is contained in:
1 parent
322874dafa
commit
92e6c9ef9f
5 files changed
+127
-4
No files matched your search
@@ -16,6 +16,7 @@
|
||||
class="items-center flex flex-row gap-2 hover:underline max-w-full min-w-0 {$$props.class ||
|
||||
''}"
|
||||
href="/c/{community.name}@{new URL(community.actor_id).hostname}"
|
||||
data-sveltekit-preload-data="tap"
|
||||
>
|
||||
{#if avatar}
|
||||
<Avatar
|
||||
|
||||
@@ -180,6 +180,7 @@
|
||||
class:text-slate-600={$userSettings.markReadPosts && read}
|
||||
class:dark:text-zinc-400={$userSettings.markReadPosts && read}
|
||||
style="grid-area: title;"
|
||||
data-sveltekit-preload-data="tap"
|
||||
>
|
||||
<Markdown source={title} inline noStyle class="leading-[1.3]"></Markdown>
|
||||
</a>
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
class="items-center flex flex-row gap-1 hover:underline max-w-full min-w-0 {$$props.class ??
|
||||
''}"
|
||||
href="/u/{user.name}@{new URL(user.actor_id).hostname}"
|
||||
data-sveltekit-preload-data="tap"
|
||||
>
|
||||
{#if avatar}
|
||||
<Avatar
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
<script lang="ts">
|
||||
import { onMount, afterUpdate } from 'svelte'
|
||||
import type { SvelteComponent } from 'svelte'
|
||||
|
||||
interface VisibleItem<T> {
|
||||
item: T
|
||||
index: number
|
||||
top: number
|
||||
}
|
||||
|
||||
export let items: any[] = []
|
||||
|
||||
let containerHeight = 0
|
||||
let scrollTop = 0
|
||||
let itemHeights: number[] = []
|
||||
let visibleItems: VisibleItem<any>[] = []
|
||||
let totalHeight = 0
|
||||
let container: HTMLDivElement
|
||||
|
||||
$: {
|
||||
let currentOffset = 0
|
||||
let startIndex = 0
|
||||
let endIndex = items.length - 1
|
||||
|
||||
// Find the start index
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
if (currentOffset + itemHeights[i] > scrollTop) {
|
||||
startIndex = Math.max(0, i)
|
||||
break
|
||||
}
|
||||
currentOffset += itemHeights[i]
|
||||
}
|
||||
|
||||
// Find the end index
|
||||
currentOffset = 0
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
if (currentOffset > scrollTop + containerHeight) {
|
||||
endIndex = Math.min(items.length - 1, i)
|
||||
break
|
||||
}
|
||||
currentOffset += itemHeights[i]
|
||||
}
|
||||
|
||||
visibleItems = items.slice(startIndex, endIndex + 1).map((item, index) => ({
|
||||
item,
|
||||
index: startIndex + index,
|
||||
top: itemHeights.slice(0, startIndex + index).reduce((a, b) => a + b, 0),
|
||||
}))
|
||||
|
||||
console.log(visibleItems.length == items.length)
|
||||
|
||||
totalHeight = itemHeights.reduce((a, b) => a + b, 0)
|
||||
}
|
||||
|
||||
function handleScroll(event: Event): void {
|
||||
const target = event.target as HTMLDivElement
|
||||
scrollTop = target.scrollTop
|
||||
}
|
||||
|
||||
function updateItemHeight(index: number, height: number): void {
|
||||
if (itemHeights[index] !== height) {
|
||||
itemHeights[index] = height
|
||||
itemHeights = itemHeights
|
||||
}
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
containerHeight = container.clientHeight
|
||||
// Force an update
|
||||
items = items
|
||||
})
|
||||
|
||||
afterUpdate(() => {
|
||||
// Measure item heights after render
|
||||
visibleItems.forEach(({ index }) => {
|
||||
const element = container.querySelector(
|
||||
`[data-index="${index}"]`
|
||||
) as HTMLDivElement
|
||||
if (element) {
|
||||
updateItemHeight(index, element.offsetHeight)
|
||||
}
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
<svelte:window on:scroll={handleScroll} />
|
||||
|
||||
<div
|
||||
bind:this={container}
|
||||
style="height: 100%; width: 100%; overflow-x: hidden; overflow-y: scroll;"
|
||||
>
|
||||
<div style="height: {totalHeight}px; position: relative;">
|
||||
{#each visibleItems as { item, index, top }}
|
||||
<div
|
||||
data-index={index}
|
||||
style="position: absolute; top: {top}px; width: 100%;"
|
||||
>
|
||||
<slot {item} {index} />
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
+22
-4
@@ -16,17 +16,24 @@
|
||||
|
||||
export let data
|
||||
|
||||
const limit = 20
|
||||
let hasMore = data.posts.posts.length == limit
|
||||
|
||||
async function loadMore() {
|
||||
console.log('loading more')
|
||||
|
||||
if (!hasMore) return
|
||||
|
||||
const newPosts = await getClient().getPosts({
|
||||
limit: 20,
|
||||
limit: limit,
|
||||
page: data.page,
|
||||
sort: data.sort,
|
||||
type_: data.listingType,
|
||||
page_cursor: data.cursor.next,
|
||||
})
|
||||
|
||||
hasMore = newPosts.posts.length == limit
|
||||
|
||||
data.cursor.next = newPosts.next_page
|
||||
data.posts.posts = [...data.posts.posts, ...newPosts.posts]
|
||||
|
||||
@@ -47,10 +54,15 @@
|
||||
</div>
|
||||
|
||||
<PostFeed posts={data.posts.posts}>
|
||||
<div class="w-full grid place-items-center h-48">
|
||||
<Spinner width={32} />
|
||||
<div class="w-full flex flex-col skeleton gap-2 animate-pulse pt-6">
|
||||
<div class="w-96 h-8"></div>
|
||||
<div class="w-full h-48"></div>
|
||||
<div class="!bg-transparent h-8 flex justify-between">
|
||||
<div class="w-48 h-8"></div>
|
||||
<div class="w-24 h-8"></div>
|
||||
</div>
|
||||
</div>
|
||||
<InfiniteScroll window threshold={400} on:loadMore={loadMore} />
|
||||
<InfiniteScroll window threshold={750} {hasMore} on:loadMore={loadMore} />
|
||||
</PostFeed>
|
||||
<div class="mt-auto">
|
||||
<Pageination
|
||||
@@ -71,3 +83,9 @@
|
||||
</Pageination>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style lang="postcss">
|
||||
.skeleton * {
|
||||
@apply bg-slate-100 dark:bg-zinc-800 rounded-md;
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user