2021-06-18 17:57:08 +01:00
|
|
|
import localForage from "localforage";
|
2021-06-18 19:25:33 +01:00
|
|
|
import { Provider } from "react-redux";
|
2021-07-05 11:23:23 +01:00
|
|
|
|
2021-06-18 17:57:08 +01:00
|
|
|
import { useEffect, useState } from "preact/hooks";
|
|
|
|
|
2021-07-05 11:23:23 +01:00
|
|
|
import { dispatch, State, store } from ".";
|
|
|
|
import { Children } from "../types/Preact";
|
|
|
|
|
2021-06-18 17:57:08 +01:00
|
|
|
interface Props {
|
2021-07-05 11:25:20 +01:00
|
|
|
children: Children;
|
2021-06-18 17:57:08 +01:00
|
|
|
}
|
|
|
|
|
2021-07-05 11:23:23 +01:00
|
|
|
export default function StateLoader(props: Props) {
|
2021-07-05 11:25:20 +01:00
|
|
|
const [loaded, setLoaded] = useState(false);
|
2021-07-05 11:23:23 +01:00
|
|
|
|
2021-07-05 11:25:20 +01:00
|
|
|
useEffect(() => {
|
|
|
|
localForage.getItem("state").then((state) => {
|
|
|
|
if (state !== null) {
|
|
|
|
dispatch({ type: "__INIT", state: state as State });
|
|
|
|
}
|
2021-06-18 17:57:08 +01:00
|
|
|
|
2021-07-05 11:25:20 +01:00
|
|
|
setLoaded(true);
|
|
|
|
});
|
|
|
|
}, []);
|
2021-06-18 17:57:08 +01:00
|
|
|
|
2021-07-05 11:25:20 +01:00
|
|
|
if (!loaded) return null;
|
|
|
|
return <Provider store={store}>{props.children}</Provider>;
|
2021-06-18 17:57:08 +01:00
|
|
|
}
|