misc: switch profile to use a derived store
This commit is contained in:
1 parent
0f4796dadc
commit
19d4617dbf
5 files changed
+77
-127
No files matched your search
+52
-106
@@ -4,7 +4,8 @@ import {
|
|||||||
} from '$lib/components/lemmy/moderation/moderation.js'
|
} from '$lib/components/lemmy/moderation/moderation.js'
|
||||||
import { toast } from 'mono-svelte'
|
import { toast } from 'mono-svelte'
|
||||||
import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js'
|
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 { userSettings } from '$lib/settings.js'
|
||||||
import { instanceToURL, moveItem } from '$lib/util.js'
|
import { instanceToURL, moveItem } from '$lib/util.js'
|
||||||
import {
|
import {
|
||||||
@@ -13,7 +14,7 @@ import {
|
|||||||
type MyUserInfo,
|
type MyUserInfo,
|
||||||
type Community,
|
type Community,
|
||||||
} from 'lemmy-js-client'
|
} 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 { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { env } from '$env/dynamic/public'
|
import { env } from '$env/dynamic/public'
|
||||||
@@ -91,19 +92,53 @@ export let profileData = writable<ProfileData>(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// stupid hack to get dev server working
|
export let profile = derived<Writable<ProfileData>, Profile>(
|
||||||
// why does this always happen to me
|
profileData,
|
||||||
|
(pd) => {
|
||||||
|
const profile =
|
||||||
|
pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile()
|
||||||
|
|
||||||
let initialInstance = get(profileData).defaultInstance
|
if (profile?.jwt) {
|
||||||
|
if (profile.user) {
|
||||||
|
site.set(undefined)
|
||||||
|
|
||||||
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
site.set(undefined)
|
||||||
|
client({ instanceURL: profile.instance })
|
||||||
|
.getSite()
|
||||||
|
.then((res) => site.set(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.set(profile.instance)
|
||||||
|
|
||||||
|
return profile
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
profileData.subscribe(async (pd) => {
|
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) {
|
setFromStorage('profileData', serialized)
|
||||||
initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL
|
|
||||||
|
if (serialized.profile == -1) {
|
||||||
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||||
}
|
}
|
||||||
if (pd.profiles.length == 0) {
|
if (serialized.profiles.length == 0) {
|
||||||
profileData.update((pd) => ({
|
profileData.update((pd) => ({
|
||||||
...pd,
|
...pd,
|
||||||
profiles: [
|
profiles: [
|
||||||
@@ -145,43 +180,6 @@ if (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export let profile = writable<Profile | undefined>(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) {
|
export async function setUser(jwt: string, inst: string, username: string) {
|
||||||
try {
|
try {
|
||||||
new URL(instanceToURL(inst))
|
new URL(instanceToURL(inst))
|
||||||
@@ -217,11 +215,6 @@ export async function setUser(jwt: string, inst: string, username: string) {
|
|||||||
avatar: user.user.local_user_view.person.avatar,
|
avatar: user.user.local_user_view.person.avatar,
|
||||||
}
|
}
|
||||||
|
|
||||||
profile.set({
|
|
||||||
...newProfile,
|
|
||||||
user: user!.user,
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
profile: id,
|
profile: id,
|
||||||
profiles: [newProfile, ...pd.profiles],
|
profiles: [newProfile, ...pd.profiles],
|
||||||
@@ -273,20 +266,7 @@ async function userFromJwt(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
export function resetProfile() {
|
||||||
profile.set(getDefaultProfile())
|
|
||||||
profileData.update((p) => ({ ...p, profile: -1 }))
|
profileData.update((p) => ({ ...p, profile: -1 }))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,10 +288,12 @@ export function deleteProfile(id: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const serializeUser = (user: Profile): Profile => ({
|
function serializeUser(user: Profile): Profile {
|
||||||
...user,
|
return {
|
||||||
user: undefined,
|
...user,
|
||||||
})
|
user: undefined,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
instance.subscribe(async (i) => {
|
instance.subscribe(async (i) => {
|
||||||
try {
|
try {
|
||||||
@@ -324,35 +306,8 @@ instance.subscribe(async (i) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
export async function setUserID(id: number) {
|
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 }))
|
profileData.update((p) => ({ ...p, profile: id }))
|
||||||
|
return get(profile)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function moveProfile(id: number, up: boolean) {
|
export function moveProfile(id: number, up: boolean) {
|
||||||
@@ -369,11 +324,7 @@ export function moveProfile(id: number, up: boolean) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getNotificationCount = async (
|
async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) {
|
||||||
jwt: string,
|
|
||||||
mod: boolean,
|
|
||||||
admin: boolean
|
|
||||||
) => {
|
|
||||||
const unreads = await getClient().getUnreadCount()
|
const unreads = await getClient().getUnreadCount()
|
||||||
|
|
||||||
let reports: number = 0
|
let reports: number = 0
|
||||||
@@ -427,11 +378,6 @@ async function checkInbox() {
|
|||||||
applications: notifs.applications,
|
applications: notifs.applications,
|
||||||
reports: notifs.reports,
|
reports: notifs.reports,
|
||||||
}
|
}
|
||||||
|
|
||||||
profile.update((p) => ({
|
|
||||||
...p!,
|
|
||||||
user: user,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(checkInbox, 4 * 60 * 1000)
|
setInterval(checkInbox, 4 * 60 * 1000)
|
||||||
|
|||||||
+21
-15
@@ -48,7 +48,7 @@ async function customFetch(
|
|||||||
if (auth && url) url.searchParams.set('auth', auth)
|
if (auth && url) url.searchParams.set('auth', auth)
|
||||||
|
|
||||||
const res = await f(url ?? input, init)
|
const res = await f(url ?? input, init)
|
||||||
if (!res.ok) error(res.status, await res.text());
|
if (!res.ok) error(res.status, await res.text())
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,27 +101,33 @@ export async function validateInstance(instance: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mayBeIncompatible(minVersion: string, availableVersion: string) {
|
export function mayBeIncompatible(
|
||||||
if (minVersion.valueOf() === availableVersion.valueOf()) return false;
|
minVersion: string,
|
||||||
|
availableVersion: string
|
||||||
|
) {
|
||||||
|
if (minVersion.valueOf() === availableVersion.valueOf()) return false
|
||||||
|
|
||||||
const versionFormatter = /[\d.]/;
|
const versionFormatter = /[\d.]/
|
||||||
if (!minVersion.match(versionFormatter) || !availableVersion.match(availableVersion)) {
|
if (
|
||||||
return true;
|
!minVersion.match(versionFormatter) ||
|
||||||
|
!availableVersion.match(availableVersion)
|
||||||
|
) {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const splitMinVersion = minVersion.split('.');
|
const splitMinVersion = minVersion.split('.')
|
||||||
const splitAvailableVersion = availableVersion.split('.');
|
const splitAvailableVersion = availableVersion.split('.')
|
||||||
|
|
||||||
if (splitMinVersion.length !== splitAvailableVersion.length) return true;
|
if (splitMinVersion.length !== splitAvailableVersion.length) return true
|
||||||
|
|
||||||
for (let i = 0; i < splitMinVersion.length; ++i) {
|
for (let i = 0; i < splitMinVersion.length; ++i) {
|
||||||
const minVersionDigit = parseInt(splitMinVersion[i]);
|
const minVersionDigit = parseInt(splitMinVersion[i])
|
||||||
const availableVersionDigit = parseInt(splitAvailableVersion[i]);
|
const availableVersionDigit = parseInt(splitAvailableVersion[i])
|
||||||
|
|
||||||
if (availableVersionDigit === undefined) return true;
|
if (availableVersionDigit === undefined) return true
|
||||||
if (minVersionDigit < availableVersionDigit) return false;
|
if (minVersionDigit < availableVersionDigit) return false
|
||||||
if (availableVersionDigit < minVersionDigit) return true;
|
if (availableVersionDigit < minVersionDigit) return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
+2
-1
@@ -5,7 +5,8 @@ import { get } from 'svelte/store'
|
|||||||
import type { SubscribedType } from 'lemmy-js-client'
|
import type { SubscribedType } from 'lemmy-js-client'
|
||||||
import { page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import { feature } from '$lib/version.js'
|
import { feature } from '$lib/version.js'
|
||||||
import { client, site } from '$lib/lemmy.js'
|
import { client } from '$lib/lemmy.js'
|
||||||
|
import { site } from './lemmy'
|
||||||
|
|
||||||
export const findClosestNumber = (numbers: number[], target: number): number =>
|
export const findClosestNumber = (numbers: number[], target: number): number =>
|
||||||
numbers.reduce((prev, curr) =>
|
numbers.reduce((prev, curr) =>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
import { getClient, site as siteStore } from '$lib/lemmy.js'
|
import { getClient } from '$lib/lemmy.js'
|
||||||
|
import { site as siteStore } from '$lib/site'
|
||||||
|
|
||||||
export async function load({ fetch }) {
|
export async function load({ fetch }) {
|
||||||
if (!get(profile)) return
|
if (!get(profile)) return
|
||||||
|
|||||||
@@ -34,10 +34,6 @@
|
|||||||
username: form.username,
|
username: form.username,
|
||||||
}
|
}
|
||||||
|
|
||||||
profile.set({
|
|
||||||
...newProfile,
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
profile: id,
|
profile: id,
|
||||||
profiles: [...pd.profiles, newProfile],
|
profiles: [...pd.profiles, newProfile],
|
||||||
|
|||||||
Reference in new issue
Block a user