From c789c914aa3fe81013a5f90f6f2cb10018686b1f Mon Sep 17 00:00:00 2001 From: Artemy Date: Mon, 3 Apr 2023 17:03:01 +0300 Subject: [PATCH] feat: settings --- index.js | 3 ++ src/App.jsx | 4 +++ src/components/button.jsx | 23 +++++++++++- src/components/menu.jsx | 39 +++++++-------------- src/components/note.jsx | 4 ++- src/components/settingsInputs.jsx | 19 ++++++++++ src/pages/create.jsx | 52 +++++++++++---------------- src/pages/note.jsx | 58 ++++++++++--------------------- src/pages/notes.jsx | 18 ++++------ src/pages/pubError.jsx | 18 +++++----- src/pages/pubNote.jsx | 25 +++++-------- src/pages/pubNoteSafe.jsx | 19 ++++------ src/pages/save-local.jsx | 5 +++ src/pages/settings.jsx | 26 ++++++++++++++ 14 files changed, 165 insertions(+), 148 deletions(-) create mode 100644 src/components/settingsInputs.jsx create mode 100644 src/pages/settings.jsx diff --git a/index.js b/index.js index 5084270..d86e3c7 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,9 @@ app.post("/publish", function (req, res) { if (isValidNote(req.body)) { let hash = sha3(JSON.stringify(req.body)); req.body.time = Date.now(); + req.body.pub = true; + req.body.pubTime = req.body.time; + try { fs.writeFileSync( `./notes/${hash}.json`, diff --git a/src/App.jsx b/src/App.jsx index c1cd648..bd568eb 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -11,6 +11,7 @@ import PubError from "./pages/pubError"; import PubNoteSafe from "./pages/pubNoteSafe"; import RenderMarkdown from "./components/markdown"; import socket from "./components/socket"; +import Settings from "./pages/settings"; function App() { Storage.prototype.setObj = function (key, obj) { @@ -20,6 +21,8 @@ function App() { return JSON.parse(this.getItem(key)) || {}; }; + window.settings = localStorage.getObj("settings") || {}; + return (
@@ -32,6 +35,7 @@ function App() { } /> } /> } /> + } /> + + } + > + {props.text} + + + ); +} + +export { Button, IconWithText, ButtonWithIcon }; diff --git a/src/components/menu.jsx b/src/components/menu.jsx index b80c5ae..7c2f555 100644 --- a/src/components/menu.jsx +++ b/src/components/menu.jsx @@ -1,4 +1,4 @@ -import { Button, IconWithText } from "./button"; +import { ButtonWithIcon } from "./button"; import { MagnifyingGlassCircleIcon, PencilIcon, @@ -9,31 +9,18 @@ import { function Menu() { return (
- - - + + + +
); } diff --git a/src/components/note.jsx b/src/components/note.jsx index e10f44b..f358fe7 100644 --- a/src/components/note.jsx +++ b/src/components/note.jsx @@ -9,7 +9,9 @@ function Note({ note }) { {note.name}
- {printDate(note.time)} + {`${printDate(note.time)} ${ + note.pub ? "| Публичная" : "| Локальная" + }`}
diff --git a/src/components/settingsInputs.jsx b/src/components/settingsInputs.jsx new file mode 100644 index 0000000..dca9ef2 --- /dev/null +++ b/src/components/settingsInputs.jsx @@ -0,0 +1,19 @@ +import { CheckBox } from "./checkbox"; + +function SettingsCheckBox({ label, title, className, settingName, onClick }) { + return ( + { + window.settings[settingName] = e.target.checked; + localStorage.setObj("settings", window.settings); + onClick(e); + }} + /> + ); +} + +export { SettingsCheckBox }; diff --git a/src/pages/create.jsx b/src/pages/create.jsx index 7421383..201af01 100644 --- a/src/pages/create.jsx +++ b/src/pages/create.jsx @@ -1,4 +1,4 @@ -import { Button, IconWithText } from "../components/button"; +import { ButtonWithIcon } from "../components/button"; import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline"; import { CheckBox } from "../components/checkbox"; import { useState } from "react"; @@ -12,21 +12,16 @@ import rehypeParse from "rehype-parse"; import remarkStringify from "remark-stringify"; import remarkGfm from "remark-gfm"; import remarkMath from "remark-math"; +import { SettingsCheckBox } from "../components/settingsInputs"; function CreateNote() { const [preview, setPreview] = useState(false); - const [publicState, setPublicState] = useState(true); + const [publicState, setPublicState] = useState(settings.publicNote); const [text, setText] = useState(localStorage.getItem("NoteText")); const [name, setName] = useState(localStorage.getItem("NoteName")); const [date, setDate] = useState(Date.now()); - // setInterval(() => { - // if (preview) { - // setDate(Date.now()); - // } - // }, 1000); - async function previewChange(val) { let md = await unified() .use(remarkGfm) @@ -45,16 +40,17 @@ function CreateNote() { return (
-

+

{`${preview ? "" : "Написать заметку"}`}

{ + onClick={(val) => { setText(localStorage.getItem("NoteText")); - setPreview(!preview); + setDate(Date.now()); + setPreview(val.target.checked); }} />
@@ -101,7 +97,7 @@ function CreateNote() { {preview && (
{text} @@ -111,30 +107,24 @@ function CreateNote() { )}
- { - setPublicState(!publicState); + checked={settings.publicNote} + settingName="publicNote" + className="justify-self-center lg:justify-self-start" + onClick={(val) => { + setPublicState(val.target.checked); }} - checked={localStorage.getItem("private")} />
- +
diff --git a/src/pages/note.jsx b/src/pages/note.jsx index 3386c72..ca85fca 100644 --- a/src/pages/note.jsx +++ b/src/pages/note.jsx @@ -1,6 +1,6 @@ import { useParams } from "react-router-dom"; import { ChevronDoubleLeftIcon, TrashIcon } from "@heroicons/react/24/outline"; -import { Button, IconWithText } from "../components/button"; +import { ButtonWithIcon } from "../components/button"; import Note from "../components/note"; function NotePage() { @@ -8,25 +8,24 @@ function NotePage() { let note = localStorage.getObj("Notes")[params.id]; - if (note) { - return ( -
- + return ( +
+ - + {note ? :
Заметки не существует.
} + {note && (
- + />
-
- ); - } else { - return ( -
- -
Заметки не существует.
-
- ); - } + )} +
+ ); } export default NotePage; diff --git a/src/pages/notes.jsx b/src/pages/notes.jsx index c09b684..2868d27 100644 --- a/src/pages/notes.jsx +++ b/src/pages/notes.jsx @@ -1,4 +1,4 @@ -import { Button, IconWithText } from "../components/button"; +import { ButtonWithIcon } from "../components/button"; import { ChevronDoubleRightIcon } from "@heroicons/react/24/outline"; import printDate from "../components/utils"; @@ -19,16 +19,12 @@ function Notes() {
{printDate(val[1].time)}
- +
diff --git a/src/pages/pubError.jsx b/src/pages/pubError.jsx index e58dcfc..89bed51 100644 --- a/src/pages/pubError.jsx +++ b/src/pages/pubError.jsx @@ -1,6 +1,6 @@ import printDate from "../components/utils"; import { ChevronDoubleLeftIcon } from "@heroicons/react/24/outline"; -import { Button, IconWithText } from "../components/button"; +import { ButtonWithIcon } from "../components/button"; import { useSearchParams } from "react-router-dom"; function PubError() { @@ -9,15 +9,13 @@ function PubError() { return (
- + +

diff --git a/src/pages/pubNote.jsx b/src/pages/pubNote.jsx index c264fc1..267ac3a 100644 --- a/src/pages/pubNote.jsx +++ b/src/pages/pubNote.jsx @@ -3,7 +3,7 @@ import { useState } from "react"; import { Navigate, useParams } from "react-router-dom"; import printDate from "../components/utils"; import { ChevronDoubleLeftIcon } from "@heroicons/react/24/outline"; -import { Button, IconWithText } from "../components/button"; +import { ButtonWithIcon } from "../components/button"; function PubNote() { let params = useParams(); @@ -37,26 +37,19 @@ function PubNote() { }); else { if (note.save !== false) { + localStorage.setItem("NotePubTime", note.time); localStorage.setItem("NoteName", note.name); - localStorage.setItem( - "NoteText", - `*(публичная заметка) (была опубликована в ${printDate( - note.time - )})* \n${note.text}` - ); + localStorage.setItem("NoteText", note.text); return ; } else { return (
- +

diff --git a/src/pages/pubNoteSafe.jsx b/src/pages/pubNoteSafe.jsx index 245503f..a4ca99f 100644 --- a/src/pages/pubNoteSafe.jsx +++ b/src/pages/pubNoteSafe.jsx @@ -1,9 +1,7 @@ -import RenderMarkdown from "../components/markdown"; import { useState } from "react"; import { useParams } from "react-router-dom"; -import printDate from "../components/utils"; import { ChevronDoubleLeftIcon } from "@heroicons/react/24/outline"; -import { Button, IconWithText } from "../components/button"; +import { ButtonWithIcon } from "../components/button"; import { CopyToClipboard } from "../components/copytocb"; import Note from "../components/note"; @@ -41,15 +39,12 @@ function PubNoteSafe() { return (
- + {note?.code === 1 && (
diff --git a/src/pages/save-local.jsx b/src/pages/save-local.jsx index 63667de..e79a141 100644 --- a/src/pages/save-local.jsx +++ b/src/pages/save-local.jsx @@ -13,6 +13,8 @@ function Save() { let id = uuidv4(); let name = localStorage.getItem("NoteName"); let text = localStorage.getItem("NoteText"); + let pubTime = Number(localStorage.getItem("NotePubTime")); + if (!name || !text) return ; let notesObj = localStorage.getObj("Notes"); @@ -21,12 +23,15 @@ function Save() { name, text, time: Date.now(), + pubTime, + pub: !!pubTime, }; localStorage.setObj("Notes", notesObj); localStorage.removeItem("NoteName"); localStorage.removeItem("NoteText"); + localStorage.removeItem("NotePubTime"); return ; } diff --git a/src/pages/settings.jsx b/src/pages/settings.jsx new file mode 100644 index 0000000..9b6ff1e --- /dev/null +++ b/src/pages/settings.jsx @@ -0,0 +1,26 @@ +import { SettingsCheckBox } from "../components/settingsInputs"; +function Settings() { + return ( +
+

+ Настройки +

+ + + + +
+ ); +} + +export default Settings;