feat: add account switcher

This commit is contained in:
Xyphyn committed 2023-08-01 12:20:37 -07:00
1 parent fc1afecfdd
commit 54ab6fdc82
6 files changed
+169 -61

No files matched your search

+39 -15
View File
@@ -1,5 +1,5 @@
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { DEFAULT_INSTANCE_URL, getClient } from '$lib/lemmy.js'
import { DEFAULT_INSTANCE_URL, getClient, instance } from '$lib/lemmy.js'
import type { MyUserInfo } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
@@ -19,6 +19,7 @@ interface Profile {
instance: string
jwt?: string
user?: PersonData
username?: string
}
/**
@@ -45,6 +46,9 @@ profileData.subscribe((pd) => {
export const profile = writable<Profile | undefined>(getProfile())
profile.subscribe(async (p) => {
if (p?.instance) {
instance.set(p.instance)
}
if (!p || !p.jwt) {
profileData.update((pd) => ({ ...pd, profile: -1 }))
return
@@ -54,13 +58,13 @@ profile.subscribe(async (p) => {
// fetch the user because p.user is undefined
const user = await userFromJwt(p.jwt, p.instance)
p = {
profile.update(() => ({
...p,
user: user,
}
}))
})
export async function setUser(jwt: string, instance: string) {
export async function setUser(jwt: string, instance: string, username: string) {
try {
new URL(`https://${instance}`)
} catch (err) {
@@ -83,6 +87,7 @@ export async function setUser(jwt: string, instance: string) {
id: id,
instance: instance,
jwt: jwt,
username: username,
}
profile.set({
@@ -120,32 +125,51 @@ const userToPersonData = (user: MyUserInfo): PersonData => ({
function getProfile() {
const id = get(profileData).profile
console.log('id is', id)
// returns something that isn't -1
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)
}
export function resetProfile() {
profile.set({
id: -1,
instance: DEFAULT_INSTANCE_URL,
})
profileData.update((p) => ({ ...p, profile: -1 }))
}
export function deleteProfile(id: number) {
const pd = get(profileData)
const index = pd.profiles.findIndex((p) => p.id == id)
if (index <= -1) return
pd.profiles.splice(index, 1)
profileData.update((p) => ({
...p,
profiles: pd.profiles,
}))
if (id == get(profile)?.id) {
resetProfile()
}
}
export function setUserID(id: number) {
const pd = get(profileData)
console.log('pd', pd)
if (id == -1) {
resetProfile()
return
}
const prof = pd.profiles.find((p) => p.id == id)
profile.set(prof)
profileData.update((p) => ({ ...p, profile: id }))
console.log('new', pd)
return prof
}
+23 -45
View File
@@ -153,20 +153,15 @@
slot="button"
on:click={toggleOpen}
>
{#if $user === undefined}
<div class="w-full h-full grid place-items-center">
<Spinner width={20} />
</div>
{/if}
{#if $user}
{#if $profile?.user}
<div class="w-8 h-8 aspect-square object-cover rounded-full">
<Avatar
url={$user.local_user_view.person.avatar}
url={$profile.user.local_user_view.person.avatar}
width={32}
alt={$user.local_user_view.person.name}
alt={$profile.user.local_user_view.person.name}
/>
</div>
{#if $user.unreads > 0}
{#if $profile.user.unreads > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute top-0 left-0 z-10"
/>
@@ -174,18 +169,19 @@
{/if}
</button>
<li class="text-xs opacity-80 text-left mx-4 my-1 py-1">
{$user ? $user.local_user_view.person.name : 'Profile'}
{$profile?.user ? $profile.user.local_user_view.person.name : 'Profile'}
</li>
{#if $profile}
<!-- <MenuButton
{#if $profile?.user}
<MenuButton
link
href="/u/{$user.local_user_view.person.name}@{$authData.instance}"
href="/u/{$profile.user.local_user_view.person
.name}@{$profile.instance}"
>
<Icon src={UserCircle} mini width={16} /> Profile
</MenuButton>
<MenuButton link href="/inbox">
<div class="relative">
{#if $user.unreads > 0}
{#if $profile.user.unreads > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/>
@@ -196,39 +192,21 @@
<MenuButton link href="/saved">
<Icon src={Bookmark} mini width={16} /> Saved
</MenuButton>
<MenuButton
on:click={() => {
localStorage.removeItem('user')
user.set(undefined)
authData.set(undefined)
}}
color="dangerSecondary"
>
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out
</MenuButton> -->
{/if}
{#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}
{#if !$profile?.user && $profileData.profiles.length == 0}
<MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
Log in
</MenuButton>
{:else}
<MenuButton on:click={() => console.log($profile)}>
<Icon src={Beaker} mini width={16} />
Log user
<MenuButton link href="/accounts">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
Switch account
<span
class="text-xs font-bold bg-slate-100 dark:bg-zinc-700 px-2 py-0.5 rounded-md ml-auto"
>
{$profileData.profiles.length}
</span>
</MenuButton>
{/if}
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />
@@ -0,0 +1,15 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte'
type T = $$Generic
const dispatcher = createEventDispatcher<{ action: any }>()
function action(detail: any) {
dispatcher('action', detail)
}
</script>
<ul class="divide-y divide-slate-200 dark:divide-zinc-900 flex flex-col">
<slot {action} />
</ul>
@@ -0,0 +1,3 @@
<div class="p-2 flex flex-row">
<slot />
</div>