A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
refactor: move selection logic to its own store
thecodrr5fe29fcc0098
diff --git a/apps/web/src/common/selectionoptions.js b/apps/web/src/common/selectionoptions.jsindex 8a42bc5d6..30204401e 100644--- a/apps/web/src/common/selectionoptions.js+++ b/apps/web/src/common/selectionoptions.js@@ -1,5 +1,5 @@import * as Icon from "../components/icons";-import { store as appStore } from "../stores/app-store";+import { store as selectionStore } from "../stores/app-store";import { store as notesStore } from "../stores/note-store";import { store as nbStore } from "../stores/notebook-store";import { store as editorStore } from "../stores/editor-store";@@ -12,8 +12,8 @@ function createOption(icon, onClick) {return {icon,onClick: async () => {- await onClick.call(this, appStore.getState());- appStore.getState().exitSelectionMode();+ await onClick.call(this, selectionStore.getState());+ selectionStore.getState().toggleSelectionMode(false);}};}diff --git a/apps/web/src/components/list-container/index.js b/apps/web/src/components/list-container/index.jsindex 00eb3f711..24a1a039f 100644--- a/apps/web/src/components/list-container/index.js+++ b/apps/web/src/components/list-container/index.js@@ -5,16 +5,16 @@ import Search from "../search";import * as Icon from "../icons";import { Virtuoso as List } from "react-virtuoso";import { useStore as useSearchStore } from "../../stores/searchstore";-import { useStore as useAppStore } from "../../stores/app-store";+import { useStore as useSelectionStore } from "../../stores/selection-store";function ListContainer(props) {const setSearchContext = useSearchStore(store => store.setSearchContext);- const shouldSelectAll = useAppStore(store => store.shouldSelectAll);- const setSelectedItems = useAppStore(store => store.setSelectedItems);+ const shouldSelectAll = useSelectionStore(store => store.shouldSelectAll);+ const setSelectedItems = useSelectionStore(store => store.setSelectedItems);+useEffect(() => {if (shouldSelectAll) setSelectedItems(props.items);}, [shouldSelectAll, setSelectedItems, props.items]);-useEffect(() => {if (props.noSearch) return;setSearchContext({@@ -23,6 +23,7 @@ function ListContainer(props) {type: props.type});}, [setSearchContext, props.item, props.items, props.type, props.noSearch]);+return (<Flex variant="columnFill">{!props.items.length && props.placeholder ? (diff --git a/apps/web/src/components/list-item/index.js b/apps/web/src/components/list-item/index.jsindex f6f7e1f28..66ca47ad1 100644--- a/apps/web/src/components/list-item/index.js+++ b/apps/web/src/components/list-item/index.js@@ -5,8 +5,8 @@ import Dropdown, { DropdownTrigger, DropdownContent } from "../dropdown";import Menu from "../menu";import {store as appStore,- useStore as useAppStore-} from "../../stores/app-store";+ useStore as useSelectionStore+} from "../../stores/selection-store";import useContextMenu from "../../utils/useContextMenu";function ActionsMenu(props) {@@ -57,11 +57,11 @@ function ListItem(props) {const [parentRef, closeContextMenu] = useContextMenu(`contextMenu${props.index}`);- const isSelectionMode = useAppStore(store => store.isSelectionMode);- const selectedItems = useAppStore(store => store.selectedItems);+ const isSelectionMode = useSelectionStore(store => store.isSelectionMode);+ const selectedItems = useSelectionStore(store => store.selectedItems);const isSelected =selectedItems.findIndex(item => props.item.id === item.id) > -1;- const selectItem = useAppStore(store => store.selectItem);+ const selectItem = useSelectionStore(store => store.selectItem);const [menuItems, setMenuItems] = useState(props.menuItems);const toggleSelection = useCallback(diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.jsindex 0ec285ea8..67560dff5 100644--- a/apps/web/src/navigation/index.js+++ b/apps/web/src/navigation/index.js@@ -5,7 +5,8 @@ import Animated from "../components/animated";import { AnimatePresence } from "framer-motion";import * as Icon from "../components/icons";import ThemeProvider from "../components/theme-provider";-import { useStore, store } from "../stores/app-store";+import { useStore } from "../stores/app-store";+import { store as selectionStore } from "../stores/selection-store";class Navigator {constructor(root, routes, options = {}) {@@ -47,7 +48,7 @@ class Navigator {return false;}// exit selection mode on navigate- store.getState().exitSelectionMode();+ selectionStore.getState().toggleSelectionMode(false);ReactDOM.render(<AnimatePresence exitBeforeEnter={true}><Animated.Flex@@ -101,7 +102,7 @@ class Navigator {export default Navigator;function NavigationContainer(props) {- const openSideMenu = useStore(store => store.openSideMenu);+ const toggleSideMenu = useStore(store => store.toggleSideMenu);const isSelectionMode = useStore(store => store.isSelectionMode);const exitSelectionMode = useStore(store => store.exitSelectionMode);const selectAll = useStore(store => store.selectAll);@@ -123,7 +124,7 @@ function NavigationContainer(props) {</Box>)}<Box- onClick={openSideMenu}+ onClick={toggleSideMenu}height={38}ml={-5}sx={{diff --git a/apps/web/src/stores/selection-store.js b/apps/web/src/stores/selection-store.jsnew file mode 100644index 000000000..4bbc0fc2d--- /dev/null+++ b/apps/web/src/stores/selection-store.js@@ -0,0 +1,45 @@+import createStore from "../common/store";+import BaseStore from "./index";++class SelectionStore extends BaseStore {+ selectedItems = [];+ shouldSelectAll = false;+ isSelectionMode = false;++ toggleSelectionMode = toggleState => {+ this.set(state => {+ const isSelectionMode =+ toggleState !== undefined ? toggleState : !state.isSelectionMode;+ state.isSelectionMode = isSelectionMode;+ state.shouldSelectAll = false;+ state.selectedItems = [];+ });+ };++ selectItem = item => {+ const index = this.selectedItems.findIndex(v => item.id === v.id);+ this.set(state => {+ if (index >= 0) {+ state.selectedItems.splice(index, 1);+ } else {+ state.selectedItems.push(item);+ }+ });+ if (this.selectedItems.length <= 0) {+ this.toggleSelectionMode();+ }+ };++ setSelectedItems(items) {+ this.set(state => (state.selectedItems = items));+ }++ selectAll() {+ if (!this.isSelectionMode) return;+ this.set(state => (state.shouldSelectAll = true));+ }+}++const [useStore, store] = createStore(SelectionStore);++export { useStore, store };