diff options
| author | Fuwn <[email protected]> | 2024-10-09 00:41:20 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-10-09 00:41:43 -0700 |
| commit | 998b63a35256ac985a5a2714dd1ca451af4dfd8a (patch) | |
| tree | 50796121a9d5ab0330fdc5d7e098bda2860d9726 /src/stores/identity.ts | |
| parent | feat(graphql): add badgeCount field (diff) | |
| download | due.moe-998b63a35256ac985a5a2714dd1ca451af4dfd8a.tar.xz due.moe-998b63a35256ac985a5a2714dd1ca451af4dfd8a.zip | |
chore(prettier): use spaces instead of tabs
Diffstat (limited to 'src/stores/identity.ts')
| -rw-r--r-- | src/stores/identity.ts | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/src/stores/identity.ts b/src/stores/identity.ts index c89df97b..1b0aee90 100644 --- a/src/stores/identity.ts +++ b/src/stores/identity.ts @@ -3,51 +3,51 @@ import type { UserIdentity } from '$lib/Data/AniList/identity'; import { writable } from 'svelte/store'; export const defaultIdentity: UserIdentity = { - name: '', - id: -2, - avatar: 'https://s4.anilist.co/file/anilistcdn/user/avatar/large/default.png' + name: '', + id: -2, + avatar: 'https://s4.anilist.co/file/anilistcdn/user/avatar/large/default.png' }; const createStore = () => { - const { subscribe, set, update } = writable<UserIdentity>( - JSON.parse( - browser - ? localStorage.getItem('identity') ?? JSON.stringify(defaultIdentity) - : JSON.stringify(defaultIdentity) - ) - ); - let state: UserIdentity; - - subscribe((value) => (state = value)); - - return { - subscribe, - set, - update, - reset: () => set(defaultIdentity), - get: () => { - const keys = Object.keys(defaultIdentity); - const identityKeys = Object.keys(state); - const updatedIdentity = { ...state }; - - for (const key of keys) - if (!identityKeys.includes(key)) - (updatedIdentity[key as keyof UserIdentity] as unknown) = - defaultIdentity[key as keyof UserIdentity]; - - if (browser) localStorage.setItem('identity', JSON.stringify(updatedIdentity)); - - return updatedIdentity; - }, - setKey: (key: keyof UserIdentity, value: unknown) => - update((identity) => ({ ...identity, [key]: value })) - }; + const { subscribe, set, update } = writable<UserIdentity>( + JSON.parse( + browser + ? localStorage.getItem('identity') ?? JSON.stringify(defaultIdentity) + : JSON.stringify(defaultIdentity) + ) + ); + let state: UserIdentity; + + subscribe((value) => (state = value)); + + return { + subscribe, + set, + update, + reset: () => set(defaultIdentity), + get: () => { + const keys = Object.keys(defaultIdentity); + const identityKeys = Object.keys(state); + const updatedIdentity = { ...state }; + + for (const key of keys) + if (!identityKeys.includes(key)) + (updatedIdentity[key as keyof UserIdentity] as unknown) = + defaultIdentity[key as keyof UserIdentity]; + + if (browser) localStorage.setItem('identity', JSON.stringify(updatedIdentity)); + + return updatedIdentity; + }, + setKey: (key: keyof UserIdentity, value: unknown) => + update((identity) => ({ ...identity, [key]: value })) + }; }; const identity = createStore(); identity.subscribe((value) => { - if (browser) localStorage.setItem('identity', JSON.stringify(value)); + if (browser) localStorage.setItem('identity', JSON.stringify(value)); }); export default identity; |