diff --git a/src/lib/auth.ts b/src/lib/auth.ts index fdd5b143..ee0db380 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -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(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 } diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index a58da9d6..e0305a5a 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -153,20 +153,15 @@ slot="button" on:click={toggleOpen} > - {#if $user === undefined} -
- -
- {/if} - {#if $user} + {#if $profile?.user}
- {#if $user.unreads > 0} + {#if $profile.user.unreads > 0}
@@ -174,18 +169,19 @@ {/if}
  • - {$user ? $user.local_user_view.person.name : 'Profile'} + {$profile?.user ? $profile.user.local_user_view.person.name : 'Profile'}
  • - {#if $profile} - {/if} - {#if !$profile?.user} - {#if $profileData.profiles.length == 0} - - - Log in - - {:else} - { - const acc = Number(prompt('user id')) - - setUserID(acc) - }} - > - - change account - - {/if} + {#if !$profile?.user && $profileData.profiles.length == 0} + + + Log in + {:else} - console.log($profile)}> - - Log user + + + Switch account + + {$profileData.profiles.length} + {/if}
    diff --git a/src/lib/components/ui/list/EditableList.svelte b/src/lib/components/ui/list/EditableList.svelte new file mode 100644 index 00000000..3421f07c --- /dev/null +++ b/src/lib/components/ui/list/EditableList.svelte @@ -0,0 +1,15 @@ + + +
      + +
    diff --git a/src/lib/components/ui/list/EditableListItem.svelte b/src/lib/components/ui/list/EditableListItem.svelte new file mode 100644 index 00000000..f4331ef0 --- /dev/null +++ b/src/lib/components/ui/list/EditableListItem.svelte @@ -0,0 +1,3 @@ +
    + +
    diff --git a/src/routes/accounts/+page.svelte b/src/routes/accounts/+page.svelte new file mode 100644 index 00000000..85ebf03f --- /dev/null +++ b/src/routes/accounts/+page.svelte @@ -0,0 +1,88 @@ + + +{#if $profileData.profiles.length == 0} +
    +
    +
    + +

    No accounts

    +
    + +
    +
    +{:else} +
    +
    +

    Accounts

    + +
    + { + deleteProfile(id.detail) + }} + let:action + > + {#each $profileData.profiles as profile (profile.id)} +
    +
    + {profile.username} + + {profile.instance} + +
    +
    + + +
    + {/each} + + +
    +{/if} diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte index 8a838828..66c45883 100644 --- a/src/routes/login/+page.svelte +++ b/src/routes/login/+page.svelte @@ -41,7 +41,7 @@ }) if (response?.jwt) { - setUser(response.jwt, data.instance) + setUser(response.jwt, data.instance, data.username) toast({ content: 'Successfully logged in.', type: ToastType.success }) goto('/')