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

353 lines
11 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";
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";
2024-04-30 16:37:40 +05:00
import Menu from "react-native-reanimated-material-menu/src/Menu";
2023-11-24 15:11:38 +05:00
import { db } from "../../common/database";
import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets";
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";
import { deleteItems } from "../../utils/functions";
import { tabBarRef } from "../../utils/global-refs";
import { updateNotebook } from "../../utils/notebooks";
import { SIZE } from "../../utils/size";
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";
import { Button } from "../ui/button";
2023-11-24 15:11:38 +05:00
import { IconButton } from "../ui/icon-button";
import Heading from "../ui/typography/heading";
2024-08-06 15:05:38 +05:00
import { strings } from "@notesnook/intl";
2023-11-24 15:11:38 +05:00
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);
const { colors: contextMenuColors } = useThemeColors("contextMenu");
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 insets = useGlobalSafeAreaInsets();
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) {
tabBarRef.current?.lock();
} else {
tabBarRef.current?.unlock();
}
}, [selectionMode]);
const addToFavorite = async () => {
if (!selectedItemsList.length) return;
db.notes.favorite(true, ...selectedItemsList);
Navigation.queueRoutesForUpdate();
clearSelection();
};
const restoreItem = async () => {
if (!selectedItemsList.length) return;
await db.trash.restore(...selectedItemsList);
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 () => {
presentDialog({
2024-08-06 15:05:38 +05:00
title: strings.deleteItems(type as string, selectedItemsList.length),
paragraph: strings.deleteItemsConfirmation(
type as string,
selectedItemsList.length
),
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);
Navigation.queueRoutesForUpdate();
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%",
height: Platform.OS === "android" ? 50 + insets.top : 50,
paddingTop: Platform.OS === "android" ? insets.top : null,
backgroundColor: colors.primary.background,
justifyContent: "space-between",
alignItems: "center",
flexDirection: "row",
zIndex: 999,
2024-04-30 16:37:40 +05:00
paddingHorizontal: 12
2023-11-24 15:11:38 +05:00
}}
>
<View
style={{
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center",
borderRadius: 100
}}
>
<IconButton
2024-02-05 12:14:45 +05:00
style={{
2023-11-24 15:11:38 +05:00
justifyContent: "center",
alignItems: "center",
height: 40,
width: 40,
borderRadius: 100,
marginRight: 10
}}
onPress={() => {
clearSelection();
}}
color={colors.primary.icon}
name="close"
/>
<View
style={{
height: 40,
borderRadius: 100,
paddingHorizontal: 16,
justifyContent: "center",
flexDirection: "row",
alignItems: "center"
}}
>
2024-04-30 16:37:40 +05:00
<Heading size={SIZE.lg} color={colors.primary.paragraph}>
2023-11-24 15:11:38 +05:00
{selectedItemsList.length}
</Heading>
</View>
</View>
2024-04-30 16:37:40 +05:00
2023-11-24 15:11:38 +05:00
<View
style={{
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center"
}}
>
<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
}}
tooltipText="Select all"
tooltipPosition={4}
2024-02-05 12:14:45 +05:00
style={{
2023-11-24 15:11:38 +05:00
marginLeft: 10
}}
color={
allSelected ? colors.primary.accent : colors.primary.paragraph
}
name="select-all"
/>
2024-04-30 16:37:40 +05:00
{selectedItemsList.length ? (
<Menu
ref={menuRef}
animationDuration={200}
2024-02-05 12:14:45 +05:00
style={{
2024-04-30 16:37:40 +05:00
borderRadius: 5,
backgroundColor: contextMenuColors.primary.background,
marginTop: -20
2023-11-24 15:11:38 +05:00
}}
2024-04-30 16:37:40 +05:00
onRequestClose={() => {
menuRef.current?.hide();
2023-11-24 15:11:38 +05:00
}}
2024-04-30 16:37:40 +05:00
anchor={
<IconButton
onPress={() => {
menuRef.current?.show();
}}
name="dots-vertical"
color={colors.primary.paragraph}
/>
}
>
{[
{
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 () => {
deleteItems(
undefined,
useSelectionStore.getState().selectionMode
).then(() => {
useSelectionStore.getState().clearSelection();
useSelectionStore.getState().setSelectionMode(undefined);
});
},
visible: type !== "trash",
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 : (
<Button
style={{
width: 150,
justifyContent: "flex-start",
borderRadius: 0
}}
type="plain"
buttonType={{
text: contextMenuColors.primary.paragraph
}}
icon={item.icon}
key={item.title}
title={item.title}
onPress={async () => {
menuRef.current?.hide();
if (Platform.OS === "ios") await sleep(300);
item.onPress();
}}
/>
)
)}
</Menu>
2023-11-24 15:11:38 +05:00
) : null}
</View>
2024-04-30 16:37:40 +05:00
</View>
2023-11-24 15:11:38 +05:00
);
}
);
SelectionHeader.displayName = "SelectionHeader";
export default SelectionHeader;