misc: fix persistence

This commit is contained in:
Xyphyn committed 2023-08-01 11:24:49 -07:00
1 parent 55a047ab73
commit fc1afecfdd
2 files changed
+60 -17

No files matched your search

+33 -10
View File
@@ -34,16 +34,16 @@ interface PersonData extends MyUserInfo {
reports: number
}
export const profileData = writable<ProfileData>({ profiles: [], profile: -1 })
export const profile = writable<Profile | undefined>({
id: -1,
instance: DEFAULT_INSTANCE_URL,
})
export const profileData = writable<ProfileData>(
getFromStorage<ProfileData>('profileData') ?? { profiles: [], profile: -1 }
)
profileData.subscribe((pd) => {
setFromStorage('profileData', pd)
})
export const profile = writable<Profile | undefined>(getProfile())
profile.subscribe(async (p) => {
if (!p || !p.jwt) {
profileData.update((pd) => ({ ...pd, profile: -1 }))
@@ -117,12 +117,35 @@ const userToPersonData = (user: MyUserInfo): PersonData => ({
reports: 0,
})
function getProfileData() {
const pd = getFromStorage<ProfileData>('profileData')!
function getProfile() {
const id = get(profileData).profile
profileData.set(pd)
console.log('id is', id)
// returns something that isn't -1
console.log(get(profileData))
if (id == -1) {
console.log('returning, since ID is', id)
// still logs :/
return
}
console.log('fetching user of id', id)
const pd = get(profileData)
console.log('pd', pd)
return pd.profiles.find((p) => p.id == id)
}
getProfileData()
export function setUserID(id: number) {
const pd = get(profileData)
console.log('pd', pd)
const prof = pd.profiles.find((p) => p.id == id)
profile.set(prof)
profileData.update((p) => ({ ...p, profile: id }))
console.log('new', pd)
return prof
}
+27 -7
View File
@@ -1,4 +1,5 @@
<script lang="ts">
import { profile, profileData, setUserID } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import Link from '$lib/components/input/Link.svelte'
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
@@ -18,6 +19,7 @@
import {
ArrowLeftOnRectangle,
ArrowRightOnRectangle,
Beaker,
Bookmark,
Cog6Tooth,
GlobeAlt,
@@ -174,8 +176,8 @@
<li class="text-xs opacity-80 text-left mx-4 my-1 py-1">
{$user ? $user.local_user_view.person.name : 'Profile'}
</li>
{#if $user && $authData}
<MenuButton
{#if $profile}
<!-- <MenuButton
link
href="/u/{$user.local_user_view.person.name}@{$authData.instance}"
>
@@ -203,12 +205,30 @@
color="dangerSecondary"
>
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out
</MenuButton>
</MenuButton> -->
{/if}
{#if !$user || !$authData}
<MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
Log in
{#if !$profile?.user}
{#if $profileData.profiles.length == 0}
<MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
Log in
</MenuButton>
{:else}
<MenuButton
on:click={() => {
const acc = Number(prompt('user id'))
setUserID(acc)
}}
>
<Icon src={ArrowLeftOnRectangle} mini width={16} />
change account
</MenuButton>
{/if}
{:else}
<MenuButton on:click={() => console.log($profile)}>
<Icon src={Beaker} mini width={16} />
Log user
</MenuButton>
{/if}
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />