From e80a83cf87eb79be4c2ac016d3c97709d41ee353 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Wed, 24 Jan 2024 20:43:41 -0800
Subject: feat(identity): use global store

---
 src/routes/+layout.svelte | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

(limited to 'src/routes/+layout.svelte')

diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte
index 5c01cb98..3b243232 100644
--- a/src/routes/+layout.svelte
+++ b/src/routes/+layout.svelte
@@ -2,7 +2,7 @@
 	import { env } from '$env/dynamic/public';
 	import { userIdentity as getUserIdentity } from '$lib/AniList/identity';
 	import { onMount } from 'svelte';
-	import userIdentity from '$stores/userIdentity';
+	import userIdentity from '$stores/identity';
 	import settings from '$stores/settings';
 	import { browser } from '$app/environment';
 	import HeadTitle from '$lib/Home/HeadTitle.svelte';
@@ -27,11 +27,6 @@
 
 	$: i18nLocale.set($settings.displayLanguage);
 
-	let currentUserIdentity = {
-		name: '',
-		id: -1,
-		avatar: 'https://s4.anilist.co/file/anilistcdn/user/avatar/large/default.png'
-	};
 	const navigationOrder = ['/', '/completed', '/schedule', '/updates', '/tools', '/settings'];
 	const previousPage: Readable<string | null> = readable(null, (set) => {
 		const unsubscribe = navigating.subscribe(($navigating) => {
@@ -59,12 +54,7 @@
 		settings.get();
 
 		if (data.user !== undefined) {
-			if ($userIdentity === '') {
-				userIdentity.set(JSON.stringify(await getUserIdentity(data.user)));
-			}
-
-			currentUserIdentity = JSON.parse($userIdentity);
-			currentUserIdentity.name = currentUserIdentity.name;
+			if ($userIdentity.id === -1) userIdentity.set(await getUserIdentity(data.user));
 		}
 	});
 </script>
@@ -88,7 +78,7 @@
 			<span style="opacity: 50%;" class="header-item">|</span>
 
 			{#if data.user}
-				<a href={root(`/user/${currentUserIdentity.name}`)} class="header-item">
+				<a href={root(`/user/${$userIdentity.name}`)} class="header-item">
 					{$locale().navigation.profile}
 				</a>
 			{/if}
@@ -111,8 +101,8 @@
 					{$locale().navigation.logOut}
 				</a>
 				{#if data.user}
-					<a href={root(`/user/${currentUserIdentity.name}`)} class="header-item">
-						<img id="avatar" src={currentUserIdentity.avatar} alt="Avatar" />
+					<a href={root(`/user/${$userIdentity.name}`)} class="header-item">
+						<img id="avatar" src={$userIdentity.avatar} alt="Avatar" />
 					</a>
 				{/if}
 			{/if}
@@ -122,7 +112,9 @@
 	<p />
 
 	<Notifications item={Notification} zIndex={5000}>
-		<Root {data} {way}><slot /></Root>
+		{#if $userIdentity.id !== -1}
+			<Root {data} {way}><slot /></Root>
+		{/if}
 	</Notifications>
 </div>
 
-- 
cgit v1.2.3

