From 79621f67ef4c778b5ee13e0d95ac5ca51c8b60b1 Mon Sep 17 00:00:00 2001 From: Wolfie <32025746+Wolfkid200444@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:58:40 -0400 Subject: [PATCH] Setup Cache / Option to Add notes other notebooks --- .../holynotes/components/modals/Error.tsx | 2 +- .../holynotes/components/modals/HelpModal.tsx | 6 +- .../holynotes/components/modals/Notebook.tsx | 35 ++------- .../components/modals/NotebookDeleteModal.tsx | 4 +- .../components/modals/RenderMessage.tsx | 20 ++++- src/plugins/holynotes/index.tsx | 27 +++++-- src/plugins/holynotes/noteHandler.ts | 77 ++++++++++++------- src/plugins/holynotes/types.ts | 2 - src/plugins/holynotes/utils.ts | 30 +++++++- 9 files changed, 131 insertions(+), 72 deletions(-) diff --git a/src/plugins/holynotes/components/modals/Error.tsx b/src/plugins/holynotes/components/modals/Error.tsx index 3f8e9c9de..da51b4882 100644 --- a/src/plugins/holynotes/components/modals/Error.tsx +++ b/src/plugins/holynotes/components/modals/Error.tsx @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -import { findByCodeLazy, findByProps } from "@webpack"; +import { findByProps } from "@webpack"; export default ({ error }: { error?: Error; } = {}) => { diff --git a/src/plugins/holynotes/components/modals/HelpModal.tsx b/src/plugins/holynotes/components/modals/HelpModal.tsx index c61761047..a8ec58079 100644 --- a/src/plugins/holynotes/components/modals/HelpModal.tsx +++ b/src/plugins/holynotes/components/modals/HelpModal.tsx @@ -4,12 +4,12 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalRoot, ModalSize } from "@utils/modal"; +import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps,ModalRoot, ModalSize } from "@utils/modal"; import { findByProps } from "@webpack"; -import { Button, Forms, Text, TextArea } from "@webpack/common"; +import { Button, Forms, Text } from "@webpack/common"; import noteHandler from "plugins/holynotes/noteHandler"; -export default ({ onClose, ...modalProps }: { onClose: () => void; }) => { +export default ({ onClose, ...modalProps }: ModalProps & { onClose: () => void; }) => { const { colorStatusGreen } = findByProps("colorStatusGreen"); return ( diff --git a/src/plugins/holynotes/components/modals/Notebook.tsx b/src/plugins/holynotes/components/modals/Notebook.tsx index 8bb6a57bb..8b6a9a49f 100644 --- a/src/plugins/holynotes/components/modals/Notebook.tsx +++ b/src/plugins/holynotes/components/modals/Notebook.tsx @@ -6,7 +6,7 @@ import ErrorBoundary from "@components/ErrorBoundary"; import { classes } from "@utils/misc"; -import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalRoot, ModalSize, openModal } from "@utils/modal"; +import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal"; import { findByProps } from "@webpack"; import { ContextMenuApi, Flex, Menu, React, TabBar, Text, TextInput } from "@webpack/common"; import noteHandler from "plugins/holynotes/noteHandler"; @@ -14,9 +14,9 @@ import { HolyNotes } from "plugins/holynotes/types"; import HelpIcon from "../icons/HelpIcon"; import Errors from "./Error"; +import HelpModal from "./HelpModal"; import ManageNotebookButton from "./ManageNotebookButton"; import { RenderMessage } from "./RenderMessage"; -import HelpModal from "./HelpModal"; const renderNotebook = ({ notes, notebook, updateParent, sortDirection, sortType, searchInput, closeModal @@ -29,7 +29,7 @@ const renderNotebook = ({ searchInput: string; closeModal: () => void; }) => { - const messageArray = Object.values(notes).map((note) => ( + const messageArray = Object.values(notes).map(note => ( + const filteredMessages = messageArray.filter(message => message.props.note.content.toLowerCase().includes(searchInput.toLowerCase()), ); - return filteredMessages; + return filteredMessages.length > 0 ? filteredMessages : ; }; -export const NoteModal = (props) => { +export const NoteModal = (props: ModalProps & { onClose: () => void; }) => { const [sortType, setSortType] = React.useState(true); const [searchInput, setSearch] = React.useState(""); const [sortDirection, setSortDirection] = React.useState(true); const [currentNotebook, setCurrentNotebook] = React.useState("Main"); - const [notes, setNotes] = React.useState({}); - const [notebooks, setNotebooks] = React.useState([]); const { quickSelect, quickSelectLabel, quickSelectQuick, quickSelectValue, quickSelectArrow } = findByProps("quickSelect"); const forceUpdate = React.useReducer(() => ({}), {})[1] as () => void; - React.useEffect(() => { - const update = async () => { - const notes = await noteHandler.getNotes(currentNotebook); - setNotes(notes); - }; - update(); - }, [currentNotebook]); - - React.useEffect(() => { - async function fetchNotebooks() { - console.log(await noteHandler.getNotebooks()); - const notebooks = await noteHandler.getNotebooks(); - setNotebooks(notebooks); - } - - fetchNotebooks(); - }, []); - + const notes = noteHandler.getNotes(currentNotebook); if (!notes) return <>; return ( @@ -120,7 +101,7 @@ export const NoteModal = (props) => { className={classes("vc-notebook-tabbar-bar", "vc-notebook-tabbar")} selectedItem={currentNotebook} onItemSelect={setCurrentNotebook}> - {notebooks.map(notebook => ( + {Object.keys(noteHandler.getAllNotes()).map(notebook => ( {notebook} diff --git a/src/plugins/holynotes/components/modals/NotebookDeleteModal.tsx b/src/plugins/holynotes/components/modals/NotebookDeleteModal.tsx index 2cc0f2cd1..f7a9592e8 100644 --- a/src/plugins/holynotes/components/modals/NotebookDeleteModal.tsx +++ b/src/plugins/holynotes/components/modals/NotebookDeleteModal.tsx @@ -5,14 +5,14 @@ */ import ErrorBoundary from "@components/ErrorBoundary"; -import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalRoot, ModalSize } from "@utils/modal"; +import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps,ModalRoot, ModalSize } from "@utils/modal"; import { Button, React, Text } from "@webpack/common"; import noteHandler from "plugins/holynotes/noteHandler"; import Error from "./Error"; import { RenderMessage } from "./RenderMessage"; -export default ({ onClose, notebook, ...props }: { onClose: () => void; notebook: string; }) => { +export default ({ onClose, notebook, ...props }: ModalProps & { onClose: () => void; notebook: string; }) => { const [notes, setNotes] = React.useState({}); React.useEffect(() => { diff --git a/src/plugins/holynotes/components/modals/RenderMessage.tsx b/src/plugins/holynotes/components/modals/RenderMessage.tsx index 64a7fade9..92757a68b 100644 --- a/src/plugins/holynotes/components/modals/RenderMessage.tsx +++ b/src/plugins/holynotes/components/modals/RenderMessage.tsx @@ -5,6 +5,7 @@ */ import { classes } from "@utils/misc"; +import { ModalProps } from "@utils/modal"; import { findByCode, findByProps } from "@webpack"; import { ContextMenuApi, FluxDispatcher, Menu, NavigationRouter, React } from "@webpack/common"; import noteHandler from "plugins/holynotes/noteHandler"; @@ -111,7 +112,13 @@ export const RenderMessage = ({ ); }; -const NoteContextMenu = (props) => { +const NoteContextMenu = ( + props: ModalProps & { + updateParent?: () => void; + notebook: string; + note: HolyNotes.Note; + closeModal?: () => void; + }) => { const { note, notebook, updateParent, closeModal } = props; return ( @@ -130,7 +137,16 @@ const NoteContextMenu = (props) => { closeModal?.(); }} /> - ); + { + noteHandler.deleteNote(note.id, notebook); + updateParent?.(); + }} + /> + + ); }}> ); }; diff --git a/src/plugins/holynotes/index.tsx b/src/plugins/holynotes/index.tsx index 5bdff71f1..882040e50 100644 --- a/src/plugins/holynotes/index.tsx +++ b/src/plugins/holynotes/index.tsx @@ -19,20 +19,31 @@ import "./style.css"; import { NavContextMenuPatchCallback } from "@api/ContextMenu"; +import { DataStore } from "@api/index"; import { addButton, removeButton } from "@api/MessagePopover"; import { Devs } from "@utils/constants"; import { openModal } from "@utils/modal"; import definePlugin from "@utils/types"; -import Message from "discord-types/general"; +import { Menu } from "@webpack/common"; import { Popover as NoteButtonPopover } from "./components/icons/NoteButton"; import { NoteModal } from "./components/modals/Notebook"; -import noteHandler from "./noteHandler"; -import { HolyNoteStore } from "./utils"; +import noteHandler, { noteHandlerCache } from "./noteHandler"; +import { DataStoreToCache } from "./utils"; const messageContextMenuPatch: NavContextMenuPatchCallback = async (children, { message }: { message: Message; }) => { - - //console.log(await noteHandler.getAllNotes()); + children.push( + + {Object.keys(noteHandler.getAllNotes()).map((notebook: string, index: number) => ( + noteHandler.addNote(message, notebook)} + /> + ))} + + ); }; @@ -51,10 +62,12 @@ export default definePlugin({ contextMenus: { "message": messageContextMenuPatch }, - store: HolyNoteStore, async start() { - addButton("HolyNotes", (message) => { + if (await DataStore.keys().then(keys => !keys.includes("Main"))) return noteHandler.newNoteBook("Main"); + if (!noteHandlerCache.has("Main")) await DataStoreToCache(); + + addButton("HolyNotes", message => { return { label: "Save Note", icon: NoteButtonPopover, diff --git a/src/plugins/holynotes/noteHandler.ts b/src/plugins/holynotes/noteHandler.ts index 6caae5d4f..457560caa 100644 --- a/src/plugins/holynotes/noteHandler.ts +++ b/src/plugins/holynotes/noteHandler.ts @@ -5,13 +5,14 @@ */ import { DataStore } from "@api/index"; -import { ChannelStore, Toasts, UserStore, lodash, showToast } from "@webpack/common"; +import { findByCode } from "@webpack"; +import { ChannelStore, lodash, Toasts, UserStore } from "@webpack/common"; import { Channel, Message } from "discord-types/general"; import { Discord, HolyNotes } from "./types"; -import { HolyNoteStore } from "./utils"; -import { findByCode } from "@webpack"; +import { saveCacheToDataStore } from "./utils"; +export const noteHandlerCache = new Map(); export default new (class NoteHandler { private _formatNote(channel: Channel, message: Message): HolyNotes.Note { @@ -38,27 +39,26 @@ export default new (class NoteHandler { } - public async getNotes(notebook?: string): Promise> { - if (await DataStore.keys().then(keys => keys.includes(notebook))) { - return await DataStore.get(notebook) ?? {}; - } else { - return this.newNoteBook(notebook).then(() => this.getNotes(notebook)); + public getNotes(notebook?: string): Record { + return noteHandlerCache.get(notebook); + } + + public getAllNotes(): HolyNotes.Note[] { + const data = noteHandlerCache.entries(); + const notes = {}; + for (const [key, value] of data) { + notes[key] = value; } - } - - public async getAllNotes(): Promise { - return await DataStore.entries(); - } - - public async getNotebooks(): Promise { - return await DataStore.keys(); + return notes; } public addNote = async (message: Message, notebook: string) => { - const notes = await this.getNotes(notebook); + const notes = this.getNotes(notebook); const channel = ChannelStore.getChannel(message.channel_id); const newNotes = Object.assign({ [message.id]: this._formatNote(channel, message) }, notes); - await DataStore.set(notebook, newNotes); + + noteHandlerCache.set(notebook, newNotes); + saveCacheToDataStore(notebook, newNotes); Toasts.show({ id: Toasts.genId(), @@ -70,7 +70,8 @@ export default new (class NoteHandler { public deleteNote = async (noteId: string, notebook: string) => { const notes = this.getNotes(notebook); - await DataStore.set(notebook, lodash.omit(notes, noteId)); + noteHandlerCache.set(notebook, lodash.omit(notes, noteId)); + saveCacheToDataStore(notebook, lodash.omit(notes, noteId)); Toasts.show({ id: Toasts.genId(), @@ -85,8 +86,12 @@ export default new (class NoteHandler { newNoteBook[note.id] = note; - await DataStore.set(from, lodash.omit(origNotebook, note.id)); - await DataStore.set(to, newNoteBook); + noteHandlerCache.set(from, lodash.omit(origNotebook, note.id)); + noteHandlerCache.set(to, newNoteBook); + + saveCacheToDataStore(from, lodash.omit(origNotebook, note.id)); + saveCacheToDataStore(to, newNoteBook); + Toasts.show({ id: Toasts.genId(), @@ -96,7 +101,16 @@ export default new (class NoteHandler { }; public newNoteBook = async (notebookName: string) => { - if (await DataStore.keys().then(keys => keys.includes(notebookName))) { + let notebookExists = false; + + for (const key of noteHandlerCache.keys()) { + if (key === notebookName) { + notebookExists = true; + break; + } + } + + if (notebookExists) { Toasts.show({ id: Toasts.genId(), message: `Notebook ${notebookName} already exists.`, @@ -104,7 +118,10 @@ export default new (class NoteHandler { }); return; } - await DataStore.set(notebookName, {}); + + noteHandlerCache.set(notebookName, {}); + saveCacheToDataStore(notebookName, {} as HolyNotes.Note[]); + return Toasts.show({ id: Toasts.genId(), message: `Successfully created ${notebookName}.`, @@ -113,7 +130,9 @@ export default new (class NoteHandler { }; public deleteNotebook = async (notebookName: string) => { - await DataStore.del(notebookName); + noteHandlerCache.delete(notebookName); + saveCacheToDataStore(notebookName); + Toasts.show({ id: Toasts.genId(), message: `Successfully deleted ${notebookName}.`, @@ -122,10 +141,12 @@ export default new (class NoteHandler { }; public refreshAvatars = async () => { - const notebooks = await this.getAllNotes(); + const notebooks = this.getAllNotes(); const User = findByCode("tag", "isClyde"); + + for (const notebook in notebooks) for (const noteId in notebooks[notebook]) { const note = notebooks[notebook][noteId]; @@ -138,7 +159,11 @@ export default new (class NoteHandler { }); } - for (const notebook in notebooks) await DataStore.set(notebook, notebooks[notebook]); + for (const notebook in notebooks) { + noteHandlerCache.set(notebook, notebooks[notebook]); + saveCacheToDataStore(notebook, notebooks[notebook]); + } + Toasts.show({ id: Toasts.genId(), message: "Successfully refreshed avatars.", diff --git a/src/plugins/holynotes/types.ts b/src/plugins/holynotes/types.ts index 8fa9fec70..0163cf60c 100644 --- a/src/plugins/holynotes/types.ts +++ b/src/plugins/holynotes/types.ts @@ -45,5 +45,3 @@ export declare namespace HolyNotes { stickerItems: Discord.Sticker[]; } } - - diff --git a/src/plugins/holynotes/utils.ts b/src/plugins/holynotes/utils.ts index 172bb9efe..003a8cdd5 100644 --- a/src/plugins/holynotes/utils.ts +++ b/src/plugins/holynotes/utils.ts @@ -4,7 +4,33 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -import { createStore } from "@api/DataStore"; +import { DataStore } from "@api/index"; + +import { noteHandlerCache } from "./noteHandler"; +import { HolyNotes } from "./types"; -export const HolyNoteStore = createStore("HolyNotesData", "HolyNotesStore"); + +export async function saveCacheToDataStore(key: string, value?: HolyNotes.Note[]) { + await DataStore.set(key, value); + +} + +export async function getFormatedEntries() { + const data = await DataStore.entries(); + const notebooks: Record = {}; + + data.forEach(function (note) { + notebooks[note[0]] = note[1]; + }); + + return notebooks; +} + +export async function DataStoreToCache() { + const data = await DataStore.entries(); + + data.forEach(function (note) { + noteHandlerCache.set(note[0], note[1]); + }); +}