Skip to content

Commit

Permalink
Fix missing post when navigating back from post
Browse files Browse the repository at this point in the history
  • Loading branch information
sant0s12 committed Jan 1, 2024
1 parent 8c5418f commit bbb8791
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
17 changes: 9 additions & 8 deletions src/lib/components/lemmy/post/PostFeed.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
import { expoOut } from "svelte/easing";
import { fly, slide } from "svelte/transition";
import InfiniteScroll from "svelte-infinite-scroll";
import { loadPosts } from "$lib/lemmy";
import { loadPosts, loadedPosts } from "$lib/lemmy.ts";
import { page } from "$app/stores";
import { goto } from "$app/navigation";
type PostViewWithCrossposts = PostView & {
withCrossposts: true;
crossposts: PostView[];
};
type PostViewWithoutCrossposts = PostView & { withCrossposts?: false };
export let posts: PostView[];
export let initialPosts: PostView[];
export let cursor: { next?: string; back?: string } | undefined = undefined;
export let community;
if ($loadedPosts.length == 0) {
$loadedPosts = initialPosts;
}
const addCrosspostProperty = (
post: PostView,
crossposts: PostView[],
Expand Down Expand Up @@ -65,7 +68,7 @@
return results;
};
$: combinedPosts = combineCrossposts(posts);
$: combinedPosts = combineCrossposts($loadedPosts);
let viewPost: number = -1;
</script>
Expand All @@ -75,7 +78,7 @@
? 'gap-3 md:gap-4'
: 'divide-y'} divide-slate-200 dark:divide-zinc-800"
>
{#if posts.length == 0}
{#if $loadedPosts.length == 0}
<div class="h-full grid place-items-center">
<Placeholder
icon={ArchiveBox}
Expand Down Expand Up @@ -167,10 +170,8 @@
let newUrl = $page.url;
newUrl.searchParams.set("cursor", cursor?.next || "");

goto(newUrl, { invalidateAll: false, noScroll: true });

let newPosts = await loadPosts(newUrl, fetch);
posts = [...posts, ...newPosts.posts.posts];
$loadedPosts = [...$loadedPosts, ...newPosts.posts.posts];

cursor = newPosts.cursor;
}}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/lemmy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type GetSiteResponse, LemmyHttp, type SortType, type ListingType } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { type GetSiteResponse, LemmyHttp, type SortType, type ListingType, type PostView } from 'lemmy-js-client'
import { get, writable, type Writable } from 'svelte/store'
import { error } from '@sveltejs/kit'
import { instance } from '$lib/instance.js'
import { instanceToURL } from '$lib/util.js'
Expand Down Expand Up @@ -164,3 +164,5 @@ export async function loadPosts(url: URL, fetch: any) {
})
}
}

export const loadedPosts: Writable<PostView[]> = writable([]);
3 changes: 1 addition & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { page } from "$app/stores";
import StickyCard from "$lib/components/ui/StickyCard.svelte";
import SiteCard from "$lib/components/lemmy/SiteCard.svelte";
import Pageination from "$lib/components/ui/Pageination.svelte";
import Sort from "$lib/components/lemmy/Sort.svelte";
Expand Down Expand Up @@ -78,7 +77,7 @@
</div>
</div>
<PostFeed
posts={data.posts.posts}
initialPosts={data.posts.posts}
cursor={{ next: data.cursor.next, back: data.cursor.back }}
/>
<div class="mt-auto">
Expand Down
7 changes: 0 additions & 7 deletions src/routes/+page.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { getClient } from '$lib/lemmy.js'
import { userSettings } from '$lib/settings.js'
import type { ListingType, SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
import { error } from '@sveltejs/kit'
import { profile } from '$lib/auth.js'
import { feature } from '$lib/version.js'
import { loadPosts } from '$lib/lemmy.ts'

export async function load({ url, fetch }) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/c/[name]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
</Button>
</Placeholder>
{:else}
<PostFeed community={true} posts={data.posts.posts} />
<PostFeed community={true} initialPosts={data.posts.posts} />
{/if}

<Pageination
Expand Down

0 comments on commit bbb8791

Please sign in to comment.