diff --git a/bun.lockb b/bun.lockb index 6592e57e..30c540f1 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5072bf19..7ad671c3 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,6 @@ { "name": "photon-lemmy", - "version": "1.30.4", - "private": true, - "scripts": { - "dev": "vite dev", - "build": "vite build", - "test": "vitest", - "preview": "vite preview", - "start": "node build/index.js", - "bunstart": "bun build/index.js", - "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", - "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch" - }, + "version": "1.31.0", "devDependencies": { "@catdadcode/svelte-adapter-bun": "^0.5.7", "@dicebear/core": "^8.0.1", @@ -22,6 +11,7 @@ "@sveltejs/kit": "latest", "@sveltejs/vite-plugin-svelte": "^3.0.0", "@tailwindcss/container-queries": "^0.1.1", + "@tanstack/svelte-virtual": "^3.8.1", "@types/markdown-it": "^12.2.3", "@types/markdown-it-container": "^2.0.6", "@types/nprogress": "^0.2.0", @@ -40,7 +30,7 @@ "svelte": "latest", "svelte-check": "^3.0.1", "svelte-hero-icons": "^5.0.0", - "svelte-tiny-virtual-list": "^2.0.5", + "svelte-infinite-scroll": "^2.0.1", "sveltekit-i18n": "^2.4.2", "tailwindcss": "^3.3.3", "tslib": "^2.4.1", @@ -50,5 +40,16 @@ "vitest": "^1.0.0", "workbox-window": "^7.0.0" }, + "private": true, + "scripts": { + "dev": "vite dev", + "build": "vite build", + "test": "vitest", + "preview": "vite preview", + "start": "node build/index.js", + "bunstart": "bun build/index.js", + "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", + "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch" + }, "type": "module" } diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 6617c494..9b8bdb0a 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -4,7 +4,8 @@ import { } from '$lib/components/lemmy/moderation/moderation.js' import { toast } from 'mono-svelte' import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js' -import { client, getClient, site } from '$lib/lemmy.js' +import { client, getClient } from '$lib/lemmy.js' +import { site } from './lemmy' import { userSettings } from '$lib/settings.js' import { instanceToURL, moveItem } from '$lib/util.js' import { @@ -13,7 +14,7 @@ import { type MyUserInfo, type Community, } from 'lemmy-js-client' -import { get, writable } from 'svelte/store' +import { derived, get, writable, type Writable } from 'svelte/store' import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js' import { browser } from '$app/environment' import { env } from '$env/dynamic/public' @@ -57,12 +58,12 @@ interface ProfileData { defaultInstance?: string } -interface PersonData extends MyUserInfo { - notifications: { - inbox: number - reports: number - applications: number - } +interface PersonData extends MyUserInfo {} + +interface Notifications { + inbox: number + reports: number + applications: number } const getCookie = (key: string): string | undefined => { @@ -91,19 +92,77 @@ export let profileData = writable( } ) -// stupid hack to get dev server working -// why does this always happen to me +let fetchingUser = false -let initialInstance = get(profileData).defaultInstance +export let profile = derived, Profile>( + profileData, + (pd) => { + const profile = + pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile() + + if (profile?.jwt) { + if (!profile.user && !fetchingUser) { + site.set(undefined) + + fetchingUser = true + + notifications.set({ applications: 0, inbox: 0, reports: 0 }) + + userFromJwt(profile.jwt, profile.instance) + .then((res) => { + if (!res?.user) + toast({ + content: 'Your login has expired. Re-login to fix this issue.', + type: 'warning', + }) + + site.set(res?.site) + + profile.user = res?.user + profile.avatar = res?.user.local_user_view.person.avatar + + checkInbox() + + fetchingUser = false + }) + .catch((e) => { + fetchingUser = false + toast({ content: e, type: 'error' }) + }) + } + } else { + if (browser) { + site.set(undefined) + client({ instanceURL: profile.instance }) + .getSite() + .then((res) => site.set(res)) + } + } + + instance.set(profile.instance) + + return profile + } +) + +export let notifications = writable({ + applications: 0, + inbox: 0, + reports: 0, +}) profileData.subscribe(async (pd) => { - setFromStorage('profileData', pd) + const serialized: ProfileData = { + ...pd, + profiles: pd.profiles.map((p) => serializeUser(p)), + } - if (pd.profile == -1 && initialInstance != pd.defaultInstance) { - initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL + setFromStorage('profileData', serialized) + + if (serialized.profile == -1) { instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) } - if (pd.profiles.length == 0) { + if (serialized.profiles.length == 0) { profileData.update((pd) => ({ ...pd, profiles: [ @@ -145,43 +204,6 @@ if ( } } -export let profile = writable(getProfile()) - -profile.subscribe(async (p) => { - if (p?.id == -1) { - instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) - } - if (!p) return - - instance.set(p.instance) - // fetch the user because p.user is undefined - if (p?.jwt && !p?.user) { - const user = await userFromJwt(p.jwt, p.instance) - .then((user) => { - if (!user?.user) - toast({ - content: 'Your login has expired. Re-login to fix this issue.', - type: 'warning', - }) - site.set(user?.site) - - return user - }) - .catch((err) => { - toast({ content: err as any, type: 'error' }) - }) - - if (!user?.user) return - - profile.update(() => ({ - ...p, - user: user!.user, - username: user?.user.local_user_view.person.name, - avatar: user?.user.local_user_view.person.avatar, - })) - } -}) - export async function setUser(jwt: string, inst: string, username: string) { try { new URL(instanceToURL(inst)) @@ -217,11 +239,6 @@ export async function setUser(jwt: string, inst: string, username: string) { avatar: user.user.local_user_view.person.avatar, } - profile.set({ - ...newProfile, - user: user!.user, - }) - return { profile: id, profiles: [newProfile, ...pd.profiles], @@ -261,32 +278,12 @@ async function userFromJwt( if (!myUser) return undefined return { - user: { - notifications: { - applications: 0, - inbox: 0, - reports: 0, - }, - ...myUser, - }, + user: myUser, site: site, } } -function getProfile() { - const id = get(profileData).profile - - if (id == -1) { - return getDefaultProfile() - } - - const pd = get(profileData) - - return pd.profiles.find((p) => p.id == id) -} - export function resetProfile() { - profile.set(getDefaultProfile()) profileData.update((p) => ({ ...p, profile: -1 })) } @@ -308,10 +305,12 @@ export function deleteProfile(id: number) { } } -const serializeUser = (user: Profile): Profile => ({ - ...user, - user: undefined, -}) +function serializeUser(user: Profile): Profile { + return { + ...user, + user: undefined, + } +} instance.subscribe(async (i) => { try { @@ -324,35 +323,8 @@ instance.subscribe(async (i) => { }) export async function setUserID(id: number) { - const pd = get(profileData) - if (id == -1) { - resetProfile() - instance.set(DEFAULT_INSTANCE_URL) - return - } - - let prof = pd.profiles.find((p) => p.id == id) - - if (!prof) return profile.update(() => getDefaultProfile()) - prof = serializeUser(prof) - profileData.update((p) => ({ ...p, profile: id })) - - if (prof?.jwt) { - const user = await userFromJwt(prof.jwt, prof.instance) - .then((u) => u) - .catch((err) => { - toast({ content: err as any, type: 'error' }) - }) - instance.set(prof.instance) - prof.user = user?.user - prof.avatar = user?.user.local_user_view.person.avatar - site.set(user?.site) - } - - profile.update(() => prof ?? getDefaultProfile()) - - return prof + return get(profile) } export function moveProfile(id: number, up: boolean) { @@ -369,11 +341,7 @@ export function moveProfile(id: number, up: boolean) { } } -const getNotificationCount = async ( - jwt: string, - mod: boolean, - admin: boolean -) => { +async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) { const unreads = await getClient().getUnreadCount() let reports: number = 0 @@ -419,26 +387,14 @@ async function checkInbox() { const notifs = await getNotificationCount( jwt, amModOfAny(user) ?? false, - isAdmin(user) + user ? isAdmin(user) : false ) - user.notifications = { + notifications.set({ inbox: notifs.unreads, applications: notifs.applications, reports: notifs.reports, - } - - profile.update((p) => ({ - ...p!, - user: user, - })) + }) } setInterval(checkInbox, 4 * 60 * 1000) - -let prevId: number | undefined = -2 -profile.subscribe((p) => { - if (p?.id == prevId || !p?.user) return - prevId = p?.id - if (p.id != -1) checkInbox() -}) diff --git a/src/lib/components/lemmy/community/CommunityLink.svelte b/src/lib/components/lemmy/community/CommunityLink.svelte index 9e7366c1..6bffc308 100644 --- a/src/lib/components/lemmy/community/CommunityLink.svelte +++ b/src/lib/components/lemmy/community/CommunityLink.svelte @@ -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} - {community.title} + {community.title} {#if showInstance}