Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: initialize state on app launch

thecodrr723613771b39
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index e7c08b0b3..a3fc5dfca 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -8,6 +8,7 @@ import "./app.css";
import { usePersistentState } from "./utils/hooks";
import { ev } from "./common";
import { useTheme } from "emotion-theming";
+import useStore from "./common/store";
const NavMenuItem = props => {
const [fill, setFill] = useState();
@@ -75,17 +76,15 @@ function App() {
0
);
const [show, setShow] = usePersistentState("navContainerState", true);
- const [sideMenuOpen, setSideMenuOpen] = useState(false);
+ const initStore = useStore(state => state.init);
+ const isSideMenuOpen = useStore(state => state.isSideMenuOpen);
+ useEffect(() => {
+ initStore();
+ }, [initStore]);
+
useEffect(() => {
RootNavigator.navigate(Object.keys(RootNavigator.routes)[selectedIndex]);
- function openSideMenu() {
- setSideMenuOpen(true);
- }
- ev.addListener("openSideMenu", openSideMenu);
- return () => {
- ev.removeListener("openSideMenu", openSideMenu);
- };
- });
+ }, []);
return (
<ThemeProvider>
<Flex
@@ -103,7 +102,7 @@ function App() {
borderRightColor: "primary",
minWidth: ["85%", 50, 50],
maxWidth: ["85%", 50, 50],
- display: [sideMenuOpen ? "flex" : "none", "flex", "flex"],
+ display: [isSideMenuOpen ? "flex" : "none", "flex", "flex"],
position: ["absolute", "relative", "relative"],
overflow: "scroll",
scrollbarWidth: "none",
diff --git a/apps/web/src/common/store.js b/apps/web/src/common/store.js
index 7ac8b3201..1c8a4d30e 100644
--- a/apps/web/src/common/store.js
+++ b/apps/web/src/common/store.js
@@ -2,7 +2,14 @@ import { db } from "./index";
import create from "zustand";
const [useStore] = create(set => ({
+ init: () => {
+ console.log("initializing state...");
+ set({
+ notebooks: db.notebooks.all
+ });
+ },
notebooks: [],
+ isSideMenuOpen: false,
addNotebook: async notebook => {
if (await db.notebooks.add(notebook)) {
set({ notebooks: db.notebooks.all });
diff --git a/apps/web/src/components/dialogs/add-notebook-dialog.js b/apps/web/src/components/dialogs/add-notebook-dialog.js
index 4fa3b4805..1e1e15701 100644
--- a/apps/web/src/components/dialogs/add-notebook-dialog.js
+++ b/apps/web/src/components/dialogs/add-notebook-dialog.js
@@ -2,7 +2,7 @@ import React from "react";
import { Flex, Box, Text, Button as RebassButton } from "rebass";
import { Input, Checkbox, Label } from "@rebass/forms";
import * as Icon from "react-feather";
-import Dialog, { showDialog } from "./dialog";
+import Dialog from "./dialog";
import { showSnack } from "../snackbar";
export default class AddNotebookDialog extends React.Component {
@@ -27,7 +27,7 @@ export default class AddNotebookDialog extends React.Component {
const props = this.props;
return (
<Dialog
- open={props.isOpen}
+ isOpen={props.isOpen}
title="Notebook"
icon={Icon.BookOpen}
content={
@@ -51,7 +51,7 @@ export default class AddNotebookDialog extends React.Component {
Topics (optional):
</Text>
<Box sx={{ maxHeight: 44 * 5, overflowY: "auto", marginBottom: 1 }}>
- {this.topics.map((value, index) => (
+ {this.state.topics.map((value, index) => (
<Flex
key={index.toString()}
flexDirection="row"
@@ -60,7 +60,7 @@ export default class AddNotebookDialog extends React.Component {
<Input
ref={ref => (this._inputRefs[index] = ref)}
variant="default"
- value={this.topics[index]}
+ value={this.state.topics[index]}
placeholder="Topic name"
onFocus={e => {
this.lastLength = e.nativeEvent.target.value.length;
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
index 02137fbf4..10ba57c91 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -14,7 +14,7 @@ export default class Dialog extends React.Component {
<ThemeProvider>
{theme => (
<Modal
- isOpen={true}
+ isOpen={props.isOpen || false}
shouldCloseOnOverlayClick={true}
onRequestClose={props.closeCick}
style={{
diff --git a/apps/web/src/views/Notebooks.js b/apps/web/src/views/Notebooks.js
index 0d4a6807a..7410a1005 100644
--- a/apps/web/src/views/Notebooks.js
+++ b/apps/web/src/views/Notebooks.js
@@ -52,7 +52,7 @@ const Notebooks = props => {
);
};
-const NotebooksContainer = props => {
+const NotebooksContainer = () => {
useEffect(() => {
const NotebookNavigator = require("../navigation/navigators/nbnavigator")
.default;