diff --git a/src/plugins/holynotes/components/modals/Error.tsx b/src/plugins/holynotes/components/modals/Error.tsx new file mode 100644 index 000000000..30d33a334 --- /dev/null +++ b/src/plugins/holynotes/components/modals/Error.tsx @@ -0,0 +1,50 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { findByCodeLazy } from "@webpack"; + +const classes = findByCodeLazy("emptyResultsWrap"); + +export default ({ error }: { error?: Error; } = {}) => { + if (error) { + // Error + console.log(error); + return ( +
+
+
+
+ There was an error parsing your notes! The issue was logged in your console, press CTRL + + I to access it! Please visit the support server if you need extra help! +
+
+
+ ); + } else if (Math.floor(Math.random() * 100) <= 10) + // Easter egg + return ( +
+
+
+
+ No notes were found. Empathy banana is here for you. +
+
+
+ ); + // Empty notebook + else + return ( +
+
+
+
+ No notes were found saved in this notebook. +
+
+
+ ); +}; diff --git a/src/plugins/holynotes/components/modals/Notebook.tsx b/src/plugins/holynotes/components/modals/Notebook.tsx index 946f6dee5..6cbb614c5 100644 --- a/src/plugins/holynotes/components/modals/Notebook.tsx +++ b/src/plugins/holynotes/components/modals/Notebook.tsx @@ -9,8 +9,47 @@ import { Flex } from "@components/Flex"; import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalRoot, openModal } from "@utils/modal"; import { React, TabBar, Text, TextInput } from "@webpack/common"; import noteHandler from "plugins/holynotes/noteHandler"; +import { HolyNotes } from "plugins/holynotes/types"; import HelpIcon from "../icons/HelpIcon"; +import Errors from "./Error"; +import RenderMessage from "./RenderMessage"; + +const renderNotebook = ({ + notes, notebook, updateParent, sortDirection, sortType, searchInput, closeModal +}: { + notes: Record; + notebook: string; + updateParent: () => void; + sortDirection: boolean; + sortType: boolean; + searchInput: string; + closeModal: () => void; +}) => { + const messageArray = Object.values(notes).map((note) => { + ; + }); + + if (sortType) + messageArray.sort( + (a, b) => + new Date(b.props.note?.timestamp)?.getTime() - new Date(a.props.note?.timestamp)?.getTime(), + ); + + if (sortDirection) messageArray.reverse(); + console.log(messageArray); + const filteredMessages = messageArray.filter((message) => + message.props.note.content.toLowerCase().includes(searchInput.toLowerCase()), + ); + + return filteredMessages; +}; @@ -19,9 +58,29 @@ export const NoteModal = (props) => { 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 forceUpdate = React.useReducer(() => ({}), {})[1] as () => void; - const notes = noteHandler.getNotes(currentNotebook); + + 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(); + }, []); + if (!notes) return <>; @@ -56,7 +115,7 @@ export const NoteModal = (props) => { className="notebook-tabbar-Bar notebook-tabbar" selectedItem={currentNotebook} onItemSelect={setCurrentNotebook}> - {Object.keys(noteHandler.getAllNotes()).map(notebook => ( + {notebooks.map(notebook => ( {notebook} @@ -66,7 +125,15 @@ export const NoteModal = (props) => {
- { } + {renderNotebook({ + notes, + notebook: currentNotebook, + updateParent: () => forceUpdate(), + sortDirection: sortDirection, + sortType: sortType, + searchInput: searchInput, + closeModal: props.onClose, + })} diff --git a/src/plugins/holynotes/components/modals/RenderMessage.tsx b/src/plugins/holynotes/components/modals/RenderMessage.tsx new file mode 100644 index 000000000..7905ed1bc --- /dev/null +++ b/src/plugins/holynotes/components/modals/RenderMessage.tsx @@ -0,0 +1,139 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { findByCodeLazy, findByProps, findByPropsLazy } from "@webpack"; +import { ContextMenuApi, FluxDispatcher, Menu, NavigationRouter, React } from "@webpack/common"; +import noteHandler from "plugins/holynotes/noteHandler"; +import { HolyNotes } from "plugins/holynotes/types"; + +const { message, groupStart, cozyMessage } = findByPropsLazy("cozyMessage"); +const User = findByCodeLazy("isClyde(){"); +const Message = findByCodeLazy("isEdited(){"); +const Channel = findByCodeLazy("ChannelRecordBase"); + +export default ({ + note, + notebook, + updateParent, + fromDeleteModal, + closeModal, +}: { + note: HolyNotes.Note; + notebook: string; + updateParent?: () => void; + fromDeleteModal: boolean; + closeModal?: () => void; +}) => { + const ChannelMessage = findByProps("ThreadStarterChatMessage").default; + + const [isHoldingDelete, setHoldingDelete] = React.useState(false); + + React.useEffect(() => { + const deleteHandler = (e: { key: string; type: string; }) => + e.key.toLowerCase() === "delete" && setHoldingDelete(e.type.toLowerCase() === "keydown"); + + document.addEventListener("keydown", deleteHandler); + document.addEventListener("keyup", deleteHandler); + + return () => { + document.removeEventListener("keydown", deleteHandler); + document.removeEventListener("keyup", deleteHandler); + }; + }, []); + + console.log(note, notebook, updateParent, fromDeleteModal, closeModal); + + const render = ( +
{ + if (isHoldingDelete && !fromDeleteModal) { + noteHandler.deleteNote(note.id, notebook); + updateParent?.(); + } + }} + onContextMenu={(event: any) => { + if (!fromDeleteModal) + // @ts-ignore + return open(event, (props: any) => ( + // @ts-ignore + + )); + }} + > + {/* + embed.timestamp + ? Object.assign(embed, { + // @ts-ignore + timestamp: new Moment(new Date(embed.timestamp)), + }) + : embed, + ), + }, + ), + ) + } + /> */} + +
+ ); + + console.log(render); +}; + +const NoteContextMenu = (props) => { + const { note, notebook, updateParent, closeModal } = props; + + return ( +
{ + ContextMenuApi.openContextMenu(e, () => + FluxDispatcher.dispatch({ type: "CONTEXT_MENU_CLOSE" })} + aria-label="Holy Notes" + > + { + NavigationRouter.transitionTo(`/channels/${note.guild_id ?? "@me"}/${note.channel_id}/${note.id}`); + closeModal?.(); + }} + /> + ); + }}>
+ ); +}; diff --git a/src/plugins/holynotes/index.tsx b/src/plugins/holynotes/index.tsx index f031bbc41..5bdff71f1 100644 --- a/src/plugins/holynotes/index.tsx +++ b/src/plugins/holynotes/index.tsx @@ -32,7 +32,7 @@ import { HolyNoteStore } from "./utils"; const messageContextMenuPatch: NavContextMenuPatchCallback = async (children, { message }: { message: Message; }) => { - console.log(await noteHandler.getAllNotes()); + //console.log(await noteHandler.getAllNotes()); }; diff --git a/src/plugins/holynotes/noteHandler.ts b/src/plugins/holynotes/noteHandler.ts index 7e0a4dafa..837ea21ed 100644 --- a/src/plugins/holynotes/noteHandler.ts +++ b/src/plugins/holynotes/noteHandler.ts @@ -38,25 +38,26 @@ export default new (class NoteHandler { public async getNotes(notebook?: string): Promise> { - if (await DataStore.keys(HolyNoteStore).then(keys => keys.includes(notebook))) { - return await DataStore.get(notebook, HolyNoteStore) ?? {}; + if (await DataStore.keys().then(keys => keys.includes(notebook))) { + return await DataStore.get(notebook) ?? {}; } else { return this.newNoteBook(notebook).then(() => this.getNotes(notebook)); } } public async getAllNotes(): Promise { - const data = await DataStore.values(HolyNoteStore); - const mainData = data[0]; - return mainData; + return await DataStore.entries(); + } + + public async getNotebooks(): Promise { + return await DataStore.keys(); } public addNote = async (message: Message, notebook: string) => { - const notes = this.getNotes(notebook); + const notes = await this.getNotes(notebook); const channel = ChannelStore.getChannel(message.channel_id); - const newNotes = Object.assign({ [notebook]: { [message.id]: this._formatNote(channel, message) } }, notes); - - await DataStore.set(notebook, newNotes, HolyNoteStore); + const newNotes = Object.assign({ [message.id]: this._formatNote(channel, message) }, notes); + await DataStore.set(notebook, newNotes); Toasts.show({ id: Toasts.genId(), @@ -68,7 +69,7 @@ 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), HolyNoteStore); + await DataStore.set(notebook, lodash.omit(notes, noteId)); Toasts.show({ id: Toasts.genId(), @@ -83,8 +84,8 @@ export default new (class NoteHandler { newNoteBook[note.id] = note; - await DataStore.set(from, lodash.omit(origNotebook, note.id), HolyNoteStore); - await DataStore.set(to, newNoteBook, HolyNoteStore); + await DataStore.set(from, lodash.omit(origNotebook, note.id)); + await DataStore.set(to, newNoteBook); Toasts.show({ id: Toasts.genId(), @@ -102,8 +103,8 @@ export default new (class NoteHandler { }); return; } - await DataStore.set(notebookName, {}, HolyNoteStore); - Toasts.show({ + await DataStore.set(notebookName, {}); + return Toasts.show({ id: Toasts.genId(), message: `Successfully created ${notebookName}.`, type: Toasts.Type.SUCCESS, diff --git a/src/plugins/holynotes/types.ts b/src/plugins/holynotes/types.ts index 0163cf60c..8fa9fec70 100644 --- a/src/plugins/holynotes/types.ts +++ b/src/plugins/holynotes/types.ts @@ -45,3 +45,5 @@ export declare namespace HolyNotes { stickerItems: Discord.Sticker[]; } } + + diff --git a/src/plugins/holynotes/utils.ts b/src/plugins/holynotes/utils.ts index c04435e10..172bb9efe 100644 --- a/src/plugins/holynotes/utils.ts +++ b/src/plugins/holynotes/utils.ts @@ -5,7 +5,6 @@ */ import { createStore } from "@api/DataStore"; -import { Settings } from "@api/Settings"; export const HolyNoteStore = createStore("HolyNotesData", "HolyNotesStore");