diff --git a/src/lib/lemmy/user.ts b/src/lib/lemmy/user.ts index 9b98c844..fd5325b2 100644 --- a/src/lib/lemmy/user.ts +++ b/src/lib/lemmy/user.ts @@ -29,7 +29,7 @@ export const addSubscription = ( if (subscribe && index == -1) { profile.data.user?.follows.push({ // @ts-ignore - follower: p.user.follows[0]?.follower, + follower: profile.data.user.follows[0]?.follower, community: community, }) } else { diff --git a/src/lib/promise.ts b/src/lib/promise.svelte.ts similarity index 64% rename from src/lib/promise.ts rename to src/lib/promise.svelte.ts index 1f389eed..c778e372 100644 --- a/src/lib/promise.ts +++ b/src/lib/promise.svelte.ts @@ -7,3 +7,11 @@ export const awaitIfServer = async ( ): Promise<{ data: Promise | T }> => ({ data: browser ? promise : await promise }) + +export class ReactiveState { + value = $state()! + + constructor(initialValue: T) { + this.value = initialValue as NonNullable + } +} diff --git a/src/routes/c/[name]/+layout.svelte b/src/routes/c/[name]/+layout.svelte index a9cc9301..96c394da 100644 --- a/src/routes/c/[name]/+layout.svelte +++ b/src/routes/c/[name]/+layout.svelte @@ -3,16 +3,16 @@ - {data.community.community_view.community.title} + {data.community.value.community_view.community.title} - {#if data.community.community_view.community.description} + {#if data.community.value.community_view.community.description} {/if} diff --git a/src/routes/c/[name]/+layout.ts b/src/routes/c/[name]/+layout.ts index cc3c7639..819abc7b 100644 --- a/src/routes/c/[name]/+layout.ts +++ b/src/routes/c/[name]/+layout.ts @@ -1,5 +1,6 @@ import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte' import { getClient } from '$lib/lemmy.svelte.js' +import { ReactiveState } from '$lib/promise.svelte' export async function load(req: any) { const community = await getClient(undefined, req.fetch).getCommunity({ @@ -7,7 +8,7 @@ export async function load(req: any) { }) return { - community: community, + community: new ReactiveState(community), slots: { sidebar: { component: CommunityCard, diff --git a/src/routes/c/[name]/+page.svelte b/src/routes/c/[name]/+page.svelte index 2edca63d..91f30fd9 100644 --- a/src/routes/c/[name]/+page.svelte +++ b/src/routes/c/[name]/+page.svelte @@ -21,11 +21,7 @@ import Header from '$lib/components/ui/layout/pages/Header.svelte' import { profile } from '$lib/auth.svelte.js' - let { data: pageData } = $props() - let data = $state(pageData) - $effect(() => { - data = pageData - }) + let { data } = $props() let sidebar: boolean = $state(false) @@ -33,14 +29,14 @@ if (browser) setSessionStorage( 'lastSeenCommunity', - data.community.community_view.community, + data.community.value.community_view.community, ) resumables.add({ - name: data.community.community_view.community.title, + name: data.community.value.community_view.community.title, type: 'community', url: page.url.toString(), - avatar: data.community.community_view.community.icon, + avatar: data.community.value.community_view.community.icon, }) }) @@ -60,8 +56,8 @@ {/snippet}
@@ -71,11 +67,11 @@
@@ -85,7 +81,7 @@
{#if profile.data.user} - {#if !data.community.discussion_languages.every( (l) => profile.data.user?.discussion_languages.includes(l), ) && profile.data.user.discussion_languages.length > 0} + {#if !data.community.value.discussion_languages.every( (l) => profile.data.user?.discussion_languages.includes(l), ) && profile.data.user.discussion_languages.length > 0} {$t('routes.community.languageWarning')} @@ -370,12 +371,12 @@
- + {$t('routes.post.commentCount')}
- {#await data.comments} + {#await data.comments.value}
{#each new Array(10) as empty, index}
{:then comments} {#if comments.comments.length == 0} {/if} {/await} - {#if data.post.post_view.counts.comments > 5} + {#if data.post.value.post_view.counts.comments > 5} - {data.post.post_view.counts.comments} + {data.post.value.post_view.counts.comments} {$t('routes.post.commentCount')} diff --git a/src/routes/post/[instance]/[id=integer]/+page.ts b/src/routes/post/[instance]/[id=integer]/+page.ts index 2bea0626..b2bed0e6 100644 --- a/src/routes/post/[instance]/[id=integer]/+page.ts +++ b/src/routes/post/[instance]/[id=integer]/+page.ts @@ -1,8 +1,9 @@ import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte' import { getClient } from '$lib/lemmy.svelte.js' -import { awaitIfServer } from '$lib/promise.js' +import { awaitIfServer } from '$lib/promise.svelte.js' import { SSR_ENABLED, settings } from '$lib/settings.svelte' import type { GetComments } from 'lemmy-js-client' +import { ReactiveState } from '$lib/promise.svelte.js' export async function load({ params, url, fetch }) { const thread = url.searchParams.get('thread') @@ -49,14 +50,14 @@ export async function load({ params, url, fetch }) { }) return { - thread: { + thread: new ReactiveState({ showContext: showContext, singleThread: parentId != undefined, focus: thread?.split('.').at(-1), - }, - post: post, - commentSort: sort, - comments: (await awaitIfServer(comments)).data, + }), + post: new ReactiveState(post), + commentSort: new ReactiveState(sort), + comments: new ReactiveState((await awaitIfServer(comments)).data), slots: { sidebar: { component: CommunityCard, diff --git a/src/routes/search/+page.svelte b/src/routes/search/+page.svelte index 75c252ef..e3750ac1 100644 --- a/src/routes/search/+page.svelte +++ b/src/routes/search/+page.svelte @@ -46,12 +46,6 @@ let { data } = $props() - let results = $state(data.results) - - $effect(() => { - results = data.results - }) - let query = $state(data.query || '') let searchElement: HTMLInputElement | undefined = $state() let form = $state() @@ -213,7 +207,7 @@
- {#each data.results as result} + {#each data.results.value as result} {#if isPostView(result)} {:else if isCommentView(result)} @@ -234,7 +228,7 @@ {/each}
- {#if data.results.length > 0} + {#if data.results.value.length > 0} `?page=${page}`} /> {/if} {/if} diff --git a/src/routes/search/+page.ts b/src/routes/search/+page.ts index a6d1ca03..2a7fee53 100644 --- a/src/routes/search/+page.ts +++ b/src/routes/search/+page.ts @@ -1,6 +1,7 @@ import { profile } from '$lib/auth.svelte' import { client, getClient } from '$lib/lemmy.svelte.js' import { getItemPublished } from '$lib/lemmy/item.js' +import { ReactiveState } from '$lib/promise.svelte.js' import type { CommentView, CommunityView, @@ -55,7 +56,7 @@ export async function load({ url, fetch }) { sort: sort, page: page, query: query, - results: everything, + results: new ReactiveState(everything), streamed: { object: profile.data?.jwt ? getClient(undefined, fetch).resolveObject({