radames's picture
add local storage keep track of played games
e61bf53
raw
history blame
834 Bytes
import { browser } from '$app/env';
import { writable } from 'svelte/store';
import type { User, SuccessPrompt } from '../types';
import { nanoid } from 'nanoid';
const initialUser: User = nanoid();
const intialCompletedPrompts: SuccessPrompt[] = [];
export const currentUser = writable<User>(
browser ? JSON.parse(localStorage['user'] || JSON.stringify(initialUser)) : initialUser
);
currentUser.subscribe((value) => {
if (browser) {
return (localStorage['user'] = JSON.stringify(value));
}
});
export const completedPromptsStore = writable<SuccessPrompt[]>(
browser
? JSON.parse(localStorage['completedPrompts'] || JSON.stringify(intialCompletedPrompts))
: intialCompletedPrompts
);
completedPromptsStore.subscribe((value) => {
if (browser) {
return (localStorage['completedPrompts'] = JSON.stringify(value));
}
});