27 lines
837 B
TypeScript
27 lines
837 B
TypeScript
// src/stores.js
|
|
import { writable, type Writable } from 'svelte/store';
|
|
import { browser } from '$app/environment';
|
|
|
|
// Initialize the store with a boolean value from local storage
|
|
export const location: Writable<boolean> = writable(fromLocalStorage('location', false));
|
|
toLocalStorage(location, 'location');
|
|
|
|
function fromLocalStorage(storageKey: string, fallback: boolean): boolean {
|
|
if (browser) {
|
|
const storedValue = localStorage.getItem(storageKey);
|
|
if (storedValue !== null && storedValue !== 'undefined') {
|
|
return storedValue === 'true';
|
|
}
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
function toLocalStorage(store: Writable<boolean>, storageKey: string) {
|
|
if (browser) {
|
|
store.subscribe((value: boolean) => {
|
|
localStorage.setItem(storageKey, String(value));
|
|
});
|
|
}
|
|
}
|
|
|