Files
notesnook/apps/mobile/app/components/selection-header/index.tsx

313 lines
10 KiB
TypeScript
Raw Normal View History

2023-11-24 15:11:38 +05:00
/*
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 { Item, ItemType, VirtualizedGrouping } from "@notesnook/core";
2024-11-27 13:55:41 +05:00
import { strings } from "@notesnook/intl";
2023-11-24 15:11:38 +05:00
import { useThemeColors } from "@notesnook/theme";
2024-04-30 16:37:40 +05:00
import React, { useEffect, useRef } from "react";
2023-11-24 15:11:38 +05:00
import {
BackHandler,
NativeEventSubscription,
Platform,
View
} from "react-native";
import { Menu } from "react-native-material-menu";
2023-11-24 15:11:38 +05:00
import { db } from "../../common/database";
import { ToastManager } from "../../services/event-manager";
import Navigation from "../../services/navigation";
import useNavigationStore from "../../stores/use-navigation-store";
import { useSelectionStore } from "../../stores/use-selection-store";
2025-02-18 10:56:27 +05:00
import { useTrashStore } from "../../stores/use-trash-store";
2023-11-24 15:11:38 +05:00
import { deleteItems } from "../../utils/functions";
2024-12-06 10:27:42 +05:00
import { fluidTabsRef } from "../../utils/global-refs";
2023-11-24 15:11:38 +05:00
import { updateNotebook } from "../../utils/notebooks";
import { AppFontSize } from "../../utils/size";
2024-12-06 10:27:42 +05:00
import { DefaultAppStyles } from "../../utils/styles";
2023-11-24 15:11:38 +05:00
import { sleep } from "../../utils/time";
import { presentDialog } from "../dialog/functions";
import MoveNoteSheet from "../sheets/add-to";
import ExportNotesSheet from "../sheets/export-notes";
import ManageTagsSheet from "../sheets/manage-tags";
2024-04-30 16:37:40 +05:00
import { MoveNotebookSheet } from "../sheets/move-notebook";
2023-11-24 15:11:38 +05:00
import { IconButton } from "../ui/icon-button";
export const SelectionHeader = React.memo(
({
items,
type,
2023-11-27 16:33:09 +05:00
id,
renderedInRoute
2023-11-24 15:11:38 +05:00
}: {
items?: VirtualizedGrouping<Item>;
id?: string;
type?: ItemType;
2023-11-27 16:33:09 +05:00
renderedInRoute?: string;
2023-11-24 15:11:38 +05:00
}) => {
2024-04-30 16:37:40 +05:00
const menuRef = useRef<Menu>(null);
2023-11-24 15:11:38 +05:00
const { colors } = useThemeColors();
const selectionMode = useSelectionStore((state) => state.selectionMode);
const selectedItemsList = useSelectionStore(
(state) => state.selectedItemsList
);
const clearSelection = useSelectionStore((state) => state.clearSelection);
const allSelected =
2023-12-27 09:40:15 +05:00
items?.placeholders?.length === selectedItemsList.length;
2023-11-24 15:11:38 +05:00
const focusedRouteId = useNavigationStore((state) => state.focusedRouteId);
useEffect(() => {
if (selectionMode) {
2024-12-06 10:27:42 +05:00
fluidTabsRef.current?.lock();
2023-11-24 15:11:38 +05:00
} else {
2024-12-06 10:27:42 +05:00
fluidTabsRef.current?.unlock();
2023-11-24 15:11:38 +05:00
}
}, [selectionMode]);
const addToFavorite = async () => {
if (!selectedItemsList.length) return;
db.notes.favorite(true, ...selectedItemsList);
Navigation.queueRoutesForUpdate();
clearSelection();
};
const restoreItem = async () => {
if (!selectedItemsList.length) return;
if ((await db.trash.restore(...selectedItemsList)) === false) return;
2023-11-24 15:11:38 +05:00
Navigation.queueRoutesForUpdate();
clearSelection();
ToastManager.show({
2024-08-06 15:05:38 +05:00
heading: strings.restored(),
2023-11-24 15:11:38 +05:00
type: "success"
});
};
const deleteItem = async () => {
if (!type) return;
2023-11-24 15:11:38 +05:00
presentDialog({
2025-02-18 10:56:27 +05:00
title: strings.doActions.permanentlyDelete.unknown(
type,
selectedItemsList.length
),
paragraph: strings.actionConfirmations.permanentlyDelete.unknown(
2024-11-27 13:55:41 +05:00
type,
selectedItemsList.length
),
2024-08-06 15:05:38 +05:00
positiveText: strings.delete(),
negativeText: strings.cancel(),
2023-11-24 15:11:38 +05:00
positivePress: async () => {
if (!selectedItemsList.length) return;
await db.trash.delete(...selectedItemsList);
2025-02-18 10:56:27 +05:00
useTrashStore.getState().refresh();
2023-11-24 15:11:38 +05:00
clearSelection();
},
positiveType: "errorShade"
});
};
useEffect(() => {
const onBackPress = () => {
clearSelection();
return true;
};
let sub: NativeEventSubscription | undefined;
if (selectionMode) {
sub = BackHandler.addEventListener("hardwareBackPress", onBackPress);
}
return () => {
sub?.remove();
};
}, [clearSelection, selectionMode]);
return selectionMode !== type || focusedRouteId !== id ? null : (
2024-04-30 16:37:40 +05:00
<View
2023-11-24 15:11:38 +05:00
style={{
width: "100%",
backgroundColor: colors.primary.background,
2024-12-06 10:27:42 +05:00
paddingVertical: DefaultAppStyles.GAP_VERTICAL,
2023-11-24 15:11:38 +05:00
alignItems: "center",
flexDirection: "row",
zIndex: 999,
2024-12-06 10:27:42 +05:00
paddingHorizontal: DefaultAppStyles.GAP,
position: "absolute",
bottom: 0,
borderTopWidth: 1,
borderColor: colors.primary.border,
justifyContent: "space-between"
2023-11-24 15:11:38 +05:00
}}
>
<View
style={{
flexDirection: "row",
alignItems: "center",
2024-12-06 10:27:42 +05:00
gap: DefaultAppStyles.GAP_SMALL
2023-11-24 15:11:38 +05:00
}}
>
<IconButton
onPress={async () => {
useSelectionStore
.getState()
2023-12-27 09:40:15 +05:00
.setAll(allSelected ? [] : [...((await items?.ids()) || [])]);
2023-11-24 15:11:38 +05:00
}}
size={AppFontSize.lg}
2024-12-06 10:27:42 +05:00
color={allSelected ? colors.primary.accent : colors.primary.icon}
2023-11-24 15:11:38 +05:00
name="select-all"
/>
2024-12-06 10:27:42 +05:00
{!selectedItemsList.length
? null
: [
2024-04-30 16:37:40 +05:00
{
2024-08-06 15:05:38 +05:00
title: strings.move(),
2024-04-30 16:37:40 +05:00
onPress: async () => {
const ids = selectedItemsList;
const notebooks = await db.notebooks.all.items(ids);
MoveNotebookSheet.present(notebooks);
},
visible: renderedInRoute === "Notebooks",
icon: "arrow-right-bold-box-outline"
},
{
2024-08-06 15:05:38 +05:00
title: strings.manageTags(),
2024-04-30 16:37:40 +05:00
onPress: async () => {
await sleep(100);
ManageTagsSheet.present(selectedItemsList);
},
visible: type === "note",
icon: "pound"
},
{
2024-08-06 15:05:38 +05:00
title: strings.export(),
2024-04-30 16:37:40 +05:00
onPress: async () => {
await sleep(100);
ExportNotesSheet.present(selectedItemsList);
},
visible: type === "note",
icon: "export"
},
{
2024-08-06 15:05:38 +05:00
title: strings.linkNotebook(),
2024-04-30 16:37:40 +05:00
onPress: async () => {
await sleep(100);
MoveNoteSheet.present();
},
visible: type === "note",
icon: "plus"
},
{
2024-08-06 15:05:38 +05:00
title: strings.unlinkNotebook(),
2024-04-30 16:37:40 +05:00
onPress: async () => {
if (!id) return;
await db.notes.removeFromNotebook(id, ...selectedItemsList);
updateNotebook(id);
Navigation.queueRoutesForUpdate();
clearSelection();
},
visible: renderedInRoute === "Notebook",
icon: "minus"
},
{
2024-08-06 15:05:38 +05:00
title: strings.unfavorite(),
2024-04-30 16:37:40 +05:00
onPress: addToFavorite,
visible: focusedRouteId === "Favorites",
icon: "star-off"
},
{
2024-08-06 15:05:38 +05:00
title: strings.moveToTrash(),
2024-04-30 16:37:40 +05:00
onPress: async () => {
2024-11-27 13:55:41 +05:00
const selection = useSelectionStore.getState();
if (!selection.selectionMode) return;
await deleteItems(
selection.selectionMode as ItemType,
selection.selectedItemsList
);
selection.clearSelection();
selection.setSelectionMode(undefined);
},
visible: type === "note" || type === "notebook",
icon: "delete"
},
{
title: strings.doActions.delete.unknown(
type!,
selectedItemsList.length
),
onPress: async () => {
const selection = useSelectionStore.getState();
if (!selection.selectionMode) return;
await deleteItems(
selection.selectionMode as ItemType,
selection.selectedItemsList
);
selection.clearSelection();
selection.setSelectionMode(undefined);
2024-04-30 16:37:40 +05:00
},
2024-11-27 13:55:41 +05:00
visible:
type !== "trash" && type !== "note" && type !== "notebook",
2024-04-30 16:37:40 +05:00
icon: "delete"
},
{
2024-08-06 15:05:38 +05:00
title: strings.restore(),
2024-04-30 16:37:40 +05:00
onPress: restoreItem,
visible: type === "trash",
icon: "delete-restore"
},
{
2024-08-06 15:05:38 +05:00
title: strings.delete(),
2024-04-30 16:37:40 +05:00
onPress: deleteItem,
visible: type === "trash",
icon: "delete"
}
].map((item) =>
!item.visible ? null : (
2024-12-06 10:27:42 +05:00
<IconButton
size={AppFontSize.lg}
2024-04-30 16:37:40 +05:00
type="plain"
2025-02-14 11:08:14 +05:00
testID={`select-${item.icon}`}
2024-12-06 10:27:42 +05:00
name={item.icon}
2024-04-30 16:37:40 +05:00
key={item.title}
2024-12-06 10:27:42 +05:00
color={colors.primary.icon}
2024-04-30 16:37:40 +05:00
onPress={async () => {
//@ts-ignore
2024-04-30 16:37:40 +05:00
menuRef.current?.hide();
if (Platform.OS === "ios") await sleep(300);
item.onPress();
}}
/>
)
)}
2023-11-24 15:11:38 +05:00
</View>
2024-12-06 10:27:42 +05:00
<IconButton
size={AppFontSize.lg}
2024-12-06 10:27:42 +05:00
onPress={() => {
clearSelection();
}}
color={colors.primary.icon}
name="close"
/>
2024-04-30 16:37:40 +05:00
</View>
2023-11-24 15:11:38 +05:00
);
}
);
SelectionHeader.displayName = "SelectionHeader";
export default SelectionHeader;