diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 74a3920..9ff2b00 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -13,13 +13,22 @@ let interval: NodeJS.Timer | null = null; let moreOlderPostsAvailable = true; let loadingOlderPosts = false; -async function fetchPosts(filter: { since?: string, before?: string }): Promise { +interface FetchOptions { + since?: string, + before?: string, + count?: number +} + +async function fetchPosts(options: FetchOptions): Promise { const params = new URLSearchParams(); - if (filter?.since !== undefined) { - params.set('since', filter.since); + if (options?.since !== undefined) { + params.set('since', options.since); } - if (filter?.before !== undefined) { - params.set('before', filter.before); + if (options?.before !== undefined) { + params.set('before', options.before); + } + if (options?.count !== undefined) { + params.set('count', options.count.toFixed(0)); } const response = await fetch(`/api/posts?${params}`); @@ -33,7 +42,7 @@ function filterDuplicates(posts: Post[]): Post[] { } function refresh() { - let filter = {}; + let filter: FetchOptions = {}; if (data.posts.length > 0) { filter = { since: data.posts[0].created_at }; } @@ -72,9 +81,9 @@ onMount(async () => { function loadOlderPosts() { loadingOlderPosts = true; - let filter = {}; + const filter: FetchOptions = { count: 20 }; if (data.posts.length > 0) { - filter = { before: data.posts[data.posts.length - 1].created_at }; + filter.before = data.posts[data.posts.length - 1].created_at; } @@ -83,7 +92,8 @@ function loadOlderPosts() { // Append old posts, filter dupes // There shouldn't be any duplicates, but better be safe than sorry data.posts = filterDuplicates(data.posts.concat(resp)); - moreOlderPostsAvailable = true; + // If we got less than we expected, there are no older posts available + moreOlderPostsAvailable = resp.length < (filter.count ?? 20); } else { moreOlderPostsAvailable = false; }