Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: improve move-to sheet ux

ammarahm-ed27e3d1898715
diff --git a/apps/mobile/app/components/sheets/add-to/context.js b/apps/mobile/app/components/sheets/add-to/context.js
new file mode 100644
index 000000000..4307e2f76
--- /dev/null
+++ b/apps/mobile/app/components/sheets/add-to/context.js
@@ -0,0 +1,33 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { createContext, useContext } from "react";
+
+export const SelectionContext = createContext({
+ enabled: false,
+ selected: [],
+ toggleSelection: (item) => null,
+ deselect: (item) => null,
+ select: (item) => null,
+ isSelected: (item) => null,
+ setMultiSelect: () => null,
+ deselectAll: () => null
+});
+export const SelectionProvider = SelectionContext.Provider;
+export const useSelectionContext = () => useContext(SelectionContext);
diff --git a/apps/mobile/app/components/sheets/add-to/filtered-list.js b/apps/mobile/app/components/sheets/add-to/filtered-list.js
new file mode 100644
index 000000000..88f8c0b38
--- /dev/null
+++ b/apps/mobile/app/components/sheets/add-to/filtered-list.js
@@ -0,0 +1,75 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import React, { useCallback, useEffect, useRef, useState } from "react";
+import { FlatList } from "react-native-gesture-handler";
+import { db } from "../../../common/database";
+import { ListHeaderInputItem } from "./list-header-item.js";
+
+export const FilteredList = ({
+ data,
+ itemType,
+ onAddItem,
+ hasHeaderSearch,
+ ...restProps
+}) => {
+ const [filtered, setFiltered] = useState(data);
+ const query = useRef();
+ const onChangeText = useCallback(
+ (value) => {
+ query.current = value;
+ try {
+ if (!value) return setFiltered(data);
+ const results = db.lookup[itemType + "s"]([...data], value);
+ setFiltered(results);
+ } catch (e) {
+ console.warn(e.message);
+ }
+ },
+ [data, itemType]
+ );
+ const onSubmit = async (value) => {
+ return await onAddItem(value);
+ };
+
+ useEffect(() => {
+ onChangeText(query.current);
+ }, [data, onChangeText]);
+
+ return (
+ <FlatList
+ {...restProps}
+ data={filtered}
+ ListHeaderComponent={
+ hasHeaderSearch ? (
+ <ListHeaderInputItem
+ onSubmit={onSubmit}
+ onChangeText={onChangeText}
+ itemType={itemType}
+ testID={"list-input" + itemType}
+ placeholder={`Search or add a new ${itemType}`}
+ />
+ ) : null
+ }
+ keyboardShouldPersistTaps="always"
+ keyboardDismissMode="none"
+ nestedScrollEnabled
+ />
+ );
+};
diff --git a/apps/mobile/app/components/sheets/add-to/index.js b/apps/mobile/app/components/sheets/add-to/index.js
index 20f830cf1..903ed17a0 100644
--- a/apps/mobile/app/components/sheets/add-to/index.js
+++ b/apps/mobile/app/components/sheets/add-to/index.js
@@ -17,10 +17,15 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React, { createRef, useEffect, useState } from "react";
+import React, {
+ createRef,
+ useCallback,
+ useEffect,
+ useMemo,
+ useState
+} from "react";
import { Keyboard, TouchableOpacity, View } from "react-native";
-import { FlatList } from "react-native-gesture-handler";
-import { notesnook } from "../../../../e2e/test.ids";
+import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import { db } from "../../../common/database";
import {
eSubscribeEvent,
@@ -32,23 +37,17 @@ import SearchService from "../../../services/search";
import { useNotebookStore } from "../../../stores/use-notebook-store";
import { useSelectionStore } from "../../../stores/use-selection-store";
import { useThemeStore } from "../../../stores/use-theme-store";
-import { getTotalNotes } from "../../../utils";
import { eOpenMoveNoteDialog } from "../../../utils/events";
-import layoutmanager from "../../../utils/layout-manager";
-import { SIZE } from "../../../utils/size";
import { Dialog } from "../../dialog";
import DialogHeader from "../../dialog/dialog-header";
import { presentDialog } from "../../dialog/functions";
import { Button } from "../../ui/button";
-import { IconButton } from "../../ui/icon-button";
-import Input from "../../ui/input";
-import { PressableButton } from "../../ui/pressable";
import SheetWrapper from "../../ui/sheet";
-import Heading from "../../ui/typography/heading";
import Paragraph from "../../ui/typography/paragraph";
-import { useCallback } from "react";
-let newNotebookTitle = null;
-const notebookInput = createRef();
+import { SelectionProvider } from "./context";
+import { FilteredList } from "./filtered-list";
+import { ListItem } from "./list-item";
+
const actionSheetRef = createRef();
const AddToNotebookSheet = () => {
const [visible, setVisible] = useState(false);
@@ -69,7 +68,6 @@ const AddToNotebookSheet = () => {
const _onClose = () => {
setVisible(false);
- newNotebookTitle = null;
setNote(null);
Navigation.queueRoutesForUpdate(
"Notes",
@@ -93,160 +91,262 @@ export default AddToNotebookSheet;
const MoveNoteComponent = ({ note }) => {
const colors = useThemeStore((state) => state.colors);
-
+ const [multiSelect, setMultiSelect] = useState(false);
const notebooks = useNotebookStore((state) =>
state.notebooks.filter((n) => n?.type === "notebook")
);
-
+ const [edited, setEdited] = useState(false);
const selectedItemsList = useSelectionStore(
(state) => state.selectedItemsList
);
const setNotebooks = useNotebookStore((state) => state.setNotebooks);
- const [expanded, setExpanded] = useState("");
- const [notebookInputFocused, setNotebookInputFocused] = useState(false);
- const [noteExists, setNoteExists] = useState([]);
- const addNewNotebook = async () => {
- if (!newNotebookTitle || newNotebookTitle.trim().length === 0)
- return ToastEvent.show({
+ const [itemState, setItemState] = useState({});
+
+ const onAddNotebook = async (title) => {
+ if (!title || title.trim().length === 0) {
+ ToastEvent.show({
heading: "Notebook title is required",
type: "error",
context: "local"
});
+ return false;
+ }
- let id = await db.notebooks.add({
- title: newNotebookTitle,
+ await db.notebooks.add({
+ title: title,
description: null,
topics: [],
id: null
});
-
- setExpanded(id);
- openAddTopicDialog(db.notebooks.notebook(id).data);
- notebookInput.current?.clear();
- notebookInput.current?.blur();
setNotebooks();
- updateNoteExists();
+ return true;
};
- const addNewTopic = async (value, item) => {
- if (!value || value.trim().length === 0) {
- ToastEvent.show({
- heading: "Topic title is required",
- type: "error",
- context: "local"
- });
- return false;
- }
-
- await db.notebooks.notebook(item.id).topics.add(value);
- setNotebooks();
- updateNoteExists();
- return true;
+ const openAddTopicDialog = (item) => {
+ presentDialog({
+ context: "move_note",
+ input: true,
+ inputPlaceholder: "Enter title",
+ title: "New topic",
+ paragraph: "Add a new topic in " + item.title,
+ positiveText: "Add",
+ positivePress: (value) => {
+ return onAddTopic(value, item);
+ }
+ });
};
- const handlePress = async (item) => {
- let noteIds =
- selectedItemsList.length > 0
- ? selectedItemsList.map((n) => n.id)
- : [note?.id];
-
- if (getCount(item)) {
- await db.notes.removeFromNotebook(
- {
- id: item.notebookId,
- topic: item.id
- },
- ...noteIds
- );
- } else {
- await db.notes.addToNotebook(
- {
- topic: item.id,
- id: item.notebookId,
- rebuildCache: true
- },
- ...noteIds
- );
- }
+ const onAddTopic = useCallback(
+ async (value, item) => {
+ if (!value || value.trim().length === 0) {
+ ToastEvent.show({
+ heading: "Topic title is required",
+ type: "error",
+ context: "local"
+ });
+ return false;
+ }
- Navigation.queueRoutesForUpdate(
- "Notes",
- "Favorites",
- "ColoredNotes",
- "TaggedNotes",
- "TopicNotes"
- );
+ await db.notebooks.notebook(item.id).topics.add(value);
+ setNotebooks();
+ return true;
+ },
+ [setNotebooks]
+ );
- setNotebooks();
- updateNoteExists();
- SearchService.updateAndSearch();
- };
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ const getSelectedNotesCountInItem = React.useCallback(
+ (item) => {
+ switch (item.type) {
+ case "notebook": {
+ const noteIds = [];
+ for (let topic of item.topics) {
+ noteIds.push(...(db.notes?.topicReferences.get(topic.id) || []));
+ }
+ let count = 0;
+ selectedItemsList.forEach((item) =>
+ noteIds.indexOf(item.id) > -1 ? count++ : undefined
+ );
+ return count;
+ }
+ case "topic": {
+ const noteIds = db.notes?.topicReferences.get(item.id);
+ let count = 0;
+ selectedItemsList.forEach((item) =>
+ noteIds.indexOf(item.id) > -1 ? count++ : undefined
+ );
+ return count;
+ }
+ }
+ },
+ [selectedItemsList]
+ );
useEffect(() => {
- updateNoteExists();
- }, [updateNoteExists]);
-
- const updateNoteExists = useCallback(() => {
- if (!note?.id && selectedItemsList?.length === 0) return;
-
- let notes =
- selectedItemsList.length > 0
- ? selectedItemsList.map((n) => n.id)
- : [note?.id];
- let ids = [];
-
- let notebooks = db.notebooks.all;
- for (let i = 0; i < notebooks.length; i++) {
- if (notebooks[i].topics) {
- for (let t = 0; t < notebooks[i].topics.length; t++) {
- let topic = notebooks[i].topics[t];
- if (topic.type !== "topic") continue;
- for (let id of notes) {
- const noteIds = db.notes?.topicReferences.get(topic.id);
- if (noteIds.indexOf(id) > -1) {
- if (ids.indexOf(notebooks[i].id) === -1) {
- ids.push(notebooks[i].id);
- }
- if (ids.indexOf(topic.id) === -1) ids.push(topic.id);
+ resetItemState();
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
+ const resetItemState = useCallback(
+ (state) => {
+ setItemState(() => {
+ const itemState = {};
+ const notebooks = db.notebooks.all;
+ for (let notebook of notebooks) {
+ itemState[notebook.id] = state
+ ? state
+ : areAllSelectedItemsInAllTopics(notebook, selectedItemsList) &&
+ getSelectedNotesCountInItem(notebook, selectedItemsList) > 0
+ ? "selected"
+ : getSelectedNotesCountInItem(notebook, selectedItemsList) > 0
+ ? "intermediate"
+ : "deselected";
+ if (itemState[notebook.id] === "selected") {
+ contextValue.select(notebook);
+ }
+ for (let topic of notebook.topics) {
+ itemState[topic.id] = state
+ ? state
+ : areAllSelectedItemsInTopic(topic, selectedItemsList) &&
+ getSelectedNotesCountInItem(topic, selectedItemsList)
+ ? "selected"
+ : getSelectedNotesCountInItem(topic, selectedItemsList) > 0
+ ? "intermediate"
+ : "deselected";
+ if (itemState[topic.id] === "selected") {
+ contextValue.select(topic);
}
}
}
- }
+ return itemState;
+ });
+ },
+ [contextValue, getSelectedNotesCountInItem, selectedItemsList]
+ );
+
+ const getItemsForItem = (item) => {
+ switch (item.type) {
+ case "notebook":
+ return item.topics?.filter((t) => t.type === "topic");
}
+ };
- setNoteExists(ids);
- }, [note?.id, selectedItemsList]);
+ function areAllSelectedItemsInAllTopics(notebook, items) {
+ return items.every((item) => {
+ return notebook.topics.every((topic) => {
+ return db.notes.topicReferences.get(topic.id).indexOf(item.id) > -1;
+ });
+ });
+ }
- const openAddTopicDialog = (item) => {
- presentDialog({
- context: "move_note",
- input: true,
- inputPlaceholder: "Enter title",
- title: "New topic",
- paragraph: "Add a new topic in " + item.title,
- positiveText: "Add",
- positivePress: (value) => {
- return addNewTopic(value, item);
+ function areAllSelectedItemsInTopic(topic, items) {
+ return items.every((item) => {
+ return db.notes.topicReferences.get(topic.id).indexOf(item.id) > -1;
+ });
+ }
+
+ const updateItemState = useCallback(function (item, state) {
+ setItemState((itemState) => {
+ const mergeState = {
+ [item.id]: state
+ };
+ if (item.type === "topic") {
+ const notebooks = db.notebooks.all;
+ const notebook = notebooks.find((n) => n.id === item.notebookId);
+ const intermediate = notebook.topics.some((topic) => {
+ return topic.id === item.id
+ ? state === "selected"
+ : itemState[topic.id] === "selected";
+ });
+ if (intermediate) mergeState[notebook.id] = "intermediate";
+ const selected = notebook.topics.every((topic) => {
+ return topic.id === item.id
+ ? state === "selected"
+ : itemState[topic.id] === "selected";
+ });
+ if (selected) mergeState[notebook.id] = "selected";
+
+ if (!selected && !intermediate) mergeState[notebook.id] = "deselected";
}
+ return {
+ ...itemState,
+ ...mergeState
+ };
});
+ }, []);
+
+ const contextValue = useMemo(
+ () => ({
+ enabled: multiSelect,
+ toggleSelection: (item) => {
+ setItemState((itemState) => {
+ if (itemState[item.id] === "selected") {
+ updateItemState(item, "deselected");
+ } else {
+ updateItemState(item, "selected");
+ }
+
+ return itemState;
+ });
+ },
+ setMultiSelect: setMultiSelect,
+ deselect: (item) => {
+ updateItemState(item, "deselected");
+ },
+ select: (item) => {
+ updateItemState(item, "selected");
+ },
+ deselectAll: (state) => {
+ resetItemState(state);
+ }
+ }),
+ [multiSelect, resetItemState, updateItemState]
+ );
+
+ const getItemFromId = (id) => {
+ for (const nb of notebooks) {
+ if (nb.id === id) return nb;
+ for (const tp of nb.topics) {
+ if (tp.id === id) return tp;
+ }
+ }
};
- const getCount = (topic) => {
- if (!topic) return;
- let notes =
- selectedItemsList.length > 0
- ? selectedItemsList.map((n) => n.id)
- : [note?.id];
- let count = 0;
- for (let id of notes) {
- const noteIds = db.notes?.topicReferences.get(topic.id);
- if (noteIds.indexOf(id) > -1) {
- count++;
+ const onSave = async () => {
+ for (const id in itemState) {
+ const item = getItemFromId(id);
+ if (item.type === "notebook") continue;
+ const noteIds = selectedItemsList.map((n) => n.id);
+ if (itemState[id] === "selected") {
+ await db.notes.addToNotebook(
+ {
+ topic: item.id,
+ id: item.notebookId,
+ rebuildCache: true
+ },
+ ...noteIds
+ );
+ } else if (itemState[id] === "deselected") {
+ await db.notes.removeFromNotebook(
+ {
+ id: item.notebookId,
+ topic: item.id,
+ rebuildCache: true
+ },
+ ...noteIds
+ );
}
}
- return count > 0 && notes.length > 0
- ? `${count} of ${notes.length} selected notes exist in this topic. (Tap to remove)`
- : null;
+ Navigation.queueRoutesForUpdate(
+ "Notes",
+ "Favorites",
+ "ColoredNotes",
+ "TaggedNotes",
+ "TopicNotes"
+ );
+ setNotebooks();
+ SearchService.updateAndSearch();
+ actionSheetRef.current?.hide();
};
return (
@@ -266,215 +366,162 @@ const MoveNoteComponent = ({ note }) => {
<View
style={{
paddingHorizontal: 12,
+ justifyContent: "space-between",
flexDirection: "row",
- justifyContent: "space-between"
+ alignItems: "flex-start"
}}
>
<DialogHeader
- title="Add to notebook"
- paragraph={"Add your notes to notebooks to find them easily."}
+ style={{
+ minHeight: 10,
+ flexShrink: 1
+ }}
+ title="Select notebooks"
+ paragraph={
+ !multiSelect
+ ? "Long press to enable multi-select."
+ : "Select topics you want to add note(s) to."
+ }
+ />
+ <Button
+ height={35}
+ style={{
+ borderRadius: 100,
+ paddingHorizontal: 24,
+ alignSelf: "flex-start"
+ }}
+ title="Save"
+ type={"accent"}
+ onPress={onSave}
/>
</View>
- <FlatList
- nestedScrollEnabled={true}
- onMomentumScrollEnd={() => {
- actionSheetRef.current?.handleChildScrollEnd();
- }}
- keyboardShouldPersistTaps="always"
- keyboardDismissMode="none"
- data={notebooks}
- ListFooterComponent={
- <View
+ {multiSelect ? (
+ <View
+ style={{
+ paddingHorizontal: 12
+ }}
+ >
+ <Button
+ title="Reset selection"
+ height={30}
style={{
- height: 200
+ alignSelf: "flex-start",
+ paddingHorizontal: 0
}}
- />
- }
- ListHeaderComponent={
- <View
- style={{
- width: "100%",
- marginTop: 10
+ onPress={() => {
+ resetItemState();
+ setMultiSelect(false);
}}
- >
- <Input
- fwdRef={notebookInput}
- onChangeText={(value) => {
- newNotebookTitle = value;
- }}
- testID={notesnook.ids.dialogs.addTo.addNotebook}
- blurOnSubmit={false}
- onFocusInput={() => {
- setNotebookInputFocused(true);
- }}
- onBlurInput={() => {
- setNotebookInputFocused(false);
- }}
- button={{
- icon: "check",
- color: notebookInputFocused ? colors.accent : colors.icon,
- onPress: addNewNotebook
- }}
- onSubmit={addNewNotebook}
- placeholder="Create a new notebook"
- />
- </View>
- }
- style={{
- paddingHorizontal: 12
- }}
- renderItem={({ item }) => (
- <View
- style={{
- borderWidth: 1,
- borderColor: expanded ? colors.nav : "transparent",
- borderRadius: 6,
- overflow: "hidden",
- marginBottom: 10
- }}
- >
- <PressableButton
- onPress={() => {
- if (!item.topics || item.topics.length === 0) {
- setExpanded(item.id);
- openAddTopicDialog(item);
- return;
- }
- layoutmanager.withAnimation(200);
- setExpanded(item.id === expanded ? null : item.id);
- setNotebookInputFocused(false);
- }}
- type="grayBg"
- customStyle={{
- height: 50,
- width: "100%",
- borderRadius: 5,
- alignItems: "flex-start"
- }}
- >
+ />
+ </View>
+ ) : null}
+
+ <SelectionProvider value={contextValue}>
+ <FilteredList
+ onMomentumScrollEnd={() => {
+ actionSheetRef.current?.handleChildScrollEnd();
+ }}
+ style={{
+ paddingHorizontal: 12
+ }}
+ ListEmptyComponent={
+ notebooks.length > 0 ? null : (
<View
style={{
width: "100%",
- height: 50,
- justifyContent: "space-between",
- flexDirection: "row",
- alignItems: "center",
- paddingHorizontal: 12
+ height: "100%",
+ justifyContent: "center",
+ alignItems: "center"
}}
>
- <View>
- <Heading
- color={
- noteExists.indexOf(item.id) > -1 ? colors.accent : null
- }
- size={SIZE.md}
- >
- {item.title}
- </Heading>
- {item.topics?.length > 0 ? (
- <Paragraph size={SIZE.xs} color={colors.icon}>
- {getTotalNotes(item) + " notes" + " & "}
- {item.topics.length === 1
- ? item.topics.length + " topic"
- : item.topics.length + " topics"}
- </Paragraph>
- ) : null}
- </View>
-
- <IconButton
- name={expanded === item.id ? "plus" : "chevron-down"}
- color={expanded === item.id ? colors.accent : colors.pri}
- size={SIZE.xl}
- onPress={() => {
- if (expanded !== item.id) {
- setExpanded(item.id);
- return;
- }
- layoutmanager.withAnimation(200);
- setExpanded(item.id);
- openAddTopicDialog(item);
- }}
- />
+ <Icon name="book-outline" color={colors.icon} size={100} />
+ <Paragraph style={{ marginBottom: 10 }}>
+ You do not have any notebooks.
+ </Paragraph>
</View>
- </PressableButton>
-
- {expanded === item.id ? (
- <FlatList
- nestedScrollEnabled
- data={item.topics?.filter((t) => t.type === "topic")}
- keyboardShouldPersistTaps="always"
- keyboardDismissMode="none"
- onMomentumScrollEnd={() => {
- actionSheetRef.current?.handleChildScrollEnd();
- }}
- style={{
- width: "100%",
- alignSelf: "flex-end",
- maxHeight: 500
- }}
- renderItem={({ item, index }) => (
- <PressableButton
- onPress={() => handlePress(item, index)}
- type="gray"
- customStyle={{
- minHeight: 50,
- borderTopWidth: index === 0 ? 0 : 1,
- borderTopColor: index === 0 ? null : colors.nav,
- width: "100%",
- borderRadius: 0,
- alignItems: "center",
- flexDirection: "row",
- paddingHorizontal: 12,
- justifyContent: "space-between",
- paddingVertical: 12
- }}
- >
- <View>
- <Paragraph color={colors.heading}>
- {item.title}
- </Paragraph>
- <Paragraph color={colors.icon} size={SIZE.xs}>
- {getTotalNotes(item) + " notes"}
- </Paragraph>
- {getCount(item) ? (
- <View
- style={{
- backgroundColor: colors.nav,
- borderRadius: 5,
- paddingHorizontal: 5,
- paddingVertical: 2,
- marginTop: 5
- }}
- >
- <Paragraph color={colors.icon} size={SIZE.xs}>
- {getCount(item)}
- </Paragraph>
- </View>
- ) : null}
- </View>
- {noteExists.indexOf(item.id) > -1 ? (
- <Button
- onPress={() => handlePress(item, index)}
- icon="check"
- testID="icon-check"
- iconColor={colors.accent}
- iconSize={SIZE.lg}
- height={35}
- width={35}
- style={{
- borderRadius: 100,
- paddingHorizontal: 0
- }}
- />
- ) : null}
- </PressableButton>
- )}
- />
- ) : null}
- </View>
- )}
- />
+ )
+ }
+ data={notebooks}
+ hasHeaderSearch={true}
+ renderItem={({ item, index }) => (
+ <ListItem
+ item={item}
+ key={item.id}
+ index={index}
+ intermediate={itemState[item.id] === "intermediate"}
+ removed={
+ itemState[item.id] === "deselected" &&
+ getSelectedNotesCountInItem(item) > 0
+ }
+ isSelected={itemState[item.id] === "selected"}
+ infoText={
+ <>
+ {item.topics.length === 1
+ ? item.topics.length + " topic"
+ : item.topics.length + " topics"}
+ </>
+ }
+ getListItems={getItemsForItem}
+ getSublistItemProps={(topic) => ({
+ selected: itemState[topic.id] === "selected",
+ intermediate: itemState[topic.id] === "intermediate",
+ isSelected: itemState[topic.id] === "selected",
+ removed:
+ itemState[topic.id] === "deselected" &&
+ getSelectedNotesCountInItem(topic) > 0,
+ style: {
+ marginBottom: 0,
+ height: 40
+ },
+ onPress: (item) => {
+ const currentState = itemState[item.id];
+ if (currentState !== "selected") {
+ resetItemState("deselected");
+ contextValue.select(item);
+ updateItemState(
+ notebooks.find((n) => n.id === item.notebookId),
+ "intermediate"
+ );
+ } else {
+ contextValue.deselect(item);
+ }
+ },
+ key: item.id,
+ type: "transparent"
+ })}
+ icon={(expanded) => ({
+ name: expanded ? "chevron-up" : "chevron-down",
+ color: expanded ? colors.accent : colors.pri
+ })}
+ onScrollEnd={() => {
+ actionSheetRef.current?.handleChildScrollEnd();
+ }}
+ hasSubList={true}
+ hasHeaderSearch={false}
+ type="grayBg"
+ sublistItemType="topic"
+ onAddItem={(title) => {
+ return onAddTopic(title, item);
+ }}
+ onAddSublistItem={(item) => {
+ openAddTopicDialog(item);
+ }}
+ />
+ )}
+ itemType="notebook"
+ onAddItem={async (title) => {
+ return await onAddNotebook(title);
+ }}
+ ListFooterComponent={
+ <View
+ style={{
+ height: 200
+ }}
+ />
+ }
+ />
+ </SelectionProvider>
</View>
</>
);
diff --git a/apps/mobile/app/components/sheets/add-to/list-header-item.js b/apps/mobile/app/components/sheets/add-to/list-header-item.js
new file mode 100644
index 000000000..71ce0c37d
--- /dev/null
+++ b/apps/mobile/app/components/sheets/add-to/list-header-item.js
@@ -0,0 +1,87 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import React, { useRef, useState } from "react";
+import { View } from "react-native";
+import { useThemeStore } from "../../../stores/use-theme-store";
+import Input from "../../ui/input";
+import Paragraph from "../../ui/typography/paragraph";
+
+export const ListHeaderInputItem = ({
+ onSubmit,
+ onChangeText,
+ placeholder,
+ testID
+}) => {
+ const [focused, setFocused] = useState(false);
+ const colors = useThemeStore((state) => state.colors);
+ const [inputValue, setInputValue] = useState();
+ const inputRef = useRef();
+ return (
+ <View
+ style={{
+ width: "100%",
+ marginTop: 10,
+ marginBottom: 5
+ }}
+ >
+ <Input
+ fwdRef={inputRef}
+ onChangeText={(value) => {
+ setInputValue(value);
+ onChangeText?.(value);
+ }}
+ testID={testID}
+ blurOnSubmit={false}
+ onFocusInput={() => {
+ setFocused(true);
+ }}
+ onBlurInput={() => {
+ setFocused(false);
+ }}
+ button={{
+ icon: inputValue ? "plus" : "magnify",
+ color: focused ? colors.accent : colors.icon,
+ onPress: async () => {
+ const result = await onSubmit(inputValue);
+ if (result) {
+ inputRef.current?.blur();
+ }
+ }
+ }}
+ placeholder={placeholder}
+ marginBottom={5}
+ />
+ {inputValue ? (
+ <View
+ style={{
+ backgroundColor: colors.shade,
+ padding: 5,
+ borderRadius: 5,
+ marginBottom: 10
+ }}
+ >
+ <Paragraph color={colors.accent}>
+ Tap on + to add {`"${inputValue}"`}
+ </Paragraph>
+ </View>
+ ) : null}
+ </View>
+ );
+};
diff --git a/apps/mobile/app/components/sheets/add-to/list-item.js b/apps/mobile/app/components/sheets/add-to/list-item.js
new file mode 100644
index 000000000..5ff1ff62e
--- /dev/null
+++ b/apps/mobile/app/components/sheets/add-to/list-item.js
@@ -0,0 +1,224 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import React, { useState, useEffect } from "react";
+import { View } from "react-native";
+import { useThemeStore } from "../../../stores/use-theme-store";
+import { SIZE } from "../../../utils/size";
+import { IconButton } from "../../ui/icon-button";
+import { PressableButton } from "../../ui/pressable";
+import Heading from "../../ui/typography/heading";
+import Paragraph from "../../ui/typography/paragraph";
+import { useSelectionContext } from "./context";
+import { FilteredList } from "./filtered-list";
+
+const _ListItem = ({
+ item,
+ index,
+ icon,
+ infoText,
+ intermediate,
+ hasSubList,
+ onPress,
+ onScrollEnd,
+ getListItems,
+ style,
+ type,
+ sublistItemType,
+ onAddItem,
+ getSublistItemProps,
+ removed,
+ isSelected,
+ hasHeaderSearch,
+ onAddSublistItem
+}) => {
+ const { enabled, toggleSelection, setMultiSelect, select, deselect } =
+ useSelectionContext();
+ const colors = useThemeStore((state) => state.colors);
+ const [expanded, setExpanded] = useState(false);
+
+ function selectItem() {
+ const currentState = isSelected;
+ toggleSelection(item);
+ if (item.type === "notebook") {
+ item.topics.forEach((item) => {
+ if (currentState) {
+ deselect(item);
+ } else {
+ select(item);
+ }
+ });
+ }
+ }
+
+ return (
+ <View
+ style={{
+ overflow: "hidden",
+ marginBottom: 10,
+ ...style
+ }}
+ >
+ <PressableButton
+ onPress={() => {
+ if (hasSubList) return setExpanded(!expanded);
+ if (enabled) return selectItem();
+ onPress(item);
+ }}
+ type={type}
+ onLongPress={() => {
+ setMultiSelect(true);
+ selectItem();
+ }}
+ customStyle={{
+ height: style?.height || 50,
+ width: "100%",
+ alignItems: "flex-start"
+ }}
+ >
+ <View
+ style={{
+ width: "100%",
+ height: 50,
+ justifyContent: "space-between",
+ flexDirection: "row",
+ alignItems: "center",
+ paddingHorizontal: 12
+ }}
+ >
+ <View
+ style={{
+ flexDirection: "row",
+ alignItems: "center"
+ }}
+ >
+ <IconButton
+ size={22}
+ customStyle={{
+ marginRight: 5,
+ width: 23,
+ height: 23
+ }}
+ color={
+ removed
+ ? colors.red
+ : intermediate || isSelected
+ ? colors.accent
+ : colors.icon
+ }
+ onPress={() => {
+ if (item.type === "notebook") {
+ setMultiSelect(true);
+ }
+ selectItem();
+ if (enabled) return;
+ onPress(item);
+ }}
+ name={
+ removed
+ ? "close-circle-outline"
+ : isSelected
+ ? "check-circle-outline"
+ : intermediate
+ ? "minus-circle-outline"
+ : "checkbox-blank-circle-outline"
+ }
+ />
+ <View>
+ {hasSubList && expanded ? (
+ <Heading size={SIZE.md}>{item.title}</Heading>
+ ) : (
+ <Paragraph size={SIZE.sm}>{item.title}</Paragraph>
+ )}
+
+ {infoText ? (
+ <Paragraph size={SIZE.xs} color={colors.icon}>
+ {infoText}
+ </Paragraph>
+ ) : null}
+ </View>
+ </View>
+
+ <View
+ style={{
+ flexDirection: "row"
+ }}
+ >
+ {onAddSublistItem ? (
+ <IconButton
+ name={"plus"}
+ color={colors}
+ size={SIZE.xl}
+ onPress={() => {
+ onAddSublistItem(item);
+ }}
+ />
+ ) : null}
+ {icon ? (
+ <IconButton
+ name={icon(expanded).name}
+ color={icon(expanded).color}
+ size={icon(expanded).size || SIZE.xl}
+ onPress={
+ hasSubList
+ ? () => setExpanded(!expanded)
+ : icon(expanded).onPress
+ }
+ />
+ ) : null}
+ </View>
+ </View>
+ </PressableButton>
+
+ {expanded && hasSubList ? (
+ <FilteredList
+ nestedScrollEnabled
+ data={getListItems(item)}
+ keyboardShouldPersistTaps="always"
+ keyboardDismissMode="none"
+ onMomentumScrollEnd={onScrollEnd}
+ style={{
+ width: "95%",
+ alignSelf: "flex-end",
+ maxHeight: 500
+ }}
+ itemType={sublistItemType}
+ hasHeaderSearch={hasHeaderSearch}
+ renderItem={({ item, index }) => (
+ <ListItem
+ item={item}
+ {...getSublistItemProps(item)}
+ index={index}
+ onScrollEnd={onScrollEnd}
+ />
+ )}
+ onAddItem={onAddItem}
+ />
+ ) : null}
+ </View>
+ );
+};
+
+export const ListItem = React.memo(_ListItem, (prev, next) => {
+ if (prev.selected === undefined) return false;
+ if (prev.isSelected !== next.isSelected) return false;
+ if (prev.selected !== next.selected) return false;
+ if (prev.intermediate !== next.intermediate) return false;
+ return true;
+});