|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
import { Route, Routes } from '@solidjs/router';
|
|
|
|
|
import { Component, createEffect, lazy } from 'solid-js';
|
|
|
|
|
import { createStore, SetStoreFunction, Store } from 'solid-js/store';
|
|
|
|
|
import { Component, createEffect, createSignal, lazy } from 'solid-js';
|
|
|
|
|
import Navbar from './ui/Navbar';
|
|
|
|
|
// Only load the components if we are navigating to them
|
|
|
|
|
const Home = lazy(() => import('./pages/Home'));
|
|
|
|
@ -12,41 +11,9 @@ export type User = {
|
|
|
|
|
login: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Helper funciton to get a global state
|
|
|
|
|
// I tried a couple other things but couldn't figure it all out in one day...
|
|
|
|
|
// Probably should be possible with Context.Provider etc.
|
|
|
|
|
// The Problem with all of them was that I could not set the User globally inside
|
|
|
|
|
// Login Component. At one point I gave up and took this shortcut (for now).
|
|
|
|
|
// Fairly modified version of: https://stackoverflow.com/a/72339551
|
|
|
|
|
export const createGlobalStore = <T extends object>(
|
|
|
|
|
storeName: string,
|
|
|
|
|
init: T
|
|
|
|
|
): [Store<T>, SetStoreFunction<T>] => {
|
|
|
|
|
const [state, setState] = createStore(init);
|
|
|
|
|
if (localStorage[storeName]) {
|
|
|
|
|
try {
|
|
|
|
|
setState(JSON.parse(localStorage[storeName]));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setState(() => init);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Used to listen to changes in state
|
|
|
|
|
// This produced a warning bc. it could create a mem leak
|
|
|
|
|
// bc. we try to create a reactive component outside of render/etc.
|
|
|
|
|
// I leave this here for future reference
|
|
|
|
|
/* createEffect(() => {
|
|
|
|
|
* localStorage.globalStore = JSON.stringify(state);
|
|
|
|
|
* }); */
|
|
|
|
|
return [state, setState];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const storeName = 'globalStore';
|
|
|
|
|
const [store, setStore] = createGlobalStore(storeName, {
|
|
|
|
|
user: { id: '', login: '' } as User,
|
|
|
|
|
});
|
|
|
|
|
const [user, setUser] = createSignal({ id: '', login: '' } as User);
|
|
|
|
|
|
|
|
|
|
const App: Component = () => {
|
|
|
|
|
createEffect(() => (localStorage[storeName] = JSON.stringify(store)));
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Navbar />
|
|
|
|
@ -59,5 +26,5 @@ const App: Component = () => {
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { store, setStore };
|
|
|
|
|
export { user, setUser };
|
|
|
|
|
export default App;
|
|
|
|
|