diff --git a/apps/mobile/app/components/attachments/index.js b/apps/mobile/app/components/attachments/index.js index ce3e9cce5..877e1c7f0 100644 --- a/apps/mobile/app/components/attachments/index.js +++ b/apps/mobile/app/components/attachments/index.js @@ -17,70 +17,34 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -import React, { useEffect, useRef, useState } from "react"; +import React, { useRef, useState } from "react"; import { View } from "react-native"; import { FlatList } from "react-native-gesture-handler"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; import { db } from "../../common/database"; import filesystem from "../../common/filesystem"; -import { - eSubscribeEvent, - eUnSubscribeEvent -} from "../../services/event-manager"; +import { presentSheet } from "../../services/event-manager"; import { useThemeStore } from "../../stores/use-theme-store"; -import { - eCloseAttachmentDialog, - eOpenAttachmentsDialog -} from "../../utils/events"; import { SIZE } from "../../utils/size"; import DialogHeader from "../dialog/dialog-header"; -import { Toast } from "../toast"; import Input from "../ui/input"; import Seperator from "../ui/seperator"; -import SheetWrapper from "../ui/sheet"; import Paragraph from "../ui/typography/paragraph"; import { AttachmentItem } from "./attachment-item"; -export const AttachmentDialog = () => { + +export const AttachmentDialog = ({ data }) => { const colors = useThemeStore((state) => state.colors); - const [visible, setVisible] = useState(false); - const [note, setNote] = useState(null); + const [note, setNote] = useState(data); const actionSheetRef = useRef(); - const [attachments, setAttachments] = useState([]); + const [attachments, setAttachments] = useState( + data + ? db.attachments.ofNote(data.id, "all") + : [...(db.attachments.all || [])] + ); const attachmentSearchValue = useRef(); const searchTimer = useRef(); const [loading, setLoading] = useState(false); - useEffect(() => { - eSubscribeEvent(eOpenAttachmentsDialog, open); - eSubscribeEvent(eCloseAttachmentDialog, close); - return () => { - eUnSubscribeEvent(eOpenAttachmentsDialog, open); - eUnSubscribeEvent(eCloseAttachmentDialog, close); - }; - }, [visible]); - - const open = (data) => { - if (data?.id) { - setNote(data); - let _attachments = db.attachments.ofNote(data.id, "all"); - setAttachments(_attachments); - } else { - setAttachments([...db.attachments.all]); - } - setVisible(true); - }; - - useEffect(() => { - if (visible) { - actionSheetRef.current?.show(); - } - }, [visible]); - - const close = () => { - actionSheetRef.current?.hide(); - setVisible(false); - }; - const onChangeText = (text) => { attachmentSearchValue.current = text; if ( @@ -104,107 +68,104 @@ export const AttachmentDialog = () => { ); - return !visible ? null : ( - { - setVisible(false); + return ( + - - - { - setLoading(true); - for (let attachment of attachments) { - let result = await filesystem.checkAttachment( - attachment.metadata.hash + { + setLoading(true); + for (let attachment of attachments) { + let result = await filesystem.checkAttachment( + attachment.metadata.hash + ); + if (result.failed) { + db.attachments.markAsFailed( + attachment.metadata.hash, + result.failed ); - if (result.failed) { - db.attachments.markAsFailed( - attachment.metadata.hash, - result.failed - ); - } else { - db.attachments.markAsFailed(attachment.id, null); - } - setAttachments([...db.attachments.all]); + } else { + db.attachments.markAsFailed(attachment.id, null); } - setLoading(false); + setAttachments([...db.attachments.all]); } + setLoading(false); + } + }} + /> + + {!note ? ( + { + onChangeText(attachmentSearchValue.current); }} /> - - {!note ? ( - { - onChangeText(attachmentSearchValue.current); + ) : null} + + { + actionSheetRef.current?.handleChildScrollEnd(); + }} + ListEmptyComponent={ + + + + {note ? "No attachments on this note" : "No attachments"} + + + } + ListFooterComponent={ + - ) : null} + } + data={attachments} + keyExtractor={(item) => item.id} + renderItem={renderItem} + /> - { - actionSheetRef.current?.handleChildScrollEnd(); - }} - ListEmptyComponent={ - - - - {note ? "No attachments on this note" : "No attachments"} - - - } - ListFooterComponent={ - - } - data={attachments} - keyExtractor={(item) => item.id} - renderItem={renderItem} - /> - - - - {" "}All attachments are end-to-end encrypted. - - - + + + {" "}All attachments are end-to-end encrypted. + + ); }; + +AttachmentDialog.present = (note) => { + presentSheet({ + component: () => + }); +}; diff --git a/apps/mobile/app/components/dialog-provider/index.js b/apps/mobile/app/components/dialog-provider/index.js index 1baea8141..97dc049b6 100644 --- a/apps/mobile/app/components/dialog-provider/index.js +++ b/apps/mobile/app/components/dialog-provider/index.js @@ -21,7 +21,6 @@ import React from "react"; import { useNoteStore } from "../../stores/use-notes-store"; import { useThemeStore } from "../../stores/use-theme-store"; import { AnnouncementDialog } from "../announcements"; -import { AttachmentDialog } from "../attachments"; import AuthModal from "../auth/auth-modal"; import { SessionExpired } from "../auth/session-expired"; import { Dialog } from "../dialog"; @@ -34,10 +33,6 @@ import MergeConflicts from "../merge-conflicts"; import PremiumDialog from "../premium"; import { Expiring } from "../premium/expiring"; import SheetProvider from "../sheet-provider"; -import { AddNotebookSheet } from "../sheets/add-notebook"; -import AddToNotebookSheet from "../sheets/add-to"; -import ManageTagsSheet from "../sheets/manage-tags"; -import PublishNoteSheet from "../sheets/publish-note"; import RateAppSheet from "../sheets/rate-app"; import RecoveryKeySheet from "../sheets/recovery-key"; import RestoreDataSheet from "../sheets/restore-data"; @@ -51,7 +46,6 @@ const DialogProvider = () => { - @@ -61,12 +55,8 @@ const DialogProvider = () => { - - - - {loading ? null : } diff --git a/apps/mobile/app/components/dialogs/add-topic/index.js b/apps/mobile/app/components/dialogs/add-topic/index.js index 0986f9d74..777e21d0e 100644 --- a/apps/mobile/app/components/dialogs/add-topic/index.js +++ b/apps/mobile/app/components/dialogs/add-topic/index.js @@ -59,14 +59,12 @@ export class AddTopicDialog extends React.Component { addNewTopic = async () => { try { - this.setState({ loading: true }); if (!this.title || this.title?.trim() === "") { ToastEvent.show({ heading: "Topic title is required", type: "error", context: "local" }); - this.setState({ loading: false }); return; } @@ -78,10 +76,11 @@ export class AddTopicDialog extends React.Component { await db.notebooks.notebook(topic.notebookId).topics.add(topic); } - this.setState({ loading: false }); this.close(); - Navigation.queueRoutesForUpdate("Notebooks", "Notebook", "TopicNotes"); - useMenuStore.getState().setMenuPins(); + setTimeout(() => { + Navigation.queueRoutesForUpdate("Notebooks", "Notebook", "TopicNotes"); + useMenuStore.getState().setMenuPins(); + }); } catch (e) { console.error(e); } @@ -177,7 +176,6 @@ export class AddTopicDialog extends React.Component { positiveTitle={this.toEdit ? "Save" : "Add"} onPressNegative={() => this.close()} onPressPositive={() => this.addNewTopic()} - loading={this.state.loading} /> diff --git a/apps/mobile/app/components/header/title.js b/apps/mobile/app/components/header/title.js index df0898677..f1c6f235b 100644 --- a/apps/mobile/app/components/header/title.js +++ b/apps/mobile/app/components/header/title.js @@ -30,7 +30,6 @@ import { useThemeStore } from "../../stores/use-theme-store"; import { eScrollEvent } from "../../utils/events"; import { SIZE } from "../../utils/size"; import Heading from "../ui/typography/heading"; -import Paragraph from "../ui/typography/paragraph"; import { useCallback } from "react"; import Tag from "../ui/tag"; @@ -120,12 +119,6 @@ export const Title = () => { }} color={currentScreen.color || colors.heading} > - {isTopic ? ( - - {notebook?.title} - {"\n"} - - ) : null} {isTag ? ( { - TopicNotes.navigate(topic, true); -}; - function navigateToTag(item) { const tag = db.tags.tag(item.id); if (!tag) return; @@ -55,24 +52,29 @@ const showActionSheet = (item) => { function getNotebook(item) { const isTrash = item.type === "trash"; - if (isTrash || !item.notebooks || item.notebooks.length < 1) return []; + if (isTrash) return []; + const items = []; + const notebooks = db.relations.to(item, "notebook") || []; - return item.notebooks.reduce(function (prev, curr) { - if (prev && prev.length > 0) return prev; - const topicId = curr.topics[0]; - const notebook = db.notebooks?.notebook(curr.id)?.data; - if (!notebook) return []; - const topic = notebook.topics.find((t) => t.id === topicId); - if (!topic) return []; + for (let notebook of notebooks) { + if (items.length > 1) break; + items.push(notebook); + } - return [ - { - title: `${notebook?.title} › ${topic?.title}`, - notebook: notebook, - topic: topic + if (item.notebooks) { + for (let nb of item.notebooks) { + if (items.length > 1) break; + const notebook = db.notebooks?.notebook(nb.id)?.data; + if (!notebook) continue; + for (let topicId of nb.topics) { + if (items.length > 1) break; + const topic = notebook.topics.find((t) => t.id === topicId); + if (!topic) continue; + items.push(topic); } - ]; - }, []); + } + } + return items; } const NoteItem = ({ @@ -88,10 +90,11 @@ const NoteItem = ({ ); const compactMode = notesListMode === "compact"; const attachmentCount = db.attachments?.ofNote(item.id, "all")?.length || 0; - const notebooks = React.useMemo(() => getNotebook(item), [item]); + const _update = useRelationStore((state) => state.updater); + // eslint-disable-next-line react-hooks/exhaustive-deps + const notebooks = React.useMemo(() => getNotebook(item), [item, _update]); const reminders = db.relations.from(item, "reminder"); const reminder = getUpcomingReminder(reminders); - const _update = useRelationStore((state) => state.updater); const noteColor = COLORS_NOTE[item.color?.toLowerCase()]; return ( <> @@ -112,12 +115,12 @@ const NoteItem = ({ flexWrap: "wrap" }} > - {notebooks?.map((_item) => ( + {notebooks?.map((item) => (