A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
refactor: clean up dialogs
thecodrrc335697263af
diff --git a/apps/web/src/common/store.js b/apps/web/src/common/store.jsindex 70d1e0714..7ac8b3201 100644--- a/apps/web/src/common/store.js+++ b/apps/web/src/common/store.js@@ -2,7 +2,7 @@ import { db } from "./index";import create from "zustand";const [useStore] = create(set => ({- notebooks: db.notebooks.all,+ notebooks: [],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.jsindex cadb862d5..4fa3b4805 100644--- a/apps/web/src/components/dialogs/add-notebook-dialog.js+++ b/apps/web/src/components/dialogs/add-notebook-dialog.js@@ -112,7 +112,7 @@ export default class AddNotebookDialog extends React.Component {}positiveButton={{text: "Add",- click: () => {+ onClick: () => {if (!this.title.trim().length)return showSnack("Please enter the notebook title.");props.onDone({@@ -122,7 +122,7 @@ export default class AddNotebookDialog extends React.Component {});}}}- negativeButton={{ text: "Cancel", click: props.close }}+ negativeButton={{ text: "Cancel", onClick: props.close }}/>);}diff --git a/apps/web/src/components/dialogs/confirm.js b/apps/web/src/components/dialogs/confirm.jsnew file mode 100644index 000000000..41c65322e--- /dev/null+++ b/apps/web/src/components/dialogs/confirm.js@@ -0,0 +1,35 @@+import React from "react";+import { Box, Text } from "rebass";+import Dialog, { showDialog } from "./dialog";++function Confirm(props) {+ return (+ <Dialog+ open={true}+ title={props.title}+ icon={props.icon}+ content={+ <Box my={1}>+ <Text>{props.message}</Text>+ </Box>+ }+ positiveButton={{+ text: "Yes",+ onClick: props.onYes+ }}+ negativeButton={{ text: "No", onClick: props.onNo }}+ />+ );+}++export const confirm = (icon, title, message) => {+ return showDialog(perform => (+ <Confirm+ title={title}+ message={message}+ icon={icon}+ onNo={() => perform(false)}+ onYes={() => perform(true)}+ />+ ));+};diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.jsindex ea041d8ef..02137fbf4 100644--- a/apps/web/src/components/dialogs/dialog.js+++ b/apps/web/src/components/dialogs/dialog.js@@ -77,7 +77,7 @@ export default class Dialog extends React.Component {mx={1}width={"25%"}disabled={props.positiveButton.disabled || false}- onClick={props.positiveButton.click}+ onClick={props.positiveButton.onClick}>{props.positiveButton.text || "OK"}</RebassButton>@@ -110,7 +110,7 @@ export const showDialog = dialog => {if (root) {return new Promise(resolve => {const PropDialog = dialog(perform.bind(this, resolve));- ReactDOM.render(<PropDialog />, root);+ ReactDOM.render(PropDialog, root);});}return Promise.reject("No element with id 'dialogContainer'");diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.jsdeleted file mode 100644index e2f0bd1be..000000000--- a/apps/web/src/components/dialogs/index.js+++ /dev/null@@ -1,404 +0,0 @@-import React, { useState, useEffect } from "react";-import ReactDOM from "react-dom";-import { Flex, Box, Text, Button as RebassButton, Button } from "rebass";-import { Input, Checkbox, Label } from "@rebass/forms";-import * as Icon from "react-feather";-import { db } from "../../common";-import Dialog, { showDialog } from "./dialog";--/* import React, { useState, useEffect } from "react";-import ReactDOM from "react-dom";-import { Flex, Box, Text, Button as RebassButton, Button } from "rebass";-import { Input, Checkbox, Label } from "@rebass/forms";-import * as Icon from "react-feather";-import { db } from "../../common";--const inputRefs = [];-export const CreateNotebookDialog = props => {- const [topics, setTopics] = useState([""]);- const addTopic = index => {- topics.splice(index + 1, 0, "");- setTopics([...topics]);- setTimeout(() => {- inputRefs[index + 1].focus();- }, 0);- };- return (- <Dialog- open={props.open}- title="Notebook"- icon={Icon.BookOpen}- content={- <Box my={1}>- <Input- variant="default"- onChange={e => (CreateNotebookDialog.title = e.target.value)}- placeholder="Enter name"- />- <Input- variant="default"- sx={{ marginTop: 1 }}- onChange={e => (CreateNotebookDialog.description = e.target.value)}- placeholder="Enter description (optional)"- />- <Label alignItems="center" my={1}>- <Checkbox variant="checkbox" />- Locked?- </Label>- <Text variant="body" fontWeight="bold" my={1}>- Topics (optional):- </Text>- <Box sx={{ maxHeight: 44 * 5, overflowY: "auto", marginBottom: 1 }}>- {topics.map((value, index) => (- <Flex- key={index.toString()}- flexDirection="row"- sx={{ marginBottom: 1 }}- >- <Input- ref={ref => (inputRefs[index] = ref)}- variant="default"- value={topics[index]}- placeholder="Topic name"- onFocus={e => {- CreateNotebookDialog.lastLength =- e.nativeEvent.target.value.length;- }}- onChange={e => {- topics[index] = e.target.value;- setTopics([...topics]);- }}- onKeyUp={e => {- if (e.nativeEvent.key === "Enter") {- addTopic(index);- } else if (- e.nativeEvent.key === "Backspace" &&- CreateNotebookDialog.lastLength === 0 &&- index > 0- ) {- topics.splice(index, 1);- setTopics([...topics]);- setTimeout(() => {- inputRefs[index - 1].focus();- }, 0);- }- CreateNotebookDialog.lastLength =- e.nativeEvent.target.value.length;- }}- />- <RebassButton- variant="tertiary"- sx={{ marginLeft: 1 }}- px={2}- py={1}- onClick={() => addTopic(index)}- >- <Box height={20}>- <Icon.Plus size={20} />- </Box>- </RebassButton>- </Flex>- ))}- </Box>- </Box>- }- positiveButton={{- text: "Done",- click: () =>- props.onDone(- topics,- CreateNotebookDialog.title,- CreateNotebookDialog.description- )- }}- negativeButton={{ text: "Cancel", click: props.close }}- />- );-};--const ConfirmationDialog = props => (- <Dialog- open={true}- title={props.title}- icon={props.icon}- content={- <Box my={1}>- <Text>{props.message}</Text>- </Box>- }- positiveButton={{- text: "Yes",- click: props.onYes- }}- negativeButton={{ text: "No", click: props.onNo }}- />-);--const LoginDialog = props => (- <Dialog- open={true}- title={props.title}- icon={props.icon}- onCloseClick={props.onCloseClick}- content={- <Box my={1}>- <Input variant="default" placeholder="Email"></Input>- <Input- variant="default"- placeholder="Password"- sx={{ marginTop: 2 }}- ></Input>- <Button width={1} my={2}>- Login- </Button>- <Flex flexDirection="row" justifyContent="space-between">- <Button variant="links">Create a New Account</Button>- <Button variant="links" alignItems="right">- Forgot password?- </Button>- </Flex>- </Box>- }- />-);--export const showSignInDialog = (icon, title, message) => {- const root = document.getElementById("dialogContainer");- const perform = (result, resolve) => {- Dialog.close();- ReactDOM.unmountComponentAtNode(root);- resolve(result);- };- if (root) {- return new Promise((resolve, _) => {- ReactDOM.render(- <LoginDialog- onCloseClick={() => {- perform(false, resolve);- }}- title={title}- message={message}- icon={icon}- />,- root- );- });- }- return Promise.reject("No element with id 'dialogContainer'");-};--export const ask = (icon, title, message) => {- const root = document.getElementById("dialogContainer");- const perform = (result, resolve) => {- Dialog.close();- ReactDOM.unmountComponentAtNode(root);- resolve(result);- };- if (root) {- return new Promise((resolve, _) => {- ReactDOM.render(- <ConfirmationDialog- id="confirmationDialog"- title={title}- message={message}- icon={icon}- onNo={() => perform(false, resolve)}- onYes={() => perform(true, resolve)}- />,- root- );- });- }- return Promise.reject("No element with id 'dialogContainer'");-};--export const MoveDialog = props => {- const [items, setItems] = useState(db.notebooks.all);- const [type, setType] = useState("notebooks");- const [title, setTitle] = useState("Notebooks");- const [mode, setMode] = useState("read");- useEffect(() => {- MoveDialog.last = [];- }, []);- return (- <Dialog- open={true}- title={"Move Note"}- icon={Icon.Move}- content={- <Box>- <Flex alignContent="center" justifyContent="space-between" my={1}>- <Flex>- {type !== "notebooks" && (- <Text- onClick={() => {- let item = MoveDialog.last.pop();- setType(item.type);- setTitle(item.title);- setItems(item.items);- }}- sx={{- ":hover": { color: "primary" },- marginRight: 2- }}- >- <Icon.ArrowLeft />- </Text>- )}- <Text variant="title">{title}</Text>- </Flex>- {type !== "notes" && (- <Text- onClick={() => {- if (mode === "write") {- setMode("read");- return;- }- setMode("write");- setTimeout(() => {- MoveDialog.inputRef.focus();- }, 0);- }}- sx={{- ":hover": { color: "primary" }- }}- >- {mode === "read" ? <Icon.Plus /> : <Icon.Minus />}- </Text>- )}- </Flex>- <Input- ref={ref => (MoveDialog.inputRef = ref)}- variant="default"- sx={{ display: mode === "write" ? "block" : "none" }}- my={1}- placeholder={type === "notebooks" ? "Notebook name" : "Topic name"}- onKeyUp={async e => {- if (e.nativeEvent.key === "Enter" && e.target.value.length > 0) {- if (type === "notebooks") {- await db.notebooks.add({- title: e.target.value- });- setItems(db.notebooks.all);- } else {- await db.notebooks- .notebook(MoveDialog.notebook.id)- .topics.add(e.target.value);- setItems(- db.notebooks.notebook(MoveDialog.notebook.id).topics- );- }- MoveDialog.inputRef.value = "";- setMode("read");- }- }}- />- <Box- sx={{- borderWidth: 1,- borderStyle: "solid",- borderColor: "border",- maxHeight: 8 * 30,- overflowY: "auto"- }}- >- {items.length ? (- items.map(v => {- return (- <Flex- sx={{- borderWidth: 1,- padding: 2,- borderBottomColor: "border",- borderBottomStyle: "solid",- ":hover": { borderBottomColor: "primary" }- }}- onClick={() => {- MoveDialog.last.push({- title: title,- items: items,- type: type- });- if (type === "notebooks") {- setType("topics");- MoveDialog.notebook = v;- setTitle(v.title);- setItems(v.topics);- } else if (type === "topics") {- setType("notes");- MoveDialog.topic = v.title;- setTitle(`${MoveDialog.notebook.title} - ${v.title}`);- setItems(- db.notebooks- .notebook(MoveDialog.notebook.id)- .topics.topic(v.title).all- );- }- }}- >- <Text sx={{ width: "80%" }}>{v.title}</Text>- {v.totalNotes !== undefined && (- <Text sx={{ width: "20%", textAlign: "right" }}>- {v.totalNotes + " Notes"}- </Text>- )}- </Flex>- );- })- ) : (- <Text- py={2}- px={2}- sx={{ textAlign: "center", fontStyle: "italic" }}- >- Nothing here- </Text>- )}- </Box>- </Box>- }- positiveButton={{- text: "Move",- click: async () => {- try {- await db.notes.move(- { id: MoveDialog.notebook.id, topic: MoveDialog.topic },- props.noteId- );- props.onMove();- } catch (e) {- console.log(e);- } finally {- props.onClose();- }- },- disabled: type !== "notes"- }}- negativeButton={{ text: "Cancel", onClick: props.onClose }}- />- );-};--export const moveNote = (noteId, notebook) => {- const root = document.getElementById("dialogContainer");- const perform = (result, resolve) => {- Dialog.close();- ReactDOM.unmountComponentAtNode(root);- resolve(result);- };- if (root) {- return new Promise((resolve, _) => {- ReactDOM.render(- <MoveDialog- noteId={noteId}- notebook={notebook}- onClose={() => perform(false, resolve)}- onMove={() => perform(true, resolve)}- />,- root- );- });- }- return Promise.reject("No element with id 'dialogContainer'");-};- */diff --git a/apps/web/src/components/dialogs/logindialog.js b/apps/web/src/components/dialogs/logindialog.jsnew file mode 100644index 000000000..60bd66f24--- /dev/null+++ b/apps/web/src/components/dialogs/logindialog.js@@ -0,0 +1,39 @@+import React, { useState, useEffect } from "react";+import ReactDOM from "react-dom";+import { Flex, Box, Text, Button as RebassButton, Button } from "rebass";+import { Input, Checkbox, Label } from "@rebass/forms";+import * as Icon from "react-feather";+import { db } from "../../common";+import Dialog, { showDialog } from "./dialog";++const LoginDialog = props => (+ <Dialog+ open={true}+ title={"Login"}+ icon={Icon.LogIn}+ onCloseClick={props.onClose}+ content={+ <Box my={1}>+ <Input variant="default" placeholder="Email"></Input>+ <Input+ variant="default"+ placeholder="Password"+ sx={{ marginTop: 2 }}+ ></Input>+ <Button width={1} my={2}>+ Login+ </Button>+ <Flex flexDirection="row" justifyContent="space-between">+ <Button variant="links">Create a New Account</Button>+ <Button variant="links" alignItems="right">+ Forgot password?+ </Button>+ </Flex>+ </Box>+ }+ />+);++export const showLogInDialog = () => {+ return showDialog(perform => <LoginDialog onClose={() => perform(false)} />);+};diff --git a/apps/web/src/components/dialogs/move-note-dialog.js b/apps/web/src/components/dialogs/move-note-dialog.jsnew file mode 100644index 000000000..72adba520--- /dev/null+++ b/apps/web/src/components/dialogs/move-note-dialog.js@@ -0,0 +1,196 @@+import React from "react";+import { Flex, Box, Text } from "rebass";+import { Input } from "@rebass/forms";+import * as Icon from "react-feather";+import { db } from "../../common";+import Dialog, { showDialog } from "./dialog";++export default class MoveDialog extends React.Component {+ history = [];+ _inputRef;+ selectedNotebook;+ selectedTopic;+ state = {+ items: [],+ type: "notebooks",+ title: "Notebooks",+ mode: "read"+ };++ render() {+ const { items, type, title, mode } = this.state;+ const props = this.props;+ return (+ <Dialog+ open={true}+ title={"Move Note"}+ icon={Icon.Move}+ content={+ <Box>+ <Flex alignContent="center" justifyContent="space-between" my={1}>+ <Flex>+ <Text+ onClick={() => {+ let item = this.history.pop();+ this.setState({ ...item });+ }}+ sx={{+ display: this.history.length ? "block" : "none",+ ":hover": { color: "primary" },+ marginRight: 2+ }}+ >+ <Icon.ArrowLeft />+ </Text>+ <Text variant="title">{title}</Text>+ </Flex>+ <Text+ onClick={() => {+ if (mode === "write") {+ this.setState({ mode: "read" });+ return;+ }+ this.setState({ mode: "write" });+ setTimeout(() => {+ this._inputRef.focus();+ }, 0);+ }}+ sx={{+ display: type === "notes" ? "none" : "block",+ ":hover": { color: "primary" }+ }}+ >+ {mode === "read" ? <Icon.Plus /> : <Icon.Minus />}+ </Text>+ </Flex>+ <Input+ ref={ref => (this._inputRef = ref)}+ variant="default"+ sx={{ display: mode === "write" ? "block" : "none" }}+ my={1}+ placeholder={+ type === "notebooks" ? "Notebook name" : "Topic name"+ }+ onKeyUp={async e => {+ if (+ e.nativeEvent.key === "Enter" &&+ e.target.value.length > 0+ ) {+ if (type === "notebooks") {+ await db.notebooks.add({+ title: e.target.value+ });+ this.setState({ items: db.notebooks.all });+ } else {+ await db.notebooks+ .notebook(MoveDialog.notebook.id)+ .topics.add(e.target.value);+ this.setState({+ items: db.notebooks.notebook(MoveDialog.notebook.id)+ .topics+ });+ }+ this._inputRef.value = "";+ this.setState({ mode: "read" });+ }+ }}+ />+ <Box+ sx={{+ borderWidth: 1,+ borderStyle: "solid",+ borderColor: "border",+ maxHeight: 8 * 30,+ overflowY: "auto"+ }}+ >+ {items.length ? (+ items.map(item => {+ return (+ <Flex+ sx={{+ borderWidth: 1,+ padding: 2,+ borderBottomColor: "border",+ borderBottomStyle: "solid",+ ":hover": { borderBottomColor: "primary" }+ }}+ onClick={() => {+ this.history.push({+ title,+ items,+ type+ });+ if (type === "notebooks") {+ this.setState({+ type: "topics",+ items: item.topics,+ title: item.title+ });+ this.selectedNotebook = item;+ } else if (type === "topics") {+ this.setState({+ type: "notes",+ title: `${this.selectedNotebook.title} - ${item.title}`,+ items: db.notebooks+ .notebook(this.notebook.id)+ .topics.topic(item.title).all+ });+ this.selectedTopic = item.title;+ }+ }}+ >+ <Text sx={{ width: "80%" }}>{item.title}</Text>+ {item.totalNotes !== undefined && (+ <Text sx={{ width: "20%", textAlign: "right" }}>+ {item.totalNotes + " Notes"}+ </Text>+ )}+ </Flex>+ );+ })+ ) : (+ <Text+ py={2}+ px={2}+ sx={{ textAlign: "center", fontStyle: "italic" }}+ >+ Nothing here+ </Text>+ )}+ </Box>+ </Box>+ }+ positiveButton={{+ text: "Move",+ click: async () => {+ try {+ await db.notes.move(+ { id: this.selectedNotebook.id, topic: this.selectedTopic },+ props.noteId+ );+ props.onMove();+ } catch (e) {+ console.log(e);+ } finally {+ props.onClose();+ }+ },+ disabled: type !== "notes"+ }}+ negativeButton={{ text: "Cancel", onClick: props.onClose }}+ />+ );+ }+}++export const showMoveNoteDialog = (noteId, notebook) => {+ return showDialog(perform => (+ <MoveDialog+ noteId={noteId}+ notebook={notebook}+ onClose={() => perform(false)}+ onMove={() => perform(true)}+ />+ ));+};diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.jsindex c9923d223..b4db57e47 100644--- a/apps/web/src/components/note/index.js+++ b/apps/web/src/components/note/index.js@@ -5,15 +5,15 @@ import TimeAgo from "timeago-react";import { db, ev } from "../../common";import { showSnack } from "../snackbar";import ListItem from "../list-item";-import { ask, moveNote } from "../dialogs";+import { confirm } from "../dialogs/confirm";+import { showMoveNoteDialog } from "../dialogs/movenotedialog";const dropdownRefs = [];const menuItems = note => [{title: note.notebook ? "Move" : "Add to",onClick: async () => {- console.log(note.id, note.notebook);- if (await moveNote(note.id, note.notebook)) {+ if (await showMoveNoteDialog(note.id)) {showSnack("Note moved successfully!");}}@@ -47,10 +47,10 @@ const menuItems = note => [title: "Move to Trash",color: "red",onClick: () => {- ask(+ confirm(Icon.Trash2,"Delete",- "Are you sure you want to move this note to Trash? It will be moved to Trash and permanently deleted after 7 days."+ "Are you sure you want to delete this note?").then(res => {if (res) {ev.emit("onClearNote", note.id);diff --git a/apps/web/src/navigation/navigators/rootnavigator.js b/apps/web/src/navigation/navigators/rootnavigator.jsindex ec34de42a..80e548aa7 100644--- a/apps/web/src/navigation/navigators/rootnavigator.js+++ b/apps/web/src/navigation/navigators/rootnavigator.js@@ -13,7 +13,7 @@ import {createDeadRoute} from "../routes";import Navigator from "../index";-import { showSignInDialog } from "../../components/dialogs";+import { showLogInDialog } from "../../components/dialogs/logindialog";import { changeTheme, isDarkTheme } from "../../utils/theme";/*For color Search*/@@ -34,7 +34,7 @@ const bottomRoutes = {isToggled: () => isDarkTheme()}),...createDeadRoute("signin", Icon.LogIn, {- onClick: () => showSignInDialog(Icon.LogIn, "Login", ""),+ onClick: () => showLogInDialog(),bottom: true}),...createRoute("settings", SettingsContainer, {diff --git a/apps/web/src/views/Favorites.js b/apps/web/src/views/Favorites.jsindex 3adea2c24..8a7af2481 100644--- a/apps/web/src/views/Favorites.js+++ b/apps/web/src/views/Favorites.js@@ -3,13 +3,13 @@ import { db, ev } from "../common";import * as Icon from "react-feather";import ListView from "../components/listview";import { showSnack } from "../components/snackbar";-import { ask } from "../components/dialogs";+import { confirm } from "../components/dialogs/confirm";const dropdownRefs = [];const menuItems = item => [{title: "Unfavorite",onClick: async () => {- ask(+ confirm(Icon.Star,"Unfavorite","Are you sure you want to remove this item from favorites?"@@ -33,7 +33,7 @@ const menuItems = item => [title: "Delete",color: "red",onClick: async () => {- ask(+ confirm(Icon.Trash2,"Delete","Are you sure you want to delete this note? It will be moved to trash and permanently deleted after 7 days."diff --git a/apps/web/src/views/Trash.js b/apps/web/src/views/Trash.jsindex 615373a1a..6b2894e0c 100644--- a/apps/web/src/views/Trash.js+++ b/apps/web/src/views/Trash.js@@ -2,7 +2,7 @@ import React from "react";import { db, ev } from "../common";import * as Icon from "react-feather";import ListView from "../components/listview";-import { ask } from "../components/dialogs";+import { confirm } from "../components/dialogs/confirm";import { showSnack } from "../components/snackbar";const dropdownRefs = [];@@ -10,7 +10,7 @@ const menuItems = item => [{title: "Restore",onClick: async () => {- ask(+ confirm(Icon.Star,"Restore",`Are you sure you want to restore this item to ${item.type}?`@@ -28,7 +28,7 @@ const menuItems = item => [title: "Delete",color: "red",onClick: async () => {- ask(+ confirm(Icon.Star,"Delete",`Are you sure you want to permanently delete this item?`