From 14321330e5584ac4ddb39bc82e645f7c2cb5faec Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Wed, 19 Feb 2025 12:18:00 +0500 Subject: [PATCH] mobile: refactor --- .../components/sheets/move-notebook/index.tsx | 7 +- .../components/side-menu/notebook-item.tsx | 196 ++++++++++++++++++ .../side-menu/side-menu-notebooks.tsx | 174 +--------------- 3 files changed, 201 insertions(+), 176 deletions(-) create mode 100644 apps/mobile/app/components/side-menu/notebook-item.tsx diff --git a/apps/mobile/app/components/sheets/move-notebook/index.tsx b/apps/mobile/app/components/sheets/move-notebook/index.tsx index b0d346f2b..01e927d8f 100644 --- a/apps/mobile/app/components/sheets/move-notebook/index.tsx +++ b/apps/mobile/app/components/sheets/move-notebook/index.tsx @@ -33,7 +33,6 @@ import { import { checkParentSelected, findRootNotebookId, - findSelectedParent, getParentNotebookId } from "../../../utils/notebooks"; import { AppFontSize } from "../../../utils/size"; @@ -84,7 +83,7 @@ export const MoveNotebookSheet = ({ useEffect(() => { (async () => { for (const notebook of selectedNotebooks) { - const root = await findSelectedParent(notebook.id); + const root = await findRootNotebookId(notebook.id); if (root !== notebook.id) { setMoveToTop(true); return; @@ -203,7 +202,7 @@ export const MoveNotebookSheet = ({ ) continue; const parent = await getParentNotebookId(notebook.id); - const root = await findSelectedParent(notebook.id); + const root = await findRootNotebookId(notebook.id); if (root !== notebook.id) { await db.relations.unlink( { @@ -271,7 +270,7 @@ const NotebookItem = ({ return selectedNotebooks.find((n) => n.id === notebook?.id) ? null : ( . +*/ + +import { useThemeColors } from "@notesnook/theme"; +import React, { useEffect } from "react"; +import { View } from "react-native"; +import { UseBoundStore } from "zustand"; +import { useTotalNotes } from "../../hooks/use-db-item"; +import { + eSubscribeEvent, + eUnSubscribeEvent +} from "../../services/event-manager"; +import { TreeItem } from "../../stores/create-notebook-tree-stores"; +import { SelectionStore } from "../../stores/item-selection-store"; +import { eOnNotebookUpdated } from "../../utils/events"; +import { AppFontSize, defaultBorderRadius } from "../../utils/size"; +import { DefaultAppStyles } from "../../utils/styles"; +import AppIcon from "../ui/AppIcon"; +import { IconButton } from "../ui/icon-button"; +import { Pressable } from "../ui/pressable"; +import Paragraph from "../ui/typography/paragraph"; + +export const NotebookItem = ({ + index, + item, + expanded, + selected, + onToggleExpanded, + focused, + selectionEnabled, + selectionStore, + onItemUpdate, + onPress, + onLongPress +}: { + index: number; + item: TreeItem; + expanded?: boolean; + onToggleExpanded?: () => void; + selected?: boolean; + focused?: boolean; + selectionEnabled?: boolean; + selectionStore: UseBoundStore; + onItemUpdate: (id?: string) => void; + onPress?: () => void; + onLongPress?: () => void; +}) => { + const notebook = item.notebook; + const isFocused = focused; + const { totalNotes, getTotalNotes } = useTotalNotes("notebook"); + const getTotalNotesRef = React.useRef(getTotalNotes); + getTotalNotesRef.current = getTotalNotes; + const { colors } = useThemeColors("sheet"); + + useEffect(() => { + getTotalNotesRef.current([item.notebook.id]); + }, [item.notebook]); + + useEffect(() => { + const onNotebookUpdate = (id?: string) => { + if (id && id !== notebook.id) return; + onItemUpdate(id); + }; + + eSubscribeEvent(eOnNotebookUpdated, onNotebookUpdate); + return () => { + eUnSubscribeEvent(eOnNotebookUpdated, onNotebookUpdate); + }; + }, [notebook.id, onItemUpdate]); + + return ( + 0 && item.depth < 6 ? 15 * item.depth : undefined, + width: "100%", + marginTop: 2 + }} + > + { + if (selectionEnabled) { + selectionStore + .getState() + .markAs(item.notebook, selected ? "deselected" : "selected"); + if (selectionStore.getState().getSelectedItemIds().length === 0) { + selectionStore.setState({ + enabled: false + }); + } + } else { + onPress?.(); + } + }} + style={{ + justifyContent: "space-between", + width: "100%", + alignItems: "center", + flexDirection: "row", + borderRadius: defaultBorderRadius, + paddingRight: DefaultAppStyles.GAP_SMALL + }} + > + + { + if (item.hasChildren) { + onToggleExpanded?.(); + } else { + onPress?.(); + } + }} + top={0} + left={50} + bottom={0} + right={40} + style={{ + width: 32, + height: 32, + borderRadius: defaultBorderRadius + }} + name={ + !item.hasChildren + ? "book-outline" + : expanded + ? "chevron-down" + : "chevron-right" + } + /> + + + {notebook?.title} + + + + {selectionEnabled ? ( + + + + ) : ( + <> + + {totalNotes?.(notebook?.id) || 0} + + + )} + + + ); +}; diff --git a/apps/mobile/app/components/side-menu/side-menu-notebooks.tsx b/apps/mobile/app/components/side-menu/side-menu-notebooks.tsx index 1fe4d23dc..1d34924d2 100644 --- a/apps/mobile/app/components/side-menu/side-menu-notebooks.tsx +++ b/apps/mobile/app/components/side-menu/side-menu-notebooks.tsx @@ -22,27 +22,16 @@ import { strings } from "@notesnook/intl"; import { useThemeColors } from "@notesnook/theme"; import React, { useEffect } from "react"; import { FlatList, TextInput, View } from "react-native"; -import { UseBoundStore } from "zustand"; import { db } from "../../common/database"; -import { useTotalNotes } from "../../hooks/use-db-item"; import NotebookScreen from "../../screens/notebook"; -import { - eSubscribeEvent, - eUnSubscribeEvent -} from "../../services/event-manager"; import Navigation from "../../services/navigation"; import { TreeItem } from "../../stores/create-notebook-tree-stores"; -import { SelectionStore } from "../../stores/item-selection-store"; import useNavigationStore from "../../stores/use-navigation-store"; import { useNotebooks } from "../../stores/use-notebook-store"; -import { eOnNotebookUpdated } from "../../utils/events"; -import { AppFontSize, defaultBorderRadius } from "../../utils/size"; +import { AppFontSize } from "../../utils/size"; import { DefaultAppStyles } from "../../utils/styles"; import { Properties } from "../properties"; -import AppIcon from "../ui/AppIcon"; -import { IconButton } from "../ui/icon-button"; -import { Pressable } from "../ui/pressable"; -import Paragraph from "../ui/typography/paragraph"; +import { NotebookItem } from "./notebook-item"; import { SideMenuHeader } from "./side-menu-header"; import { SideMenuListEmpty } from "./side-menu-list-empty"; import { @@ -51,165 +40,6 @@ import { useSideMenuNotebookTreeStore } from "./stores"; -const NotebookItem = ({ - index, - item, - expanded, - selected, - onToggleExpanded, - focused, - selectionEnabled, - selectionStore, - onItemUpdate, - onPress, - onLongPress -}: { - index: number; - item: TreeItem; - expanded?: boolean; - onToggleExpanded?: () => void; - selected?: boolean; - focused?: boolean; - selectionEnabled?: boolean; - selectionStore: UseBoundStore; - onItemUpdate: (id?: string) => void; - onPress?: () => void; - onLongPress?: () => void; -}) => { - const notebook = item.notebook; - const isFocused = focused; - const { totalNotes, getTotalNotes } = useTotalNotes("notebook"); - const getTotalNotesRef = React.useRef(getTotalNotes); - getTotalNotesRef.current = getTotalNotes; - const { colors } = useThemeColors("sheet"); - - useEffect(() => { - getTotalNotesRef.current([item.notebook.id]); - }, [item.notebook]); - - useEffect(() => { - const onNotebookUpdate = (id?: string) => { - if (id && id !== notebook.id) return; - onItemUpdate(id); - }; - - eSubscribeEvent(eOnNotebookUpdated, onNotebookUpdate); - return () => { - eUnSubscribeEvent(eOnNotebookUpdated, onNotebookUpdate); - }; - }, [notebook.id, onItemUpdate]); - - return ( - 0 && item.depth < 6 ? 15 * item.depth : undefined, - width: "100%", - marginTop: 2 - }} - > - { - if (selectionEnabled) { - selectionStore - .getState() - .markAs(item.notebook, selected ? "deselected" : "selected"); - if (selectionStore.getState().getSelectedItemIds().length === 0) { - selectionStore.setState({ - enabled: false - }); - } - } else { - onPress?.(); - } - }} - style={{ - justifyContent: "space-between", - width: "100%", - alignItems: "center", - flexDirection: "row", - borderRadius: defaultBorderRadius, - paddingRight: DefaultAppStyles.GAP_SMALL - }} - > - - { - if (item.hasChildren) { - onToggleExpanded?.(); - } else { - onPress?.(); - } - }} - top={0} - left={50} - bottom={0} - right={40} - style={{ - width: 32, - height: 32, - borderRadius: defaultBorderRadius - }} - name={ - !item.hasChildren - ? "book-outline" - : expanded - ? "chevron-down" - : "chevron-right" - } - /> - - - {notebook?.title} - - - - {selectionEnabled ? ( - - - - ) : ( - <> - - {totalNotes?.(notebook?.id) || 0} - - - )} - - - ); -}; - export const SideMenuNotebooks = () => { const tree = useSideMenuNotebookTreeStore((state) => state.tree); const [notebooks, loading] = useNotebooks();