misc: change $instance to a $state

This commit is contained in:
James Xylight committed 2025-02-09 11:36:04 -08:00
1 parent 8dcca84e60
commit d8e916d526
26 files changed
+54 -48

No files matched your search

+2 -9
View File
@@ -3,7 +3,7 @@ import {
isAdmin,
} from '$lib/components/lemmy/moderation/moderation.js'
import { toast } from 'mono-svelte'
import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js'
import { DEFAULT_INSTANCE_URL } from '$lib/instance.svelte.js'
import { client, getClient } from '$lib/lemmy.svelte.js'
import { site } from './lemmy.svelte'
import { instanceToURL, moveItem } from '$lib/util.svelte'
@@ -27,7 +27,7 @@ import { t } from './translations'
const getDefaultProfile = (): Profile => ({
id: -1,
instance: profileData.defaultInstance ?? get(instance),
instance: profileData.defaultInstance ?? DEFAULT_INSTANCE_URL,
})
function getFromStorage<T>(key: string): T | undefined {
@@ -116,7 +116,6 @@ class CurrentProfile {
export let profile = new CurrentProfile()
async function fetchUserData(profile: CurrentProfile) {
instance.set(profile.data.instance)
if (profile.data.jwt) {
site.data = undefined
notifications.set({ applications: 0, inbox: 0, reports: 0 })
@@ -161,10 +160,6 @@ $effect.root(() => {
}
setFromStorage('profileData', serialized)
if (serialized.profile == -1) {
instance?.set(profileData.defaultInstance ?? DEFAULT_INSTANCE_URL)
}
if (serialized.profiles.length == 0) {
profileData.profiles = [
{
@@ -230,8 +225,6 @@ export async function setUser(jwt: string, inst: string, username?: string) {
})
}
instance.set(inst)
const id = Math.max(...profileData.profiles.map((p) => p.id)) + 1
profileData.profile = id
profileData.profiles.push({
+1 -1
View File
@@ -22,7 +22,7 @@
import Profile from './Profile.svelte'
import NavButton from './NavButton.svelte'
import Logo from '../Logo.svelte'
import { LINKED_INSTANCE_URL } from '$lib/instance'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
import { t } from '$lib/translations'
import CommandsWrapper from './commands/CommandsWrapper.svelte'
import type { ClassValue } from 'svelte/elements'
@@ -3,7 +3,7 @@
import { page } from '$app/state'
import { profile, setUserID, type Profile } from '$lib/auth.svelte.js'
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
import { LINKED_INSTANCE_URL } from '$lib/instance'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
import ProfileAvatar from '$lib/lemmy/ProfileAvatar.svelte'
import { Button } from 'mono-svelte'
import { Icon, QuestionMarkCircle } from 'svelte-hero-icons'
@@ -3,6 +3,7 @@
import { browser } from '$app/environment'
import { env } from '$env/dynamic/public'
import { writable } from 'svelte/store'
import { profile } from './auth.svelte'
export const LINKED_INSTANCE_URL =
(env.PUBLIC_LOCK_TO_INSTANCE ?? 'true').toLowerCase() == 'true'
@@ -18,4 +19,13 @@ const getDefaultInstance = (): string => {
}
export const DEFAULT_INSTANCE_URL = getDefaultInstance()
export let instance = writable(DEFAULT_INSTANCE_URL)
class InstanceData {
#instance = $derived(profile.data.instance)
get data() {
return this.#instance
}
}
export let instance = new InstanceData()
+1 -1
View File
@@ -1,7 +1,7 @@
import { type GetSiteResponse, LemmyHttp } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { error } from '@sveltejs/kit'
import { instance } from '$lib/instance.js'
import { instance } from '$lib/instance.svelte.js'
import { instanceToURL } from '$lib/util.svelte'
import { profile } from '$lib/auth.svelte'
import { toast } from 'mono-svelte'
+4 -4
View File
@@ -1,5 +1,5 @@
import { browser } from '$app/environment'
import { instance } from '$lib/instance'
import { instance } from '$lib/instance.svelte'
import { client } from '$lib/lemmy.svelte'
import type {
GetPosts,
@@ -32,11 +32,11 @@ export async function postFeed(args: {
}) {
const feed = get(postFeeds)[args.id]
const posts = shouldReload(feed, args.url, get(instance))
const posts = shouldReload(feed, args.url, instance.data)
? await client({ func: args.fetch }).getPosts(args.request)
: feed.data.posts
if (shouldReload(feed, args.url, get(instance)) && browser)
if (shouldReload(feed, args.url, instance.data) && browser)
postFeeds.updateFeed(args.id, {
data: {
...args.request,
@@ -48,7 +48,7 @@ export async function postFeed(args: {
},
url: args.url,
lastSeen: 0,
instance: get(instance),
instance: instance.data,
})
return (